package triangleMod;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Point;
class Triangle extends Component{
private static final long serialVersionUID = 1L;
//A, B, C initial triangle-points, assigned in the constructor
private Point A, B, C;
//points for the 3 new inner triangles in the current recursive step
private Point A1, B1, C1, A2, B2, C2, A3, B3, C3;
public Triangle(Point a, Point b, Point c){
this.A = a;
this.B = b;
this.C = c;
}
//helper var for recursion termination
int i= 300;
//draws fractal
public void drawFractal(Graphics g, Point a, Point b, Point c){
/**
* draw outer triangle
**/
//AB
g.drawLine(a.x, a.y, b.x, b.y);
//AC
g.drawLine(a.x, a.y, c.x, c.y);
//BC
g.drawLine(b.x, b.y, c.x, c.y);
/**
* Points of the 3 new triangles get assigned
**/
//lower left triangle coordinates
A1 = new Point(A.x, A.y);
B1 = new Point(c.x, b.y);
C1 = new Point((a.x+c.x)/2, (c.y+a.y)/2);
//lower right triangle coordinates
A2 = new Point(c.x, b.y);
B2 = new Point(B.x, B.y);
C2 = new Point((c.x+b.x)/2, (c.y+a.y)/2);
//upper triangle coordinates
A3 = new Point((a.x+c.x)/2, (c.y+a.y)/2);
B3 = new Point((c.x+b.x)/2, (c.y+a.y)/2);
C3 = new Point(C.x, C.y);
/**
* draw inner reversed triangle
**/
//AB
g.drawLine(A3.x, A3.y, B3.x, B3.y);
//AC
g.drawLine(A3.x, A3.y, A2.x, A2.y);
//BC
g.drawLine(B3.x, B3.y, A2.x, A2.y);
/**
* rekusive method calls for the three new triangles
**/
//termination condition not set correctly, helper var i used for testing
while (i-->0) {
drawFractal(g, A1, B1, C1);
//drawFractal(g, A2, B2, C2);
//drawFractal(g, A3, B3, C3);
}
}
public void paint(Graphics g){
drawFractal(g, A, B, C);
}
} |