// Cubic Bezier Control Point Calculator 0.1 // by aaron siegel (11-2-05) Curve curve1; class Curve{ int x1, y1, x2, y2, xdiff, ydiff, midx, midy; float bend, perpx, perpy, xdiff1, ydiff1, xdiff2, ydiff2, con1x, con1y, con2x, con2y; //contstructor Curve(int xa, int ya, int xb, int yb, float b){ x1 = xa; y1 = ya; x2 = xb; y2 = yb; bend = b; // find which value has more to keep diff a positive value // also used to calculate mid point if(x1 > x2){ xdiff = x1 - x2; midx = x2 + xdiff/2; } else { xdiff = x2 - x1; midx = x1 + xdiff/2; } if(y1 > y2){ ydiff = y1 - y2; midy = y2 + ydiff/2; } else { ydiff = y2 - y1; midy = y1 + ydiff/2; } // find perpindicular point (works for quadratic beziers) if((y1 < y2) && (x1 < x2)){ // node1: top/left, node2: bottom/right perpx = midx + ydiff/bend; perpy = midy - xdiff/bend; } else if((y1 > y2) && (x1 < x2)) { // node1: bottom/left, node2: top/right perpx = midx - ydiff/bend; perpy = midy - xdiff/bend; } else if((y1 > y2) && (x1 > x2)){ // node1: bottom/right, node2: top/left perpx = midx + ydiff/bend; perpy = midy - xdiff/bend; } else { // node1: top/right, node2: bottom/left perpx = midx - ydiff/bend; perpy = midy - xdiff/bend; } // find halfway points (for cubic bezier control points) // find which value has more to keep diff a positive value if(perpx > x1){ xdiff1 = perpx - x1; con1x = x1 + xdiff1/2; } else { xdiff1 = x1 - perpx; con1x = perpx + xdiff1/2; } if(perpx > x2){ xdiff2 = perpx - x2; con2x = x2 + xdiff2/2; } else { xdiff2 = x2 - perpx; con2x = perpx + xdiff2/2; } if(perpy > y1){ ydiff1 = perpy - y1; con1y = y1 + ydiff1/2; } else { ydiff1 = y1 - perpy; con1y = perpy + ydiff1/2; } if(perpy > y2){ ydiff2 = perpy - y2; con2y = y2 + ydiff2/2; } else { ydiff2 = y2 - perpy; con2y = perpy + ydiff2/2; } } void draw(){ // draw actual curve here, called every refresh stroke(0,255,0); ellipse(x1,y1,5,5); ellipse(x2,y2,5,5); stroke(255,0,0); ellipse(con1x,con1y,5,5); ellipse(con2x,con2y,5,5); stroke(0,0,255); ellipse(perpx,perpy,5,5); stroke(0); bezier(x1, y1, con1x, con1y, con2x, con2y, x2, y2); } } void setup(){ size(300,300); smooth(); int x1 = 20; int y1 = 200; int x2 = 150; int y2 = 150; int bend = 3; curve1 = new Curve(x1, y1, x2, y2, bend); } void draw(){ background(255); curve1.draw(); }