Reputation: 7649
I have a xy plot in which I have a straight line. I'm able to drag lines by using mouseX and mouseY values, but I would like to do this for curves.
Firstly how do I make a curve, when I only have co-ordinates for a point and then how can I drag it?
Upvotes: 0
Views: 842
Reputation: 1082
First read link mentioned by @Petros then you need calculate coordinates for control points so they lay on your line. Then you have to implement mouse events for dragging points. You can be inspired by this basic example.
float[] p = { 50, 100, 80, 100, 150, 100, 180, 100 };
int point = 0;
boolean locked = false;
void setup() {
size(300, 200);
smooth();
}
void draw() {
background(255);
stroke(0);
noFill();
//Here you can just change from bazier to spline curve
//curve(p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
bezier(p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
noStroke();
fill(255, 0, 0);
ellipse(p[0], p[1], 3, 3);
ellipse(p[6], p[7], 3, 3);
fill(0, 0, 255, 192);
ellipse(p[2], p[3], 3, 3);
ellipse(p[4], p[5], 3, 3);
}
void mousePressed() {
if(overPoint(mouseX, mouseY)) {
locked = true;
} else {
locked = false;
}
}
void mouseDragged() {
if(locked) {
p[point] = mouseX;
p[point+1] = mouseY;
}
}
void mouseReleased() {
locked = false;
}
boolean overPoint(float x, float y){
for(point = 0; point < 7; point += 2){
if(p[point] + 2 > x && p[point] - 2 < x){
if(p[point+1] + 2 > y && p[point+1] - 2 < y){
return true;
}
}
}
return false;
}
Upvotes: 1