Problem · Geometry

Triangle and Points

Learn this problem
EasyGRGreyOrangeOA

Problem statement

You are given the coordinates of a triangle with three vertices:

  • (x1, y1)
  • (x2, y2)
  • (x3, y3)

You are also given two points:

  • Point p = (xp, yp)
  • Point q = (xq, yq)

Return the following code:

  1. Return 1 if the three vertices cannot form a triangle.
  2. Return 2 if both p and q lie inside the triangle.
  3. Return 3 if only p lies inside the triangle.
  4. Return 4 if only q lies inside the triangle.
  5. Return 5 if neither point lies inside the triangle.

Boundary rule: A point on an edge or exactly on a vertex is considered inside the triangle.

Input: The triangle coordinates x1, y1, x2, y2, x3, y3, followed by the point coordinates xp, yp, xq, yq.

Function

triangleAndPoints(x1: int, y1: int, x2: int, y2: int, x3: int, y3: int, xp: int, yp: int, xq: int, yq: int) → int

Examples

Example 1

x1 = 0y1 = 0x2 = 5y2 = 0x3 = 0y3 = 5xp = 1yp = 1xq = 6yq = 6return = 3

Only point p lies inside the triangle.

More GreyOrange problems

drafts saved locally
public int triangleAndPoints(int x1, int y1, int x2, int y2, int x3, int y3, int xp, int yp, int xq, int yq) {
  // write your code here
}
x10
y10
x25
y20
x30
y35
xp1
yp1
xq6
yq6
expected3
checking account