0

I have a pandas dataframe with 3 columns, PointsA["x", "y", "z"] and PointsB["x", "y", "z"].

I can generate the convex hull ConvexHull(PointsA) Note I'm assuming this only uses the x and y since convex hull in a 2D function.

I want to calculate if each point in PointsB is inside or outside the convex hull of PointsA.

If a point in PointsB is in the convex hull of pointsA then the value True is set to a new column in PointsB["in_A"], and if not, then False.

So far I have the following:

PointsA = pointcloudA[['x', 'y', 'z']]
PointsB = pointcloudB[['x', 'y', 'z']]

Convexhull_of_A = ConvexHull(PointsA)

# which of PointsB are in the `Convexhull_of_A`, assign PointsB['in_A'] == True
# which of PointsB are not in the `Convexhull_of_A`, assign PointsB['in_A'] == False
Spatial Digger
  • 1,883
  • 1
  • 19
  • 37
  • Let H = h1, h2, ..., hN be the convex hull, traversed counterclockwise. Next, consider the line segment h1->h2 and the point p from points B. If the cross product of h2-h1 and p-h1 are positive, then p lies on the left side of h1->h2. Do this same check for all hi->hi+1, and iff all sides say "p" is "on the left" then "p" is inside. – Mark Lavin Feb 15 '22 at 14:49

1 Answers1

1

Shapely Library might be what you are looking for, you can consider the output of your ConvexHull as a polygon and check with Shapely if that polygon contains the point as in the below example.

from shapely.geometry.polygon import Polygon

polygonA = Polygon(Convexhull_of_A.points)
# which of PointsB are in the `Convexhull_of_A`
[polygonA.contains(PointB) for PointB in PointsB] 

Variable names should be lowercase with words separated by underscores.

Look below for answer regarding polygon.

What's the fastest way of checking if a point is inside a polygon in python

Yonas Kassa
  • 3,362
  • 1
  • 18
  • 27