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. This method is not imperfect and doesn't account for all cases (not all complex shapes: concave shapes or holes may have issues).
(float x, float y)
| 3066 | * (not all complex shapes: concave shapes or holes may have issues). |
| 3067 | */ |
| 3068 | public boolean contains(float x, float y) { |
| 3069 | if (family == PATH || family == GEOMETRY) { |
| 3070 | PVector p = new PVector(x, y); |
| 3071 | if (matrix != null) { |
| 3072 | // apply the inverse transformation matrix to the point coordinates |
| 3073 | PMatrix inverseCoords = matrix.get(); |
| 3074 | // TODO why is this called twice? [fry 190724] |
| 3075 | // commit was https://github.com/processing/processing/commit/027fc7a4f8e8d0a435366eae754304eea282512a |
| 3076 | inverseCoords.invert(); // maybe cache this? |
| 3077 | inverseCoords.invert(); // maybe cache this? |
| 3078 | inverseCoords.mult(new PVector(x, y), p); |
| 3079 | } |
| 3080 | |
| 3081 | // http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html |
| 3082 | boolean c = false; |
| 3083 | for (int i = 0, j = vertexCount-1; i < vertexCount; j = i++) { |
| 3084 | if (((vertices[i][Y] > p.y) != (vertices[j][Y] > p.y)) && |
| 3085 | (p.x < |
| 3086 | (vertices[j][X]-vertices[i][X]) * |
| 3087 | (y-vertices[i][Y]) / |
| 3088 | (vertices[j][1]-vertices[i][Y]) + |
| 3089 | vertices[i][X])) { |
| 3090 | c = !c; |
| 3091 | } |
| 3092 | } |
| 3093 | return c; |
| 3094 | |
| 3095 | } else if (family == GROUP) { |
| 3096 | // If this is a group, loop through children until we find one that |
| 3097 | // contains the supplied coordinates. If a child does not support |
| 3098 | // contains() throw a warning and continue. |
| 3099 | for (int i = 0; i < childCount; i++) { |
| 3100 | if (children[i].contains(x, y)) return true; |
| 3101 | } |
| 3102 | return false; |
| 3103 | |
| 3104 | } else { |
| 3105 | // https://github.com/processing/processing/issues/1280 |
| 3106 | throw new IllegalArgumentException("The contains() method is only implemented for paths."); |
| 3107 | } |
| 3108 | } |
| 3109 | |
| 3110 | |
| 3111 | // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
no test coverage detected