I have gone around and around with this problem for a while now.
Let's say there is a circle divided into eight sectors, with sector one being
from degrees zero to forty-five, sector two being from degrees forty-five to
ninety, and so on. Let's say a point is plotted somewhere randomly on the
circle. How would you determine which sector that point is in?
The reason I ask this question is because I have a perfectly round user interface
and I need to perform some action depending on which sector was clicked.
My thoughts so far are to somehow draw an imaginary line between the point clicked
and the origin of the circle, draw an imaginary line from the origin straight
up the screen, and get the angle between those two lines clockwise. If it's
between (i - 1) * 45 and i * 45 degrees, then it's in the ith sector. I would
not know how to program this without a formula; believe me, I tried.
I have gone around and around with this problem for a while now.
Let's say there is a circle divided into eight sectors, with sector one being
from degrees zero to forty-five, sector two being from degrees forty-five to
ninety, and so on. Let's say a point is plotted somewhere randomly on the
circle. How would you determine which sector that point is in?
The reason I ask this question is because I have a perfectly round user interface
and I need to perform some action depending on which sector was clicked.
My thoughts so far are to somehow draw an imaginary line between the point clicked
and the origin of the circle, draw an imaginary line from the origin straight
up the screen, and get the angle between those two lines clockwise. If it's
between (i - 1) * 45 and i * 45 degrees, then it's in the ith sector. I would
not know how to program this without a formula; believe me, I tried.
Reference:
depending on the language you are using you may be able to use an arctan2 function,
arctan2((clickx - centerx) , (clicky - centery)) would give you the angle of
the click relative to the center of the circle.
For a problem like this, I would first of all start with "standard" coordinate
system with origin at the centre of the circle and 0 degrees are on the positive
side of the x axis and increase counter-clockwise.
I assume that you have (or can calculate) the x,y coordinates of the plotted
point.
The general solution for n number of sectors would require you to calculate the
vector angle from the origin to the plotted point and then decide which 360/n
sector it resides on. One of the first google results here:
For your case though, the 8-section circle makes it much simpler. In the first
quarter (positive x and y), x y means your point is below the diagonal, x
y means your point is above the diagonal.
The same method can be applied to the other 3 quarters. First decide which quarter
you are in (positive or negative x & y) and then if you are above or below the
diagonal |x| |y|.
I have gone around and around with this problem for a while now.
Let's say there is a circle divided into eight sectors, with sector one being
from degrees zero to forty-five, sector two being from degrees forty-five to
ninety, and so on. Let's say a point is plotted somewhere randomly on the
circle. How would you determine which sector that point is in?
The reason I ask this question is because I have a perfectly round user interface
and I need to perform some action depending on which sector was clicked.
My thoughts so far are to somehow draw an imaginary line between the point clicked
and the origin of the circle, draw an imaginary line from the origin straight
up the screen, and get the angle between those two lines clockwise. If it's
between (i - 1) * 45 and i * 45 degrees, then it's in the ith sector. I would
not know how to program this without a formula; believe me, I tried.
Reference:
Assuming you have x and y coordinates with (0,0) being the center of your circle,
convert them to polar form (r,θ):
r = sqrt(x^2 + y^2)
θ = arctan(y/x) ... or atan(y/x) depending on the language.
All you really need though is θ, the angle. The arctan function will probably
return θ in radians so you would need to multiply by 180/π to convert the angle
to degrees. In essence:
Angle in degrees = (180*arctan(y/x))/3.14
Also note that on your diagram, an angle of zero degrees is the boundary between
sectors 2 and 3. So:
an angle between 0 and 45 means sector 2
an angle between 45 and 90 means sector 1
an angle between 90 and 135 means sector 8
I have gone around and around with this problem for a while now.
Let's say there is a circle divided into eight sectors, with sector one being
from degrees zero to forty-five, sector two being from degrees forty-five to
ninety, and so on. Let's say a point is plotted somewhere randomly on the
circle. How would you determine which sector that point is in?
The reason I ask this question is because I have a perfectly round user interface
and I need to perform some action depending on which sector was clicked.
My thoughts so far are to somehow draw an imaginary line between the point clicked
and the origin of the circle, draw an imaginary line from the origin straight
up the screen, and get the angle between those two lines clockwise. If it's
between (i - 1) * 45 and i * 45 degrees, then it's in the ith sector. I would
not know how to program this without a formula; believe me, I tried.
Reference:
Assuming you have x and y coordinates with (0,0) being the center of your circle,
convert them to polar form (r,θ):
r = sqrt(x^2 + y^2)
θ = arctan(y/x) ... or atan(y/x) depending on the language.
All you really need though is θ, the angle. The arctan function will probably
return θ in radians so you would need to multiply by 180/π to convert the angle
to degrees. In essence:
Angle in degrees = (180*arctan(y/x))/3.14
Also note that on your diagram, an angle of zero degrees is the boundary between
sectors 2 and 3. So:
an angle between 0 and 45 means sector 2
an angle between 45 and 90 means sector 1
an angle between 90 and 135 means sector 8
etc
Hope that helps!
An edit:
The arctan function is only defined for angles between -90 (270) and 90. I glossed
over that. So, as per your subject line, it is complicated!
If x is positive then
____If y is positive then
________Angle_in_degrees = (180*arctan(y/x))/3.14
____Else if y is negative
________Angle_in_degrees = 360 + (180*arctan(y/x))/3.14
____end if
Else if x is negative
____Angle_in_degrees = 180 + (180*arctan(y/x))/3.14
End if
If either x or y are zero (notice I specifically used "positive" and
"negative" above), it means the user has clicked right on a boundary,
this would require more if-then statements to accommodate. In this case (x=0
or y=0), I might just add a tiny value to the x or y coordinate to make the variable
non-zero and then send it through the above code.
The inverse trig functions aren't always fun to work with
(sorry for the underscores, BL doesn't maintain my formatting)
I have gone around and around with this problem for a while now.
Let's say there is a circle divided into eight sectors, with sector one being
from degrees zero to forty-five, sector two being from degrees forty-five to
ninety, and so on. Let's say a point is plotted somewhere randomly on the
circle. How would you determine which sector that point is in?
The reason I ask this question is because I have a perfectly round user interface
and I need to perform some action depending on which sector was clicked.
My thoughts so far are to somehow draw an imaginary line between the point clicked
and the origin of the circle, draw an imaginary line from the origin straight
up the screen, and get the angle between those two lines clockwise. If it's
between (i - 1) * 45 and i * 45 degrees, then it's in the ith sector. I would
not know how to program this without a formula; believe me, I tried.
Reference:
I think you should search online for radial menu code for whatever programming
language you are using. There are plenty of examples out there that have code
for radial menus. Why build your own when examples already exist?
I have gone around and around with this problem for a while now.
Let's say there is a circle divided into eight sectors, with sector one being
from degrees zero to forty-five, sector two being from degrees forty-five to
ninety, and so on. Let's say a point is plotted somewhere randomly on the
circle. How would you determine which sector that point is in?
The reason I ask this question is because I have a perfectly round user interface
and I need to perform some action depending on which sector was clicked.
My thoughts so far are to somehow draw an imaginary line between the point clicked
and the origin of the circle, draw an imaginary line from the origin straight
up the screen, and get the angle between those two lines clockwise. If it's
between (i - 1) * 45 and i * 45 degrees, then it's in the ith sector. I would
not know how to program this without a formula; believe me, I tried.