Curba lui Koch

Java-Logo
Cod Sursa JAVA


/*
   Simple fractal program that illustrates recursion.
   Recall that the vector <-y,x> is perpendicular to the vector .
   Check the dot product.
 */
import java.awt.*;
import java.applet.*;
public class Fractal extends Applet {
  public void init()
  {
           resize(600,400);
  }  
  
  Point addPoints(Point one, Point two)
  {
      return new Point (one.x+two.x,one.y+two.y);
  }
  
  Point up(Point one, Point two, double scale)
  {
       int x = (int) (scale*(two.x-one.x));
       int y = (int) (scale*(two.y-one.y));
       return addPoints(one, new Point(x,y));
  }
  
  Point rotate(Point one,Point two)
  {
      Point p = up(one,two, 1.0/2);
      int y = (int)((one.x - two.x)/4.0 );
      int x = (int) ((two.y- one.y)/4.0);
      return addPointsp, new Point(x,y));
  }   
      
  public void drawFractal(Point b,Point e, int d, Graphics g)
  {
      if (d == 0g.drawLine(b.x,b.y,e.x,e.y);
        else
       {
          Point next = up(b,e,1.0/4);
          Point mid = rotate(b,e);
          Point last = up(b,e,3.0/4);
          drawFractal(b,next, d-1,g);
          drawFractal(next,mid,d-1,g);
          drawFractal(mid,last,d-1,g);
          drawFractal(last,e,d-1,g);
        }
  }
  
  public void paint(Graphics g)
  {
    g.setColor(Color.blue);
    Point begin = new Point(100,200);
    Point end = new Point(500,200);
    int depth = 4;     // How deep is too deep?
    drawFractal(begin,end,depth, g );
  }
}

 

Rezultat:

curba lui koch

Leave a comment