Find if a point is inside or outside of a triangle
Triangle coordinates
Triangle lines equations
Test point
a
_{x}
a
_{y}
y =
x +
p
_{x}
p
_{y}
b
_{x}
b
_{y}
y =
x +
c
_{x}
c
_{y}
y =
x +
Point location:
Find if a point is inside or outside of a circle
Circle equation:
( x -
)
^{2}
+ ( y -
)
^{2}
=
x
^{2}
+ y
^{2}
+
x +
y +
= 0
Test point (x ,y):
(
,
)
Point location:
Determine if a point is inside or outside of a triangle whose vertices are the points
(x
_{1}
, y
_{1}
), (x
_{2}
, y
_{2}
) and (x
_{3}
, y
_{3}
).
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:
k
_{1}
= (p
_{x}
⎯ x
_{1}
)•(y
_{2}
⎯ y
_{1}
) ⎯ (p
_{y}
⎯ y
_{1}
)•(x
_{2}
⎯ x
_{1}
)
k
_{2}
= (p
_{x}
⎯ x
_{2}
)•(y
_{3}
⎯ y
_{2}
) ⎯ (p
_{y}
⎯ y
_{2}
)•(x
_{3}
⎯ x
_{2}
)
k
_{3}
= (p
_{x}
⎯ x
_{3}
)•(y
_{1}
⎯ y
_{3}
) ⎯ (p
_{y}
⎯ y
_{3}
)•(x
_{1}
⎯ x
_{3}
)
If all three equations are either positive or negative then the point
(p
_{x}
, p
_{y}
)
is inside the triangle.
If triangle is given by the
lines equations
:
y = m
_{1}
x + b
_{1}
y = m
_{2}
x + b
_{2}
y = m
_{3}
x + b
_{3}
Find intersection points of the sides of the triangle.
After finding the intersection points use the vector method.
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 k
_{1}
, k
_{2}
and k
_{3}
.
k
_{1}
= (1 ⎯ 3)•(5 ⧾ 2) ⎯ (3 ⧾ 2)•(1 ⎯ 3) = ⎯ 4
k
_{2}
= (1 ⎯ 1)•(2 ⎯ 5) ⎯ (3 ⎯ 5)•(⎯ 3 ⎯ 1) = ⎯ 8
k
_{3}
= (1 ⧾ 3)•(⎯ 2 ⎯ 2) ⎯ (3 ⎯ 2)•(3 ⧾ 3) = ⎯ 22
Because the signs of k
_{1}
, k
_{2}
and k
_{3}
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.
k
_{1}
= (p
_{x}
⎯ x
_{1}
)•(y
_{2}
⎯ y
_{1}
) ⎯ (p
_{y}
⎯ y
_{1}
)•(x
_{2}
⎯ x
_{1}
)
···
k
_{n-1}
= (p
_{x}
⎯ x
_{n-1}
)•(y
_{n}
⎯ y
_{n-1}
) ⎯ (p
_{y}
⎯ y
_{n-1}
)•(x
_{n}
⎯ x
_{n-1}
)
···
k
_{n}
= (p
_{x}
⎯ x
_{n}
)•(y
_{1}
⎯ y
_{n}
) ⎯ (p
_{y}
⎯ y
_{n}
)•(x
_{1}
⎯ x
_{n}
)
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.
How to determine if a point (p
_{x}
, p
_{y}
) is inside or outside of a circle given by the form
(x ⎯ a)
^{2}
⧾ (y ⎯ b)
^{2}
= r
^{2}
The center of the circle is at point: (a, b)
The radius of the circle is: r
Find the distance of point (p
_{x}
, p
_{y}
) from the center of the circle by
the equation:
If the distance is less then the radius then the point is inside the circle.
If the circle equation is of the form
x
^{2}
⧾ y
^{2}
⧾ Ax ⧾ By ⧾ C = 0
The center of the circle is at point:
The radius of the circle is:
Find the distance of point (p
_{x}
, p
_{y}
) from the center of the circle
by the equation:
If the distance is less then the radius then the point is inside the circle.