import java.applet.Applet; import java.awt.*; public class Triangles extends Applet { public void drawRecursiveTriangles( int x1, int y1, int x2, int y2, int x3, int y3, Graphics surface) { if (((x1-x2)*(x1-x2) +(y1-y2)*(y1-y2)) > 1) // square of the length between // (x1,y1) and (x2,y2) { surface.drawLine(x1,y1,x2,y2); surface.drawLine(x2,y2,x3,y3); surface.drawLine(x3,y3,x1,y1); drawRecursiveTriangles( (x1+x2)/2,(y1+y2)/2, (x1+x3)/2, (y1+y3)/2, x1,y1, surface); drawRecursiveTriangles( (x1+x2)/2,(y1+y2)/2, (x3+x2)/2, (y3+y2)/2, x2,y2, surface); drawRecursiveTriangles( (x1+x3)/2, (y1+y3)/2, (x3+x2)/2, (y3+y2)/2, x3,y3, surface); } } public void paint(Graphics g) { // use points for the largest triangle drawRecursiveTriangles(10,280,290,280,150,40, g); } }