Determine if a point is inside or outside of a triangle whose vertices are the points
(x1, y1), (x2, y2) and (x3, y3).
|
By vectors analysis - if the cross product of the vectors:
are all positive or all negative, the point is inside the triangle.
The cross product of all three pairs of the triangle sides are:
After solving the determinants we get the values:
k1 = (px ⎯ x1)•(y2 ⎯ y1) ⎯ (py ⎯ y1)•(x2 ⎯ x1)
k2 = (px ⎯ x2)•(y3 ⎯ y2) ⎯ (py ⎯ y2)•(x3 ⎯ x2)
k3 = (px ⎯ x3)•(y1 ⎯ y3) ⎯ (py ⎯ y3)•(x1 ⎯ x3)
If all three equations are either positive or negative then the point (px, py) is inside the triangle.
|
Example: given a triangle whose vertices are at (3, ⎯ 2), (1, 5) and (⎯ 3, 2) determine if
the point (1, 3) is inside or outside of the triangle.
|
Find k1, k2 and k3.
k1 = (1 ⎯ 3)•(5 ⧾ 2) ⎯ (3 ⧾ 2)•(1 ⎯ 3) = ⎯ 4
k2 = (1 ⎯ 1)•(2 ⎯ 5) ⎯ (3 ⎯ 5)•(⎯ 3 ⎯ 1) = ⎯ 8
k3 = (1 ⧾ 3)•(⎯ 2 ⎯ 2) ⎯ (3 ⎯ 2)•(3 ⧾ 3) = ⎯ 22
Because the signs of k1, k2 and k3 are all negative the point is inside the given triangle.
|
The vector analysis can be applied to any convex polygon that has n sides to determine if the point p is inside the polygon.
|
k1 = (px ⎯ x1)•(y2 ⎯ y1) ⎯ (py ⎯ y1)•(x2 ⎯ x1)
···
kn-1 = (px ⎯ xn-1)•(yn ⎯ yn-1) ⎯ (py ⎯ yn-1)•(xn ⎯ xn-1)
···
kn = (px ⎯ xn)•(y1 ⎯ yn) ⎯ (py ⎯ yn)•(x1 ⎯ xn)
This calculation should apply to each side of the polygon from 1 to n. If the signs of all k's are the same
positive or negative then the point p is inside the polygon.
Note: this method applies only to convex polygons.
|