Drag and drop the points, then grab the formatted code from the console
A fork of Bezier Curve Editor for Processing by Tony
xxxxxxxxxx
Bezier bez;
Point selectedPoint;
// --- Palette ---
color red = #d04648;
color dark = #140c1c;
color yellow = #dad45e;
color light_blue = #6dc2ca;
color peppermint = #deeed6;
void setup() {
//fullScreen();
size(1000, 1000);
//rectMode(CENTER);
smooth();
bez = new Bezier(width/2, height/2);
}
void draw() {
background(dark);
stroke(peppermint);
strokeWeight(10);
rect(0,0,width,height);
bez.draw();
}
//=========================================
// Event Call-backs
//-----------------
void mousePressed() {
selectedPoint = getPointUnderMouse();
}
void mouseDragged() {
if (selectedPoint != null) {
selectedPoint.x = mouseX;
selectedPoint.y = mouseY;
bez.recalculateCenter();
}
}
void mouseReleased() {
selectedPoint = null;
println('bezier(' +
bez.start.x + ", " + bez.start.y + ", " +
bez.ctrlStart.x + ", " + bez.ctrlStart.y + ", " +
bez.ctrlEnd.x + ", " + bez.ctrlEnd.y + ", " +
bez.end.x + ", " + bez.end.y + ")");
}
//=========================================
// Classes
//------------------
class Bezier {
Point start;
Point end;
Point ctrlStart;
Point ctrlEnd;
Point center;
Bezier(float x, float y) {
float len = 100;
center = new Point(x, y, peppermint);
start = new Point(x - len, y - len, light_blue);
end = new Point(x + len, y - len, yellow);
ctrlStart = new Point(x - len, y + len, light_blue);
ctrlEnd = new Point(x + len, y + len, yellow);
}
void recalculateCenter() {
// recalculate center by averaging the outer points
center.updatePoints(averagePoint(new Point[] { start, ctrlStart, ctrlEnd, end }));
}
void draw() {
stroke(peppermint);
strokeWeight(1);
line(start.x, start.y, ctrlStart.x, ctrlStart.y);
line(ctrlEnd.x, ctrlEnd.y, end.x, end.y);
stroke(red);
strokeWeight(3);
noFill();
bezier(
start.x, start.y,
ctrlStart.x, ctrlStart.y,
ctrlEnd.x, ctrlEnd.y,
end.x, end.y
);
start.draw();
ctrlStart.draw();
ctrlEnd.draw();
end.draw();
center.draw();
noFill();
}
Point[] getPoints() {
return new Point[] {
start, ctrlStart, ctrlEnd, end, center };
}
}
class Point {
float x, y;
float drawSize = 16;
color drawColor = light_blue;
Point(float a, float b, color c) {
x = a;
y = b;
drawColor = c;
}
void draw() {
noStroke();
fill(this.drawColor);
ellipse(x, y, drawSize, drawSize);
}
boolean hotSpotContains(float x, float y) {
return dist(x, y, this.x, this.y) < drawSize;
}
void updatePoints (Point newPoint) {
x = newPoint.x;
y = newPoint.y;
}
}
//=========================================
// Helper functions
//------------------
Point getPointUnderMouse() {
Point[] points = bez.getPoints();
for (int j = 0; j < points.length; j++) {
if (points[j].hotSpotContains(mouseX, mouseY))
return points[j];
}
return null;
}
Point averagePoint (Point[] points) {
float xSum = 0;
float ySum = 0;
for (int i = 0; i < points.length; i++) {
xSum += points[i].x;
ySum += points[i].y;
}
return new Point(xSum/points.length, ySum/points.length, peppermint);
}