2dbi

Area of Triangle from Coordinates

viaLeetCode

Problem Given three points (x[i], y[i]) forming a triangle with at least one side parallel to an axis, compute its area (guaranteed integral).

Input / Output

  • Input: int arrays x[3], y[3].
  • Output: integer area.

Constraints

  • Coordinates up to 10^4 in magnitude; watch intermediate overflow if limits are larger.

Example

  • (0,0), (4,0), (0,3) → area 6.

Expected approach

  • Shoelace formula, which needs no axis-parallel assumption: area = |x0(y1−y2) + x1(y2−y0) + x2(y0−y1)| / 2. The axis-parallel guarantee just makes the area integral and allows the simpler base×height/2 if you spot the parallel side. Degenerate (collinear) points give 0 — mention it. O(1).
Add a follow-up question they asked
No follow-ups yet. Be the first to add one.
asked …
LeaderboardSalary
Language
Account