// Helene Martin, CSE 142 // Draws a Sierpinski triangle generated using the chaos game. // The three points (x1, y1), (x2, y2) and (x3, y3) are three // points defining an equilateral triangle of side 500. Try changing // one or more of them to skew the trianble. // If you think this is cool, you may be a computer scientist... // (or a mathematician) import java.awt.*; import java.util.*; public class Triangle { public static void main(String[] args) { DrawingPanel p = new DrawingPanel(500, 500); Graphics g = p.getGraphics(); Random r = new Random(); // left-most point of the triangle int x1 = 0; int y1 = 433; // middle point of the triangle int x2 = 250; int y2 = 0; // right-most point of the triangle int x3 = 500; int y3 = 433; int x = 0; int y = 0; for (int i = 0; i < 100000; i++) { int rand = r.nextInt(3); if (rand == 0) { x = (x + x1) / 2; y = (y + y1) / 2; } else if (rand == 1) { x = (x + x2) / 2; y = (y + y2) / 2; } else { x = (x + x3) / 2; y = (y + y3) / 2; } g.fillRect(x, y, 1, 1); p.sleep(1); } } }