9.12 · Rectangle containment and moving a point between coordinate spaces

The small geometry exercises that interviews use: does one rectangle fit inside another, and is this touch inside that button once you account for the view’s position and scrolling?

Mental model. A point or rectangle only has meaning together with the coordinate space it is measured in; convert first, then compare.

Containment and spaces

Normalise rectangles, then convert between views.

  1. Write a function that checks whether one CGRect fully contains another, then explain how you decide whether a touch lies inside a view that sits inside a scrolled parent. 9.12.1 · L3
    Containment is four comparisons: the inner rectangle’s minX and minY must be at least the outer’s, and its maxX and maxY at most the outer’s. Two details make…

More in Layout, rendering & animation