Java - How to check of a point is inside a sliceof

2019-04-17 11:21发布

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?

3条回答
\"骚年 ilove
2楼-- · 2019-04-17 11:41

It is more a trig problem Try something like this.

 int numberOfSlices=8;

   double angleInDegrees=(Math.toDegrees(Math.atan2(xx-x ,yy-y)));

   long slice= Math.round(( numberOfSlices*angleInDegrees )/360 );
查看更多
够拽才男人
3楼-- · 2019-04-17 11:42

For radial slices (circular sectors), you have a couple of alternatives:

  1. Use 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.
  2. For a particular slice, you can test which side of each slice edge the point falls. Classify the point accordingly. This is more complicated to calculate but probably faster (for a single slice) than calling Math.atan2.

The following sample code calculates the slice index for a particular point:

int sliceIndex(double xx, double yy, double x, double y, int nSlices) {
    double angle = Math.atan2(yy - y, xx - x);
    double sliceAngle = 2.0 * Math.PI / nSlices;
    return (int) (angle / sliceAngle);
}

The above code makes the following assumptions:

  • slices are all the same (angular) width
  • slices are indexed counter-clockwise
  • slice 0 starts at the +x axis
  • slices own their right edge but not their left edge

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.)

查看更多
我命由我不由天
4楼-- · 2019-04-17 11:44

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 ?)

if( (xx-x)*(xx-x) + (yy-y)*(yy-y) > radius*radius)
   return false;

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

  double theta = Math.atan2(yy-y,xx-x);
  //theta is now in the range -Math.PI to Math.PI
  if(theta<0)
     theta = Math.PI - theta;
  //Now theta is in the range [0, 2*pi]
  //Use this value to determine which slice of the circle the 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 now be a number from 1 to 8 representing which part of the circle
  // the point is in, where the slices are numbered 1 to numSlices starting with
  // the right middle (positive x-axis if the center is (0,0).
查看更多
登录 后发表回答