Skip to content
Shubham JangleResources
React

Explore topics

Overlapping Circles

Watch the walkthrough
Open on Instagram

Exercise: Draw and count overlapping circles

Click inside a drawing area to create an equal-sized circle centered at the pointer. Display how many earlier circles overlap the new one. For a diameter of 100 px, two circles overlap when their center distance is less than 100 px; decide explicitly whether touching edges count.

Requirements

  • Store centers and stable circle IDs in state; render from that data.
  • Convert pointer coordinates to drawing-area coordinates using its bounding rectangle.
  • Calculate overlap from center distance and both radii; avoid a square bounding-box approximation.
  • Provide Reset and keep controls from accidentally creating circles.

Acceptance checks and discussion

Check identical centers, clearly separated centers, and exactly touching edges. Keep scrolling or repositioning the drawing area from shifting new circles. Discuss the O(n) check per insertion and O(n²) total work for n circles.

The brief above is an original practice specification. The linked sandbox is supporting solution material and may implement a simpler subset of these requirements.

Supporting material