#P1355. Point and Triangle Position
Point and Triangle Position
Point and Triangle Position
Given the coordinates of three vertices of a triangle and a point, determine the position of the point relative to the triangle. Specifically, output INSIDE
if the point lies strictly within the triangle, BORDER
if it lies exactly on one of the triangle's sides (including vertices), and OUTSIDE
otherwise.
The problem can be solved using area comparisons. Let the triangle be defined by vertices \(A(x_1,y_1)\), \(B(x_2,y_2)\), and \(C(x_3,y_3)\). Compute the area (multiplied by 2 for integer arithmetic) of triangle ABC: \[ \text{area}_{ABC} = |x_1(y_2-y_3) + x_2(y_3-y_1) + x_3(y_1-y_2)| \] Then, compute the areas of triangles PAB, PBC, and PCA for the given point \(P(x_p,y_p)\). If the sum of these three areas equals \(\text{area}_{ABC}\), the point is either inside or on the boundary of the triangle. Furthermore, if any of the sub-areas equals zero, then the point lies on a side (or vertex) of the triangle.
Note: It is guaranteed that the given triangle is non-degenerate.
inputFormat
The input consists of 8 space-separated integers:
- x1 y1 : Coordinates of the first vertex.
- x2 y2 : Coordinates of the second vertex.
- x3 y3 : Coordinates of the third vertex.
- xp yp : Coordinates of the query point.
outputFormat
Output a single string: INSIDE
, BORDER
, or OUTSIDE
based on the position of the point relative to the triangle.
sample
0 0 5 0 0 5 1 1
INSIDE