# How to enforce touching within a ring shape?

I'd like to have the player moving the finger within the shaded ring:

The game should be able to detect "out-of-boundary" events once the finger moves outside the ring. How can I accomplish this?

• Sounds like you just need a distance check from the touched position to the center point each time the touch moves, checking that the distance is between the minimum (inside of ring) and maximum (outside of ring). What have you tried so far? – DMGregory Jan 17 '16 at 14:07

Presumably you have the center point of the ring (c), and the radius of the inner and outer segments of the valid portion of the ring (ri and ro, respectively)

Then, given any touch point p, you simply need to perform two point-in-circle tests. The touch is valid (within the ring) if it is both outside the inner circle and inside the outer circle. A point p is within a circle of radius r with a center at c if it satisfies the inequality:

(p.x - c.x)^2 + (p.y - c.y)^2 < r^2


So you need

BOOL insideInner = pow((p.x - c.x), 2) + pow(p.y - c.y, 2) < (ri * ri);
BOOL insideOuter = pow((p.x - c.x), 2) + pow(p.y - c.y, 2) < (ro * ro);
BOOL isValid = !insideInner && insiderOuter;


to determine if the current touch point is valid (`isValid). You get updated touch positions every touch or drag event, which you can feed to the computation and determine if the touch ever leaves the valid area.

Note that you can factor out a bunch of that math to avoid repeated computation.