Variable/interactive beginShape() kind?

I have a set of vertices I’m drawing within beginShape(), and I wanted to cycle through the different options each time the mouse is clicked i.e. beginShape(TRIANGLES), beginShape(POINTS), beginShape(LINES) etc.

I’ve made an array of the different options:

String[] shapeType = {"POINTS", "LINES", "TRIANGLES", "TRIANGLE_FAN", "TRIANGLE_STRIP", "QUADS", "QUAD_STRIP"};

and what I’m trying to achieve is something like this:

beginShape(shapeType[n]);
vertex(x, y);
...etc
endShape();

Where n is cycled through the shapeType[] array on mousePressed().

The only problem is that POINTS, LINES, etc are not Strings but I don’t know what else they could be! (I’m guessing they are constants?).
I’m getting the error “beginShape() expects parameters like beginShape(int)”.
But surely TRIANGLES etc isn’t an integer??!

Is there any way to achieve this kind of variable shape type?

Many thanks!

1 Like

Hi @cyanblue,

You’re rigth, the parameters in beginShape() are constants that are equal to integers.

If you look in the source code of Processing you can know to what integer correspond every option your list:

static final int POINTS          = 3;   // vertices
static final int LINES           = 5;   // beginShape(), createShape()
static final int TRIANGLES       = 9;   // vertices
static final int TRIANGLE_STRIP  = 10;  // vertices
static final int TRIANGLE_FAN    = 11;  // vertices
static final int QUADS           = 17;  // vertices
static final int QUAD_STRIP      = 18;  // vertices

Having that you can try with a int array instead.

See source

3 Likes

Thank you so much @zenemig!
That worked perfectly - and thanks for the link to the processing source code, that was really fascinating :slight_smile:

If you are using the PDE editor you will also notice that capitalized PConstants (like TRIANGLE_FAN or P2D or CENTER or UP) are never written in quotes – they are variable names – and are automatically highlighted in the editor in a green-gray color.

2 Likes