-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserpoonski.java
More file actions
118 lines (100 loc) · 2.71 KB
/
Copy pathserpoonski.java
File metadata and controls
118 lines (100 loc) · 2.71 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
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.util.TreeSet;
import javax.swing.JFrame;
public class serpoonski extends JFrame {
static class pair implements Comparable<pair>{
private int x;
private int y;
public pair(int a, int b) {
x = a;
y = b;
}
public void set(pair p) {
x = p.x;
y = p.y;
}
public pair clone() {
return new pair(x + 0, y + 0);
}
public int compareTo(pair other) {
Integer px = this.x;
Integer py = this.y;
Integer ox = other.x;
Integer oy = other.y;
if (!px.equals(ox)) return px.compareTo(ox);
if (!py.equals(oy)) return py.compareTo(oy);
return 0;
}
public boolean equals(pair other) {
return this.compareTo(other) == 0;
}
public String toString() {
return "[" + x + " " + y + "]";
}
}
@SuppressWarnings("deprecation")
public serpoonski (TreeSet<pair> points) throws Exception {
int margins = 100;
int xmax = -1;
int ymax = -1;
Canvas c = new Canvas () {
public void paint(Graphics g) {
g.setColor(Color.BLACK);
for (pair p : points) {
g.drawLine(p.x + margins, p.y + margins, p.x + margins, p.y + margins);
}
}
};
for (pair p : points) {
xmax = Math.max(p.x, xmax);
ymax = Math.max(p.y, ymax);
}
c.setBackground(Color.WHITE);
setSize(xmax + 2 * margins, ymax + 2 * margins);
add(c);
show();
}
static TreeSet<pair> gen(pair[] vertices, int n, double p, boolean diff, int diffset) {
int x = vertices[0].x;
int y = vertices[0].y;
int length = vertices.length;
int prev = 0;
int corner = 0;
double q = 1 - p;
TreeSet<pair> res = new TreeSet<pair>();
for (pair point : vertices) res.add(point.clone());
for (int i = 0; i < n; i++) {
res.add(new pair(x, y));
prev = corner;
corner = (int)(Math.random() * length);
if (diff) {
while ((corner - prev + 10 * length) % length == diffset) corner = (int)(Math.random() * length);
}
x = (int)Math.round(p * x + q * vertices[corner].x);
y = (int)Math.round(p * y + q * vertices[corner].y);
}
return res;
}
static TreeSet<pair> gen(pair[] vertices, int n, double p) {
return gen(vertices, n, p, false, 0);
}
static pair x(int x, int y) {
return new pair(x, y);
}
public static void main(String[] args) {
System.out.println("FRACTALS");
int n = 600;
pair[] triangle = {x(0, 0), x(n, 0), x(n / 2, n)};
pair[] square = {x(0, 0), x(0, n), x(n, n), x(n, 0)};
pair[] midsquare = {x(0, 0), x(0, n), x(n, n), x(n, 0), x(0, n / 2), x(n / 2, 0), x(n, n / 2), x(n / 2, n)};
try {
TreeSet list = gen(midsquare, 1000000, 1.0 / 3);
serpoonski res = new serpoonski(list);
}
catch (Exception e) {
System.out.println(e);
}
}
}