math - Java - How to check of a point is inside a sliceof a circle -
i have circle drawn, , want make can have more slices four. can 4 quadrants because check if mouse in in circle , inside box.
this how checking if point in circle.
if( math.sqrt((xx-x)*(xx-x) + (yy-y)*(yy-y)) <= radius) { return true; } else { return false; } how can modify if circle divided more 4 regions?
first can check point within circle did. woudln't combine check quadrant (is why have radius/2 ?)
if( (xx-x)*(xx-x) + (yy-y)*(yy-y) > radius*radius) return false; now can see region point in using atan2 function. atan2 arctan except arctangent function returns value between -pi/2 , pi/2 (-90 , +90 degrees). need actual angle in polar coordinate fashion. assuming (x,y) center of circle , interested in location of point (xx,yy) have
double theta = math.atan2(yy-y,xx-x); //theta in range -math.pi math.pi if(theta<0) theta = math.pi - theta; //now theta in range [0, 2*pi] //use value determine slice of circle point resides in. //for example: int numslices = 8; int whichslice = 0; double slicesize = math.pi*2 / numslices; double slicestart; for(int i=1; i<=numslices; i++) { slicestart = i*slicesize; if(theta < slicestart) { whichslice = i; break; } } //whichslice should number 1 8 representing part of circle // point in, slices numbered 1 numslices starting // right middle (positive x-axis if center (0,0).
Comments
Post a Comment