Geometry Puzzle: Points Inside vs Outside a Triangle

viaGlassdoor

Problem: Given 5 points and a triangle formed by any 3 of them, determine whether the remaining 2 points lie inside or outside that triangle.

Constraints: Points given as (x, y) coordinates; need a robust geometric test (no floating point round-off issues at boundaries).

Example: Triangle formed by points A, B, C; determine for points D and E whether each lies inside the triangle ABC.

Approach: Use the sign of cross products (orientation test): for a point P and triangle ABC, compute the cross product of (B-A, P-A), (C-B, P-B), and (A-C, P-C). If all three cross products have the same sign (all positive or all negative), P lies inside the triangle; a zero cross product means P is exactly on an edge. This avoids floating-point area-based methods and works with integer coordinates.

Add a follow-up question they asked
No follow-ups yet. Be the first to add one.
asked …
LeaderboardSalary
Language
Account