-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerpenskiTriangles.java
More file actions
136 lines (67 loc) · 2.67 KB
/
Copy pathSerpenskiTriangles.java
File metadata and controls
136 lines (67 loc) · 2.67 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package assignment_10528808;
/**
*
* @author Mawutor
*/
import java.awt.Color;
import java.awt.Point;
import java.util.*;
import java.awt.*;
import javax.swing.*;
public class SerpenskiTriangles
{
// midpoint(): midpoint facilitator
public static Point midpoint(Point p1, Point p2) {
return new Point((p1.x + p2.x) / 2, (p1.y + p2.y) / 2);
}
// Sierpinski(): draws the Sierpinski Fractal
public static void Sierpinski(Graphics g, Point p1, Point p2, Point p3, Color c, int depth) {
g.setColor(Color.BLACK);
if (depth == 1) {
Polygon tri = new Polygon();
//tri.paint(g);
}
else {
Color d = Color.black;
Point p4 = midpoint(p1, p2);
Point p5 = midpoint(p2, p3);
Point p6 = midpoint(p3, p1);
Sierpinski(g, p1, p4, p6, d, depth-1);
Sierpinski(g, p4, p2, p5, d, depth-1);
Sierpinski(g, p6, p5, p3, d, depth-1);
}
}
// main(): application entry point
public static void main(String[] args) {
// set up scanner
Scanner scan = new Scanner(System.in);
// display program's purpose
System.out.println("This program will draw a Sierpinski Fractal to the user's");
System.out.println("specified depth.");
System.out.println();
// determine desired cycles and color
System.out.println("How many cycles would you like the Sierpinski Fractal to");
System.out.print("be taken out to?");
int a = scan.nextInt();
System.out.println();
// produce Sierpinski Fractal
Point p1 = new Point(100, 40);
Point p2 = new Point(190, 187);
Point p3 = new Point(10, 187);
Color c = Color.black;
JFrame window = new JFrame("Sierpinski Fractal");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(200, 200);
window.setVisible(true);
Graphics g = window.getGraphics();
System.out.print("\nEnter any character when ready.");
Scanner stdin = new Scanner(System.in);
stdin.nextLine();
Sierpinski(g, p1, p2, p3, c, a);
}
}