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?


2 Answers 2


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;
if (x*x + y*y > radius*radius) {
    // outside the grey circle
  • \$\begingroup\$ There, I've added a common optimization, do I deserve an upvote yet? \$\endgroup\$
    – MickLH
    Mar 11, 2016 at 3:50
  • \$\begingroup\$ I did upvote, it just says it doesnt show since Im new. I did accept it though. \$\endgroup\$
    – 1ncline
    Mar 11, 2016 at 3:55
  • \$\begingroup\$ lol my bad, didn't even check \$\endgroup\$
    – MickLH
    Mar 11, 2016 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.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .