-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSierpinskiTriangle.java
More file actions
116 lines (99 loc) · 3.74 KB
/
SierpinskiTriangle.java
File metadata and controls
116 lines (99 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import java.util.Arrays;
/**
* Draws the Sierpinski Triangle recursively.
*/
public class SierpinskiTriangle {
// Window width and height
private static final int WIDTH = 960;
private static final int HEIGHT = 540;
// Changes how close the triangle's vertices are to the edge of the screen
private static final int EDGE_FACTOR = 40;
// Positive integer for number of levels
private static final int ORDER = 6;
/** Empty constructor */
public SierpinskiTriangle() {}
/** Sets up the window to draw. */
public static void setUpWindow() {
StdDraw.setTitle("Recursive Fractal");
StdDraw.setCanvasSize(WIDTH, HEIGHT);
StdDraw.setXscale(0, WIDTH);
StdDraw.setYscale(0, HEIGHT);
StdDraw.setPenRadius(0.005);
StdDraw.setPenColor(StdDraw.BLACK);
// Initial coordinates of the triangle
double[] x = {WIDTH/EDGE_FACTOR, WIDTH/2, WIDTH - WIDTH/EDGE_FACTOR};
double[] y = {HEIGHT/EDGE_FACTOR, HEIGHT - HEIGHT/EDGE_FACTOR, HEIGHT/EDGE_FACTOR};
StdDraw.polygon(x, y);
// Draw the middle top triangle
drawTop(Arrays.copyOf(x, 3), Arrays.copyOf(y, 3), ORDER);
// Draw the bottom left triangle
drawLeft(Arrays.copyOf(x, 3), Arrays.copyOf(y, 3), ORDER);
// Draw the bottom right triangle
drawRight(Arrays.copyOf(x, 3), Arrays.copyOf(y, 3), ORDER);
}
/**
* Draws the middle top triangle.
* @param x X-coordinates of the triangle to draw
* @param y Y-coordinates of the triangle to draw
* @param order The triangle's level (depth)
*/
public static void drawTop(double[] x, double[] y, int order) {
if (order == 0)
return;
else {
x[0] = (x[0] + x[1])/2;
x[2] = (x[1] + x[2])/2;
y[0] = (y[0] + y[1])/2;
y[2] = y[0];
StdDraw.polygon(x, y);
drawTop(Arrays.copyOf(x, 3), Arrays.copyOf(y, 3), order - 1);
drawLeft(Arrays.copyOf(x, 3), Arrays.copyOf(y, 3), order - 1);
drawRight(Arrays.copyOf(x, 3), Arrays.copyOf(y, 3), order - 1);
}
}
/**
* Draws the bottom left triangle.
* @param x X-coordinates of the triangle to draw
* @param y Y-coordinates of the triangle to draw
* @param order The triangle's level (depth)
*/
public static void drawLeft(double[] x, double[] y, int order) {
if (order == 0)
return;
else {
x[2] = x[1];
x[1] = (x[0] + x[1])/2;
y[1] = (y[0] + y[1])/2;
StdDraw.polygon(x, y);
drawTop(Arrays.copyOf(x, 3), Arrays.copyOf(y, 3), order - 1);
drawLeft(Arrays.copyOf(x, 3), Arrays.copyOf(y, 3), order - 1);
drawRight(Arrays.copyOf(x, 3), Arrays.copyOf(y, 3), order - 1);
}
}
/**
* Draws the bottom right triangle.
* @param x X-coordinates of the triangle to draw
* @param y Y-coordinates of the triangle to draw
* @param order The triangle's level (depth)
*/
public static void drawRight(double[] x, double[] y, int order) {
if (order == 0)
return;
else {
x[0] = x[1];
x[1] = (x[1] + x[2])/2;
y[1] = (y[0] + y[1])/2;
StdDraw.polygon(x, y);
drawTop(Arrays.copyOf(x, 3), Arrays.copyOf(y, 3), order - 1);
drawLeft(Arrays.copyOf(x, 3), Arrays.copyOf(y, 3), order - 1);
drawRight(Arrays.copyOf(x, 3), Arrays.copyOf(y, 3), order - 1);
}
}
/**
* Run the Sierpinski Triangle program.
* @param args Arguments
*/
public static void main(String[] args) {
setUpWindow();
}
}