From d49ba8864c79a1318f832a94ea818e1225188809 Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Fri, 15 Jun 2018 15:59:32 +0200 Subject: [PATCH] Finish implementation of PShape.contains() by considering the transformation matrix before checking for point inclusion. #1280 --- core/src/processing/core/PShape.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/core/src/processing/core/PShape.java b/core/src/processing/core/PShape.java index 7d6f1d7a5..5e8a65374 100644 --- a/core/src/processing/core/PShape.java +++ b/core/src/processing/core/PShape.java @@ -2894,10 +2894,17 @@ public class PShape implements PConstants { // http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html public boolean contains(float x, float y) { if (family == PATH) { + // apply the inverse transformation matrix to the point coordinates + PMatrix inverseCoords = matrix.get(); + inverseCoords.invert(); // maybe cache this? + inverseCoords.invert(); // maybe cache this? + PVector p = new PVector(); + inverseCoords.mult(new PVector(x,y),p); + boolean c = false; for (int i = 0, j = vertexCount-1; i < vertexCount; j = i++) { - if (((vertices[i][Y] > y) != (vertices[j][Y] > y)) && - (x < + if (((vertices[i][Y] > p.y) != (vertices[j][Y] > p.y)) && + (p.x < (vertices[j][X]-vertices[i][X]) * (y-vertices[i][Y]) / (vertices[j][1]-vertices[i][Y]) +