-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCantorSet.java
More file actions
64 lines (52 loc) · 1.75 KB
/
Copy pathCantorSet.java
File metadata and controls
64 lines (52 loc) · 1.75 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
import java.util.Arrays;
/**
* The Cantor Set involves drawing line segments where the middle third is
* removed as more levels are drawn.
*/
public class CantorSet {
/** Window width */
private static final int WIDTH = 960;
/** Window height */
private static final int HEIGHT = 540;
/** Positive integer for number of levels */
private static final int ORDER = 10;
/** The pen radius */
private static final double PEN_RADIUS = 0.01;
/** 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(PEN_RADIUS);
StdDraw.setPenColor(StdDraw.BLACK);
double[] x = {WIDTH/5, 4*WIDTH/5};
double[] y = {4*HEIGHT/5, 4*HEIGHT/5};
drawLine(Arrays.copyOf(x, 2), Arrays.copyOf(y, 2), 1);
}
/**
* Draws each line for the fractal.
*
* @param x The set of x coordinates
* @param y The set of y coordinates
* @param n The current level being iterated through
*/
public static void drawLine(double[] x, double[] y, int n) {
if (ORDER == n)
return;
else {
StdDraw.line(x[0], y[0], x[1], y[1]);
final double LINE_WIDTH = x[1] - x[0];
x[1] -= 2*LINE_WIDTH/3;
y[0] -= 10;
y[1] -= 10;
drawLine(Arrays.copyOf(x, 2), Arrays.copyOf(y, 2), n + 1);
x[0] += 2*LINE_WIDTH/3;
x[1] += 2*LINE_WIDTH/3;
drawLine(Arrays.copyOf(x, 2), Arrays.copyOf(y, 2), n + 1);
}
}
public static void main(String[] args) {
setUpWindow();
}
}