benfry / processing4

Processing 4.x releases for Java 17
https://processing.org
Other
1.35k stars 237 forks source link

fix #791 #792

Open hx2A opened 1 year ago

hx2A commented 1 year ago

Fix for #791.

Regrettably, when I tested the fix for #643 I didn't test shapes with multiple contours. This fix will increment the codeIndex variable so that the vertex codes from the vertexCodes array stay in line with the vertices in the vertices array.

PShape s;

void setup() {
  size(400, 200);
  fill(255, 0, 0);
  noLoop();
}

void draw() {
  translate(50, 50);
  s = createShape();
  s.beginShape();
  s.fill(255, 0, 0);
  s.vertex(  0, 0);
  s.vertex(100, 0);
  s.vertex(100, 100);
  s.vertex(  0, 100);

  for (int x = 5; x <= 85; x += 20) {
    for (int y = 5; y <= 85; y += 20) {
      s.beginContour();
      s.vertex(x, y);
      s.vertex(x, y + 10);
      s.vertex(x + 10, y + 10);
      s.vertex(x + 10, y);
      s.endContour();
    }
  }

  s.endShape(CLOSE);
  shape(s, 0, 0);

  translate(200, 0);

  beginShape();
  fill(255, 0, 0);
  vertex( 0, 0);
  vertex(100, 0);
  vertex(100, 100);
  vertex(0, 100);

  for (int x = 5; x <= 85; x += 20) {
    for (int y = 5; y <= 85; y += 20) {
      beginContour();
      vertex(x, y);
      vertex(x, y + 10);
      vertex(x + 10, y + 10);
      vertex(x + 10, y);
      endContour();
    }
  }

  endShape(CLOSE);
}

image

I should have thought to test with multiple contours when I made the previous PR #776 .

Here's a much more complex example using real-life data from OpenStreetMap:

image

There's a ton of contours in this shape it is drawn correctly.

hx2A commented 1 year ago

I tested this on FX2D and it works there as well.

I reviewed the code I've been running over the past week and I see I did use shapes with multiple contours but I must have been using P2D each time, until today. Anyhow, I'm glad I found the shortcoming in my PR before someone else did.

Here's a real-world example with OpenStreetMap data employing the fix, adding a new drawing mode to the prettymaps library:

image

Thank you for all of your hard work over the years on Processing! I use it every day.