/* This class describes a basic line segment, with a few added functions for finding midpoints that we need for our Koch Fractal. */ class KochLine { // Each line has a start and end point Point start; Point end; public KochLine(Point s_, Point e_) { start = s_.copy(); end = e_.copy(); } public void render() { stroke(255); line(start.getX(), start.getY(), end.getX(), end.getY()); } public Point getStart() { return start.copy(); } public Point getEnd() { return end.copy(); } public Point getLeft() { // The left point of a Koch Fractal is just 1/3rd into the line float x = start.getX() + (end.getX() - start.getX()) / 3.0f; float y = start.getY() + (end.getY() - start.getY()) / 3.0f; return new Point(x, y); } public Point getMiddle() { // The middle point is a little harder to calculate, as it is // the center of the line, but offset from the line itself, forming // the triangle of the Koch Fractal float x = start.getX() + 0.5f * (end.getX() - start.getX()) + ( sin(radians(60)) * (end.getY() - start.getY())) / 3.0f; float y = start.getY() + 0.5f * (end.getY() - start.getY()) - ( sin(radians(60)) * (end.getX() - start.getX())) / 3.0f; return new Point(x, y); } public Point getRight() { // the right point of a Koch Fractal is 2/3rds of the way into the line float x = start.getX() + 2 * (end.getX() - start.getX()) / 3.0f; float y = start.getY() + 2 * (end.getY() - start.getY()) / 3.0f; return new Point(x, y); } }