Files
processing4/java/examples/Topics/Fractals and L-Systems/Koch/KochLine.pde
2013-03-18 22:12:27 -04:00

68 lines
1.1 KiB
Plaintext

// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
// Koch Curve
// 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 a;
PVector b;
KochLine(PVector start, PVector end) {
a = start.get();
b = end.get();
}
void display() {
stroke(255);
line(a.x, a.y, b.x, b.y);
}
PVector start() {
return a.get();
}
PVector end() {
return b.get();
}
// This is easy, just 1/3 of the way
PVector kochleft() {
PVector v = PVector.sub(b, a);
v.div(3);
v.add(a);
return v;
}
// More complicated, have to use a little trig to figure out where this PVector is!
PVector kochmiddle() {
PVector v = PVector.sub(b, a);
v.div(3);
PVector p = a.get();
p.add(v);
v.rotate(-radians(60));
p.add(v);
return p;
}
// Easy, just 2/3 of the way
PVector kochright() {
PVector v = PVector.sub(a, b);
v.div(3);
v.add(b);
return v;
}
}