Skip to main content
Back to projects

Collinear Points

Computational geometry

Fundamentals Interactive
Python geometry
Interactive demo finds collinear points in your browser

Click to scatter points, then ask the demo to find every set of three or more points that lie on the same straight line. Each line is drawn through its extremes and its points are highlighted.

Click in the plot to add points, then press "Find collinear lines".

How it works

  1. 1 Scatter points
  2. 2 Test every pair
  3. 3 Count points on the line
  4. 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.