How it works
- 1 Scatter points
- 2 Test every pair
- 3 Count points on the line
- 4 Draw each collinear set
How it works
Three points are collinear when the area of the triangle they form is zero — equivalently, when the cross product of two edges is zero. The demo checks every pair of points, collects every other point that lies on their line, and keeps the sets of three or more.
Building it, step by step
1. Scatter points
Points are placed in a plane with integer-style coordinates, like the classic assignment.
2. Check every pair
For each pair, the cross product with every other point tells whether it lies on the same line.
3. Report collinear sets
Sets of three or more collinear points are drawn as a line through the extremes, with the points highlighted.
What I learned
The naive approach is O(n^3) — checking every triple. Sorting points by slope from each anchor
brings it down, but the brute-force version is a clear reminder that the most expensive step in
geometry code is usually testing combinations.