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.
(float x, float y)
| 2962 | * with PATH shapes or GROUP shapes that contain other GROUPs or PATHs. |
| 2963 | */ |
| 2964 | public boolean contains(float x, float y) { |
| 2965 | if (family == PATH) { |
| 2966 | PVector p = new PVector(x, y); |
| 2967 | if (matrix != null) { |
| 2968 | // apply the inverse transformation matrix to the point coordinates |
| 2969 | PMatrix inverseCoords = matrix.get(); |
| 2970 | // TODO why is this called twice? [fry 190724] |
| 2971 | // commit was https://github.com/processing/processing/commit/027fc7a4f8e8d0a435366eae754304eea282512a |
| 2972 | inverseCoords.invert(); // maybe cache this? |
| 2973 | inverseCoords.invert(); // maybe cache this? |
| 2974 | inverseCoords.mult(new PVector(x, y), p); |
| 2975 | } |
| 2976 | |
| 2977 | // http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html |
| 2978 | boolean c = false; |
| 2979 | for (int i = 0, j = vertexCount-1; i < vertexCount; j = i++) { |
| 2980 | if (((vertices[i][Y] > p.y) != (vertices[j][Y] > p.y)) && |
| 2981 | (p.x < |
| 2982 | (vertices[j][X]-vertices[i][X]) * |
| 2983 | (y-vertices[i][Y]) / |
| 2984 | (vertices[j][1]-vertices[i][Y]) + |
| 2985 | vertices[i][X])) { |
| 2986 | c = !c; |
| 2987 | } |
| 2988 | } |
| 2989 | return c; |
| 2990 | |
| 2991 | } else if (family == GROUP) { |
| 2992 | // If this is a group, loop through children until we find one that |
| 2993 | // contains the supplied coordinates. If a child does not support |
| 2994 | // contains() throw a warning and continue. |
| 2995 | for (int i = 0; i < childCount; i++) { |
| 2996 | if (children[i].contains(x, y)) return true; |
| 2997 | } |
| 2998 | return false; |
| 2999 | |
| 3000 | } else { |
| 3001 | // https://github.com/processing/processing/issues/1280 |
| 3002 | throw new IllegalArgumentException("The contains() method is only implemented for paths."); |
| 3003 | } |
| 3004 | } |
| 3005 | |
| 3006 | |
| 3007 | // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
no test coverage detected