Reputation: 4946
I have the (x,y)
-coordinates of all three corners of a 2D-triangle. Now I want to check if a point with (xp,yp)
is inside this triangle:
I know two methods (only theoretically, not yet implemented) to check:
with vectors:
vec_0A + vec_AB*lambda + vec_AC*my = vec_0P
lambda + my =< 1
with equations of lines:
calculate the three linear equations of AB, AC, BC and check for each equation if P is left/right of it.
Problem 1:
It must be accurate, because typical (x,y)
-values of my corners and points look like this:
(-0.049721957725789148, 0.024809768773549616)
-> 18 positions after decimal point
Problem 2: It should has a good performance, because I want to check if P is inside triangle(ABC) OR inside triangle(DEF) OR inside triangle(GHI) OR inside triangle(JKL) OR outside of all of them. And I have to do it with ~ 10,000 points.
I read somewhere that the vector-way isn't that accurate. True? Do you know some other ways of checking? Which way of checking do you recommend?
Upvotes: 3
Views: 222
Reputation: 8108
With problems like this always look for libraries... it is a mathematical problem and probably has a solution in a library. One quick answer:
import matplotlib
matplotlib.path.Path.contains_points # is the function you are looking for
check the docstring for usage instructions.
Upvotes: 3