getting points on a spline curve
in
Programming Questions
•
10 months ago
Hello,
I need a program that gives me the possibility to set some points (3 to a 100 points) and make a spline curve out of it.
After that it should run along the spline and give me a circle that goes right through the spline point.
I already wrote some rough code with a bezier curve and the bezierPoint() function, but I need those points for a spline curve.
Here is the code for the circle running along a bezier:
and here is what I want to have instead of the bezier curve:
I searched the Internet but did not find something that helped me.
It would be really great if someone could tell me how to change the bezier to a spline curve.
Florian
I need a program that gives me the possibility to set some points (3 to a 100 points) and make a spline curve out of it.
After that it should run along the spline and give me a circle that goes right through the spline point.
I already wrote some rough code with a bezier curve and the bezierPoint() function, but I need those points for a spline curve.
Here is the code for the circle running along a bezier:
- PVector K1, K2, K3, EndPoint; // triangle points for circle and bezier end point
void setup() {
size(900, 700);
K1 = new PVector(0, 0);
K2 = new PVector(0, 0);
K3 = new PVector(0, 0);
EndPoint = new PVector(300, 550);
}
// wenn alle punkte auf einer geraden liegen ist der Kreispunkt im unendlichen, und nicht berechnungsfähig
int steps = 100; // resulution of steps inside the bezier curve
int i = 0;
void draw() {
frameRate(9);
background(255);
text("press mouse to set the endpoint",10,10);
// drawing the bezier,bezier points, control points/lines
noFill();
bezier(650, 200, 100, 100, 600, 600, EndPoint.x, EndPoint.y);
fill(0, 255, 0);
ellipse(650, 200, 5, 5);
ellipse(100, 100, 5, 5);
ellipse(600, 600, 5, 5);
ellipse(EndPoint.x, EndPoint.y, 5, 5);
line(650, 200, 100, 100);
line(EndPoint.x, EndPoint.y, 600, 600);
fill(255);
//getting three points for circular calculations
float t = i / float(steps);
K1.x = bezierPoint(650, 100, 600, EndPoint.x, i / float(steps));
K1.y = bezierPoint(200, 100, 600, EndPoint.y, i / float(steps));
K2.x = bezierPoint(650, 100, 600, EndPoint.x, (i+1) / float(steps));
K2.y = bezierPoint(200, 100, 600, EndPoint.y, (i+1) / float(steps));
K3.x = bezierPoint(650, 100, 600, EndPoint.x, (i+2) / float(steps));
K3.y = bezierPoint(200, 100, 600, EndPoint.y, (i+2) / float(steps));
//drawing the points for circular caluclation
fill(0, 50, 200);
ellipse(K1.x, K1.y, 5, 5);
ellipse(K2.x, K2.y, 5, 5);
ellipse(K3.x, K3.y, 5, 5);
i++;
i%=steps-1;
//drawing the circle center and radius with the circleCenter function
fill(255, 0, 100);
ellipse(circleCenter(K1, K2, K3).x, circleCenter(K1, K2, K3).y, 10, 10);
noFill();
ellipse(circleCenter(K1, K2, K3).x, circleCenter(K1, K2, K3).y, PVector.dist(K1, circleCenter(K1, K2, K3))*2, PVector.dist(K1, circleCenter(K1, K2, K3))*2);
}
// changing the endpoint of the bezier
void mousePressed() {
EndPoint.x = mouseX;
EndPoint.y = mouseY;
}
PVector circleCenter(PVector A, PVector B, PVector C) {
PVector dA = PVector.sub(B, A);
PVector dB = PVector.sub(C, B);
PVector center = new PVector(0, 0);
float aSlope = dA.y/dA.x;
float bSlope = dB.y/dB.x;
center.x = (aSlope*bSlope*(A.y - C.y) + bSlope*(A.x + B.x) - aSlope*(B.x+C.x)) / (2* (bSlope-aSlope) );
if (aSlope != 0) center.y = -1*(center.x - (A.x+B.x)/2)/aSlope + (A.y+B.y)/2;
else center.y = -1 * (center.x - (B.x + C.x) / 2) / bSlope + (B.y + C.y) / 2;
return center;
}
- size(500, 500);
int[ ] coords = {
40, 40, 80, 60, 100, 100, 60, 120, 50, 150, 200, 200,200,290,340,100
};
background(255);
smooth();
noFill();
stroke(0);
beginShape();
curveVertex(0, 1); // the first control point
for (int i = 0; i < coords.length ; i+=2) curveVertex(coords[i], coords[i+1]); // is also the start point of curve
curveVertex(coords.length-2, coords.length-1); // is also the last control point
endShape();
fill(255, 0, 0);
noStroke();
for (int i = 0; i < coords.length; i += 2) ellipse(coords[i], coords[i + 1], 3, 3);
I searched the Internet but did not find something that helped me.
It would be really great if someone could tell me how to change the bezier to a spline curve.
Florian
1