Convert between curveVertex and bezierVertex?
in
Programming Questions
•
1 year ago
Does anyone know if it's possible to convert curveVertcies into bezierVertices? I want to do this because I like curveVertices but bezierVertices are more commonly used (e.g. Svg, Geomertative) because they're more flexible.
Obviously one option is to convert them by eye, which is what I've done here:
And from how that looks it's not obviously there's a relationship between the two. I've been down into the Processing source code to see how each are drawn - and since that's taught me how to calculate intermediate points I could throw the problem into a numerical solver every time I want to make such a conversion. Is there something more elegant and mathematically I could do?
Thanks in advance for any assistance.
Best wishes,
Antony
Obviously one option is to convert them by eye, which is what I've done here:
And from how that looks it's not obviously there's a relationship between the two. I've been down into the Processing source code to see how each are drawn - and since that's taught me how to calculate intermediate points I could throw the problem into a numerical solver every time I want to make such a conversion. Is there something more elegant and mathematically I could do?
Thanks in advance for any assistance.
Best wishes,
Antony
- class Point
- {
- Point(int nX, int nY) {x = nX; y = nY;}
- int x;
- int y;
- }
- Point v0 = new Point(40, 160);
- Point v1 = new Point(40,80);
- Point c12 = new Point(39,70);
- Point c21 = new Point(53,43);
- Point v2 = new Point(60,60);
- Point c23 = new Point(68,103);
- Point c32 = new Point(65,136);
- Point v3 = new Point(80,160);
- Point v4 = new Point(160, 160);
- void setup()
- {
- size(500,400);
- smooth();
- rectMode(CENTER);
- }
- void draw()
- {
- scale(2);
- pushMatrix();
- translate(80,0);
- strokeWeight(1);
- stroke(0);
- noFill();
- // Draw Catmull-Rom curve
- beginShape();
- curveVertex(v0.x, v0.y);
- curveVertex(v1.x, v1.y);
- curveVertex(v2.x, v2.y);
- curveVertex(v3.x, v3.y);
- curveVertex(v4.x, v4.y);
- endShape();
- noStroke();
- fill(255,0,0);
- // Draw vertices for Catmull-Rom curve
- int n = 4;
- rect(v0.x, v0.y, n, n);
- rect(v1.x, v1.y, n, n);
- rect(v2.x, v2.y, n, n);
- rect(v3.x, v3.y, n, n);
- rect(v4.x, v4.y, n, n);
- popMatrix();
- stroke(0);
- noFill();
- // Draw bezier curve
- beginShape();
- vertex(v1.x, v1.y);
- bezierVertex(c12.x, c12.y, c21.x, c21.y, v2.x, v2.y);
- bezierVertex(c23.x, c23.y, c32.x, c32.y, v3.x, v3.y);
- endShape();
- // Join bezier vertices to bezier control points
- strokeWeight(0);
- stroke(0,0,255);
- line(v1.x, v1.y, c12.x, c12.y);
- line(v2.x, v2.y, c21.x, c21.y);
- line(v2.x, v2.y, c23.x, c23.y);
- line(v3.x, v3.y, c32.x, c32.y);
- noStroke();
- fill(255,0,0);
- // Draw bezier vertices
- rect(v1.x, v1.y, n, n);
- rect(v2.x, v2.y, n, n);
- rect(v3.x, v3.y, n, n);
- noStroke();
- fill(0,255,0);
- // Draw bezier control points
- rect(c12.x, c12.y, n, n);
- rect(c21.x, c21.y, n, n);
- rect(c23.x, c23.y, n, n);
- rect(c32.x, c32.y, n, n);
- }
1