I have a circle drawn, and I want to make it so I can have more slices than four. I can easily do four quadrants because I just check if the mouse in in the circle and inside a box.
This is how I am checking if the point is in the circle.
if( Math.sqrt((xx-x)*(xx-x) + (yy-y)*(yy-y)) <= radius)
{
return true;
}
else
{
return false;
}
How can I modify this if the circle is divided into more than 4 regions?
It is more a trig problem Try something like this.
For radial slices (circular sectors), you have a couple of alternatives:
Math.atan2
to calculate the 4-quadrant angle of the line from the circle center to the point. Compare to the slice angles to determine the slice index.Math.atan2
.The following sample code calculates the slice index for a particular point:
The above code makes the following assumptions:
You can adjust the calculations if these assumptions do not apply. (For instance, you can subtract the start angle from
angle
to eliminate assumption 3.)First we can check that the point is within the circle as you did. But I woudln't combine this with a check for which quadrant (is that why you have radius/2 ?)
Now we can look to see which region the point is in by using the atan2 function.
atan2
is like Arctan except the Arctangent function always returns a value between -pi/2 and pi/2 (-90 and +90 degrees). We need the actual angle in polar coordinate fashion. Now assuming that (x,y) is the center of your circle and we are interested in the location of the point (xx,yy) we have