-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKoch.pde
73 lines (57 loc) · 1.63 KB
/
Koch.pde
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
// Koch Curve
// Daniel Shiffman <http://www.shiffman.net>
// A class to describe one line segment in the fractal
// Includes methods to calculate midPVectors along the line according to the Koch algorithm
class KochLine {
// Two PVectors,
// a is the "left" PVector and
// b is the "right PVector
PVector start;
PVector end;
KochLine(PVector a, PVector b) {
start = a.get();
end = b.get();
}
void display() {
stroke(200,100,200);
strokeWeight(2);
// strokeWeight(softLines ? 1 : .5);
line(start.x, start.y, end.x, end.y);
}
PVector kochA() {
return start.get();
}
// This is easy, just 1/3 of the way
PVector kochB() {
PVector v = PVector.sub(end, start);
v.div(3);
v.add(start);
return v;
}
// More complicated, have to use a little trig to figure out where this PVector is!
PVector kochC() {
PVector a = start.get(); // Start at the beginning
PVector v = PVector.sub(end, start);
v.div(3);
a.add(v); // Move to point B
rotate(v, -radians(60)); // Rotate 60 degrees
a.add(v); // Move to point C
return a;
}
// Easy, just 2/3 of the way
PVector kochD() {
PVector v = PVector.sub(end, start);
v.mult(2/3.0);
v.add(start);
return v;
}
PVector kochE() {
return end.get();
}
}
public void rotate(PVector v, float theta) {
float xTemp = v.x;
// Might need to check for rounding errors like with angleBetween function?
v.x = v.x*PApplet.cos(theta) - v.y*PApplet.sin(theta);
v.y = xTemp*PApplet.sin(theta) + v.y*PApplet.cos(theta);
}