Problem · Geometry
Do They Belong?
Learn this problemProblem statement
A triangle formed by the three points a(x1, y1), b(x2, y2) and c(x3, y3) is a non-degenerate triangle if the following rules are respected (|ab| is the length of the line between points a and b):
- |ab| + |bc| > |ac|
- |bc| + |ac| > |ab|
- |ab| + |ac| > |bc|
A point belongs to a triangle if it lies somewhere on or inside the triangle. Given two points p = (xp, yp) and q = (xq, yq), return the correct scenario number:
- 0: If the triangle abc does not form a valid non-degenerate triangle.
- 1: If point p belongs to the triangle but point q does not.
- 2: If point q belongs to the triangle but point p does not.
- 3: If both points p and q belong to the triangle.
- 4: If neither point p nor point q belong to the triangle.
Function
pointsBelong(x1: int, y1: int, x2: int, y2: int, x3: int, y3: int, xp: int, yp: int, xq: int, yq: int) → int
Complete the function pointsBelong in the editor below.
pointsBelong has the following parameter(s):
int x1, y1, x2, y2, x3, y3: integer coordinates of the three points that may create a valid triangleint xp, yp, xq, yq: integer coordinates of the two points p and q
Returns
int: an integer value that represents the scenario
Examples
Example 1
x1 = 0y1 = 0x2 = 2y2 = 0x3 = 4y3 = 0xp = 2yp = 0xq = 4yq = 0return = 0
The lines do not form a valid non-degenerate triangle: The three points a, b, c lie on the same line, so it is impossible to form a triangle. The answer is 0.
Constraints
0 ≤ x1, y1, x2, y2, x3, y3, xp, yp, xq, yq ≤ 2000