// 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.copy(); b = end.copy(); } void display() { stroke(255); line(a.x, a.y, b.x, b.y); } PVector start() { return a.copy(); } PVector end() { return b.copy(); } // 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.copy(); 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; } }