Ask Question

Name:
Title:
Your Question:

Answer Question

Name:
Your Answer:
User Submitted Source Code!


Description:
  s
Language: JAVA
Code:
import java.awt.*;
import java.applet.*;

public class Hil extends Applet
{
    private int dist0=512,dist=dist0;
    private int x = 0, y = 0;

    public void init()
    {
        dist0 = 512;
        resize ( dist0, dist0 );
    }
    public void paint(Graphics g)
    {
        int level=4;
        dist=dist0;
        for (int i=level;i>0;i--) dist /= 2;
        goToXY ( dist/2, dist/2 );
        HilbertA(level); // start recursion
    }
    private void HilbertA (int level)
    {
        if (level > 0)
        {   HilbertB(level-1);           lineRel(0,dist);
            HilbertA(level-1);           lineRel(dist,0);
            HilbertA(level-1);           lineRel(0,-dist);
            HilbertC(level-1);
        }
    }
    private void HilbertB (int level)
    {
        if (level > 0)
        {   HilbertA(level-1);           lineRel(dist,0);
            HilbertB(level-1);            lineRel(0,dist);
            HilbertB(level-1);            lineRel(-dist,0)
            HilbertD(level-1);
        }
    }
    private void HilbertC (int level)
    {
        if (level > 0)
        {   HilbertD(level-1);    lineRel(-dist,0);
            HilbertC(level-1);    lineRel(0,-dist);
            HilbertC(level-1);    lineRel(dist,0);
            HilbertA(level-1);
        }
    }
    private void HilbertD (int level)
    {
        if (level > 0)
        {   HilbertC(level-1);    lineRel(0,-dist);
            HilbertD(level-1);    lineRel(-dist,0);
            HilbertD(level-1);    lineRel(0,dist);
            HilbertB(level-1);
        }
    }
      public void goToXY(int x, int y)
       {
             this.x = x;   
             this.y = y;
      }
    public void lineRel(int deltaX, int deltaY)
    {
         getGraphics().drawLine( x, y, x+deltaX, y+deltaY);
        x += deltaX;    
        y += deltaY;
    }
}
Comments: