@@ -2822,22 +2822,48 @@ public boolean isClosed() {
2822
2822
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2823
2823
2824
2824
2825
- // http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
2825
+ /**
2826
+ * Return true if this x, y coordinate is part of this shape. Only works
2827
+ * with PATH shapes or GROUP shapes that contain other GROUPs or PATHs.
2828
+ */
2826
2829
public boolean contains (float x , float y ) {
2827
2830
if (family == PATH ) {
2831
+ PVector p = new PVector (x , y );
2832
+ if (matrix != null ) {
2833
+ // apply the inverse transformation matrix to the point coordinates
2834
+ PMatrix inverseCoords = matrix .get ();
2835
+ // TODO why is this called twice? [fry 190724]
2836
+ // commit was https://github.com/processing/processing/commit/027fc7a4f8e8d0a435366eae754304eea282512a
2837
+ inverseCoords .invert (); // maybe cache this?
2838
+ inverseCoords .invert (); // maybe cache this?
2839
+ inverseCoords .mult (new PVector (x , y ), p );
2840
+ }
2841
+
2842
+ // http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
2828
2843
boolean c = false ;
2829
2844
for (int i = 0 , j = vertexCount -1 ; i < vertexCount ; j = i ++) {
2830
- if (((vertices [i ][Y ] > y ) != (vertices [j ][Y ] > y )) &&
2831
- (x <
2845
+ if (((vertices [i ][Y ] > p . y ) != (vertices [j ][Y ] > p . y )) &&
2846
+ (p . x <
2832
2847
(vertices [j ][X ]-vertices [i ][X ]) *
2833
- (y -vertices [i ][Y ]) /
2834
- (vertices [j ][1 ]-vertices [i ][Y ]) +
2835
- vertices [i ][X ])) {
2848
+ (y -vertices [i ][Y ]) /
2849
+ (vertices [j ][1 ]-vertices [i ][Y ]) +
2850
+ vertices [i ][X ])) {
2836
2851
c = !c ;
2837
2852
}
2838
2853
}
2839
2854
return c ;
2855
+
2856
+ } else if (family == GROUP ) {
2857
+ // If this is a group, loop through children until we find one that
2858
+ // contains the supplied coordinates. If a child does not support
2859
+ // contains() throw a warning and continue.
2860
+ for (int i = 0 ; i < childCount ; i ++) {
2861
+ if (children [i ].contains (x , y )) return true ;
2862
+ }
2863
+ return false ;
2864
+
2840
2865
} else {
2866
+ // https://github.com/processing/processing/issues/1280
2841
2867
throw new IllegalArgumentException ("The contains() method is only implemented for paths." );
2842
2868
}
2843
2869
}
0 commit comments