I’m trying to use the PShape curveVertex() methods and they don’t seem to be working. I’m using Processing V4.2 on a Mac M2 Monterey. Javadocs for PShape indicates support for curveVertex(). I want to keep the PShape in a variable and reuse the shape.
In making the example I noticed that support for regular vertex() in PShape methods differ (they close the shape).
PVector [] coords = new PVector [4] ;
PShape sh;
void setup() {
size(400, 400);
sh = createShape();
PVector pt1 = new PVector(10, 10);
PVector pt2 = new PVector(100, 100);
PVector pt3 = new PVector(200, 0);
PVector pt4 = new PVector(300, 100);
coords[0] = pt1;
coords[1] = pt2;
coords[2] = pt3;
coords[3] = pt4;
}
void draw() {
background(0);
text("PShape Methods", 10, 10);
// create shape with PShape methods
sh.beginShape();
sh.noFill();
sh.strokeWeight(3);
sh.stroke(255);
// first curvevertex at beginning serves as control point
sh.curveVertex(coords[0].x, coords[0].y);
for (int j = 0; j<4; j++) {
sh.curveVertex(coords[j].x, coords[j].y);
}
// last curvevertex at end for control point
sh.curveVertex(coords[3].x, coords[3].y);
sh.endShape();
shape(sh);
// Processing methods for drawing directly to canvas
translate(0, 100);
text("Processing Methods", 10, 10);
noFill();
strokeWeight(3);
stroke(255);
beginShape();
// extra curvevertex at beginning serves as control point
curveVertex(coords[0].x, coords[0].y);
for (int j = 0; j<4; j++) {
curveVertex(coords[j].x, coords[j].y);
}
// extra curvevertex at end for control point
curveVertex(coords[3].x, coords[3].y);
endShape();
}
The P2D renderer crashes (on the Mac) when it’s looping (with some odd fill artifacts). I’ll need to draw this on the fly so will need to get that working.
essentially, this is about the difference between using curveVertex directly and saving it in a PShape first. Not the same result, especially on Mac, might be a bug.
this runs on WIN, same result for both, doesn’t close the shape:
PVector [] coords = {
new PVector(10, 10),
new PVector(100, 100),
new PVector(200, 0),
new PVector(300, 100)
};
PShape sh;
void setup() {
size(1400, 700);
// create shape with PShape methods
sh = createShape(PShape.PATH);
sh.beginShape();
sh.noFill();
sh.strokeWeight(3);
sh.stroke(255, 0, 0);
int[] indices = { 0, 0, 1, 2, 3, 3 };
for (int j : indices) {
sh.curveVertex(coords[j].x, coords[j].y);
}
sh.endShape();
}
void draw() {
background(0);
stroke(255);
text("PShape Methods", 10, 10);
shape(sh, 210, 10 );
// Processing methods for drawing directly to canvas
translate(0, 100);
text("Processing Methods", 10, 10);
noFill();
strokeWeight(3);
stroke(255);
beginShape();
// extra curvevertex at beginning serves as control point
curveVertex(coords[0].x, coords[0].y);
for (int j = 0; j<4; j++) {
curveVertex(coords[j].x, coords[j].y);
}
// extra curvevertex at end for control point
curveVertex(coords[3].x, coords[3].y);
endShape();
}
Looking at both Glv and Chrisir explorations I learn a few things…
Mainly that the trouble seems to stem from redefining the vertices on a shape that has already been created. If you move the definition of the shape to the setup() - it works perfectly (I can’t do this because the shape is dynamic).
Or if you use createShape() again in the draw() loop, works perfectly. I wonder if there’s a performance or memory hit for putting the createShape in the draw() loop?
(by the way I found that there’s a way to explicitly specify that you want an open path, which probably is the default. But it still doesn’t work in this case)
sh.endShape(PShape.OPEN);
It looks like there’s a way to cycle through the pre-existing vertices and move them as seen here:
But that’s going to be hairy for my application because the number of vertices is constantly changing.