Skip to content

Instantly share code, notes, and snippets.

@jmorrow1
Last active October 19, 2016 21:58
Show Gist options
  • Save jmorrow1/dd209e86eeb214dbfcbda639079341d6 to your computer and use it in GitHub Desktop.
Save jmorrow1/dd209e86eeb214dbfcbda639079341d6 to your computer and use it in GitHub Desktop.
//James Morrow
//jamesmorrowdesign.com
Point[][] pts;
float amt;
boolean complete = false;
ArrayList<Point> bezier = new ArrayList<Point>();
void setup() {
size(600, 600);
//pts = fromInput(regularPolygon(width/2, height/2, 200, 5));
pts = fromInput(randomPolygon(width/2, height/2, 200, 4));
}
Point[] regularPolygon(float x, float y, float r, int n) {
Point[] pts = new Point[n];
for (int i=0; i<n; i++) {
float angle = PApplet.map(i, 0, n, 0, TWO_PI);
pts[i] = new Point(x + r*PApplet.cos(angle), y + r*PApplet.sin(angle));
}
return pts;
}
Point[] randomPolygon(float x, float y, float r, int n) {
float[] angles = new float[n];
for (int i=0; i<n; i++) {
angles[i] = random(TWO_PI);
}
angles = sort(angles);
Point[] pts = new Point[n];
for (int i=0; i<n; i++) {
pts[i] = new Point(x + r*cos(angles[i]), y + r*sin(angles[i]));
}
return pts;
}
static Point[][] fromInput(Point[] input) {
Point[][] pts = new Point[input.length][];
pts[0] = input;
for (int i=1; i<pts.length; i++) {
pts[i] = new Point[pts.length-i];
for (int j=0; j<pts[i].length; j++) {
pts[i][j] = new Point(0, 0);
}
}
return pts;
}
void draw() {
background(255);
strokeWeight(4);
//draw bezier
stroke(175, 0, 0);
noFill();
beginShape();
for (int i=0; i<bezier.size(); i++) {
Point pt = bezier.get(i);
vertex(pt.x, pt.y);
}
endShape();
strokeWeight(2);
stroke(0);
fill(0);
ellipseMode(CENTER);
for (int i=0; i<pts.length-1; i++) {
for (int j=0; j<pts[i].length-1; j++) {
//draw
line(pts[i][j].x, pts[i][j].y, pts[i][j+1].x, pts[i][j+1].y);
//trace
lerp(pts[i+1][j], pts[i][j], pts[i][j+1], amt);
ellipse(pts[i+1][j].x, pts[i+1][j].y, 12, 12);
}
}
amt += 0.005f;
if (!complete) {
bezier.add(new Point(pts[pts.length-1][pts[pts.length-1].length-1]));
}
if (amt > 1) {
if (!complete) {
complete = true;
println("finish");
}
amt--;
}
}
void lerp(Point pt, Point a, Point b, float amt) {
pt.x = lerp(a.x, b.x, amt);
pt.y = lerp(a.y, b.y, amt);
}
static class Point {
float x, y;
Point(Point pt) {
this(pt.x, pt.y);
}
Point(float x, float y) {
this.x = x;
this.y = y;
}
String toString() {
return "(" + x + ", " + y + ")";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment