In addition to my post (that has been linked above) a small sketch.
Chrisir
ArrayList<SimplePoint> bList = new ArrayList();
boolean runPoints=false;
float t;
// drag with mouse
SimplePoint holding;
boolean hold=false;
void setup() {
size(800, 800);
bList = new ArrayList();
background(0);
}
void draw() {
background(0);
// Run green ball
if (runPoints&&bList.size()==4) {
displayRunningBall();
}
// decoration and showing curve ---------
// show points
int i_show_points=0;
for (SimplePoint bz : bList) {
bz.display( i_show_points );
i_show_points++;
}
// show Bezier
if (bList.size()==4) {
showBezier();
}
// text
fill(255);
text("Enter 4 points with the mouse (0 and 3 are anchors, 1 and 2 control points). \n"
+"Then hit Enter to run a ball on the curve (Enter to stop). C to clear list.",
20, 20);
// drag mouse
if (hold) {
holding.x=mouseX;
holding.y=mouseY;
}//if
}//func
// ---------------------------------------------------------------------
void keyPressed() {
if (keyCode == ENTER||keyCode == RETURN) {
runPoints = !runPoints;
// reset
t=0;
} else if (key=='c') {
bList.clear();
}
}
void mousePressed() {
// Do we have 4 points already?
if (bList.size()<4) {
// No
SimplePoint myBezier = new SimplePoint(mouseX, mouseY);
bList.add(myBezier);
runPoints=false;
} else
{
// Yes, drag with mouse
for (SimplePoint bz : bList) {
if (dist(mouseX, mouseY, bz.x, bz.y) < 10) {
hold=true;
holding=bz;
}
}
}//else
}//
void mouseReleased() {
hold = false;
holding = null;
}
// -------------------------------------------------------------------
void displayRunningBall() {
int i=0;
SimplePoint a1Point=(SimplePoint)bList.get(i);
SimplePoint c1Point=(SimplePoint)bList.get(i+1);
SimplePoint c2Point=(SimplePoint)bList.get(i+2);
SimplePoint a2Point=(SimplePoint)bList.get(i+3);
float x = bezierPoint(a1Point.x, c1Point.x, c2Point.x, a2Point.x, t/10);
float y = bezierPoint(a1Point.y, c1Point.y, c2Point.y, a2Point.y, t/10);
noStroke();
fill(0, 255, 0);
ellipse(x, y, 10, 10);
strokeWeight (1);
t += 0.1;
if (t>10) {
t=0;
}
}// func
void showBezier() {
int i2=0;
SimplePoint a1Point=(SimplePoint)bList.get(i2);
SimplePoint c1Point=(SimplePoint)bList.get(i2+1);
SimplePoint c2Point=(SimplePoint)bList.get(i2+2);
SimplePoint a2Point=(SimplePoint)bList.get(i2+3);
stroke(255);
noFill();
bezier ( a1Point.x, a1Point.y,
c1Point.x, c1Point.y,
c2Point.x, c2Point.y,
a2Point.x, a2Point.y);
}//func
// ===================================================================
class SimplePoint {
float x;
float y;
SimplePoint(float _x, float _y ) {
x = _x;
y = _y;
}
void display( int i ) {
stroke(255);
point(x, y);
point(x+1, y+1);
fill(255);
text(i,
x+4, y+5);
}
}//class
//