Shuffling vertex order in beginshape() to draw random polygons

Hello,
this is my first post and I’m fairly new to Processing.

I have a series of vertex coordinates and I was wondering if it’s possible to shuffle the order in which they draw the polygon (v0, v1, v2, v3 -> v3, v1, v0, v2 for example). I don’t mind internal intersections as a result.

beginShape();
vertex(x0, y0);
vertex(x1, y1);
vertex(x2, y2);
vertex(x3, y3);
endShape(CLOSE);

I’m trying to make random polygons out of svg shapes which are set by specific vertices. I have to figure out how to convert the svg xml into processing, but I’m going step by step. :turtle:

Would appreciate if anyone has relevant bibliography or links newbie-friendly.

Thanks!!

you can store the data

vertex(x0, y0);
vertex(x1, y1);

more convenient in an PVector array names points1.

Since it’s an array it has an index to access its slots.

Now You can shuffle the list of indexes (inventory)

PVector[] points1 = {
  new PVector (11, 111), 
  new PVector (211, 111), 
  new PVector (311, 311), 
  new PVector (51, 511)
};

IntList inventory = new IntList();
boolean reset=false; 

void setup() {
  size(1100, 900);
  background(111); 

  for (int i=0; i<points1.length; i++) {
    inventory.append(i);
  }
}//func 

void draw() {
  background(111); 
  beginShape();
  for (int i=0; i<points1.length; i++) {
    int index=inventory.get(i); 
    vertex(points1[index].x, points1[index].y);
  }// for
  endShape(CLOSE);
}//func 

void mousePressed() {
  inventory.shuffle();
  println(inventory);
}
//

Wow! Thank you so much!
This is exactly what I was looking for. I’m going to read in depth so I can understand how it works :exploding_head:

1 Like