# Detecting position within a circle

I need to detect the position of a knob on a touchpad. I need to detect if it is in particular areas and if so, which one. The sections, of course, being the colored parts.

I know that the black center circle ends at 0.8f and -0.8f for both the X and Y axis.

The information I can get is the percent X and percent Y which is 0 to 1 from center or 0 to -1 from center the other way.

How would I go about doing this?

This is exactly what the arc tangent function of 2 arguments (Math.atan2) does.

double angle = Math.atan2(y, x);


Also, a common trick used to cheaply test the radius is:

double radius = 0.8;
// outside the grey circle
}

• There, I've added a common optimization, do I deserve an upvote yet? Mar 11 '16 at 3:50
• I did upvote, it just says it doesnt show since Im new. I did accept it though. Mar 11 '16 at 3:55
• lol my bad, didn't even check Mar 11 '16 at 4:05

You should have a look at polar coordinates.

The dark central circle is the set of points

C = { (r, phi) | 0 <= r <= 0.8, 0 <= phi < 2 pi }


The other coloured parts are the sets

C_i = { (r, phi) | 0.8 <= r <= 1, phi_i <= phi < phi_(i+1) }

where i in {0,..,5}, phi_6 = phi_0 and you need to measure the angles phi_i from your diagram.

For the conversion between polar and Cartesian coordinates use the formulas from the linked Wikipedia article.