diff --git a/core/src/processing/core/PShape.java b/core/src/processing/core/PShape.java index 1e3390798..60b4521f8 100644 --- a/core/src/processing/core/PShape.java +++ b/core/src/processing/core/PShape.java @@ -2040,7 +2040,7 @@ public class PShape implements PConstants { parent.addName(nom, shape); } else { if (nameTable == null) { - nameTable = new HashMap(); + nameTable = new HashMap<>(); } nameTable.put(nom, shape); } @@ -2387,14 +2387,14 @@ public class PShape implements PConstants { /** * ( begin auto-generated from PShape_setFill.xml ) * - * The setFill() method defines the fill color of a PShape. - * This method is used after shapes are created or when a shape is defined explicitly - * (e.g. createShape(RECT, 20, 20, 80, 80)) as shown in the above example. - * When a shape is created with beginShape() and endShape(), its - * attributes may be changed with fill() and stroke() within - * beginShape() and endShape(). However, after the shape is - * created, only the setFill() method can define a new fill value for - * the PShape. + * The setFill() method defines the fill color of a PShape. + * This method is used after shapes are created or when a shape is defined explicitly + * (e.g. createShape(RECT, 20, 20, 80, 80)) as shown in the above example. + * When a shape is created with beginShape() and endShape(), its + * attributes may be changed with fill() and stroke() within + * beginShape() and endShape(). However, after the shape is + * created, only the setFill() method can define a new fill value for + * the PShape. * * ( end auto-generated ) * @@ -2543,14 +2543,14 @@ public class PShape implements PConstants { /** * ( begin auto-generated from PShape_setStroke.xml ) * - * The setStroke() method defines the outline color of a PShape. - * This method is used after shapes are created or when a shape is defined - * explicitly (e.g. createShape(RECT, 20, 20, 80, 80)) as shown in - * the above example. When a shape is created with beginShape() and - * endShape(), its attributes may be changed with fill() and - * stroke() within beginShape() and endShape(). - * However, after the shape is created, only the setStroke() method - * can define a new stroke value for the PShape. + * The setStroke() method defines the outline color of a PShape. + * This method is used after shapes are created or when a shape is defined + * explicitly (e.g. createShape(RECT, 20, 20, 80, 80)) as shown in + * the above example. When a shape is created with beginShape() and + * endShape(), its attributes may be changed with fill() and + * stroke() within beginShape() and endShape(). + * However, after the shape is created, only the setStroke() method + * can define a new stroke value for the PShape. * * ( end auto-generated ) * @@ -2891,7 +2891,10 @@ public class PShape implements PConstants { // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . - // http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html + /** + * Return true if this x, y coordinate is part of this shape. Only works + * with PATH shapes or GROUP shapes that contain other GROUPs or PATHs. + */ public boolean contains(float x, float y) { if (family == PATH) { // apply the inverse transformation matrix to the point coordinates @@ -2900,7 +2903,8 @@ public class PShape implements PConstants { inverseCoords.invert(); // maybe cache this? PVector p = new PVector(); inverseCoords.mult(new PVector(x,y),p); - + + // http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html boolean c = false; for (int i = 0, j = vertexCount-1; i < vertexCount; j = i++) { if (((vertices[i][Y] > p.y) != (vertices[j][Y] > p.y)) && @@ -2913,19 +2917,18 @@ public class PShape implements PConstants { } } return c; + } else if (family == GROUP) { - // If this is a group, loop through children until we find one that - // contains the supplied coordinates. If a child does not support contains() - // just throw a warning and continue. + // If this is a group, loop through children until we find one that + // contains the supplied coordinates. If a child does not support + // contains() throw a warning and continue. for (int i = 0; i < childCount; i++) { - try { - if (children[i].contains(x, y)) return true; - } catch (IllegalArgumentException e) { - PGraphics.showWarning(e); - } + if (children[i].contains(x, y)) return true; } return false; + } else { + // https://github.com/processing/processing/issues/1280 throw new IllegalArgumentException("The contains() method is only implemented for paths."); } } diff --git a/core/todo.txt b/core/todo.txt index 35d27af37..04686fd82 100644 --- a/core/todo.txt +++ b/core/todo.txt @@ -31,6 +31,11 @@ X https://github.com/processing/processing/pull/5475 contrib X Fixed a crash occuring while loading certain SVGs exported from Illustrator X https://github.com/processing/processing/pull/5526 +X Support PShape.contains() on GROUP objects +X https://github.com/processing/processing/pull/5550 +X Improve implementation of PShape.contains() to take the CTM into account +X https://github.com/processing/processing/pull/5549 + 3.4 _ add circle() and square()