Java - 如何检查一个点是否在一个圆圈内 [英] Java - How to check of a point is inside a sliceof a circle

查看:1618
本文介绍了Java - 如何检查一个点是否在一个圆圈内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我画了一个圆圈,并且我想这样做,所以我可以有四个以上的切片。我可以轻松地做四个象限,因为我只是检查鼠标是否在圆圈内和框内。



这是我如何检查点是否在圆中如果(Math.sqrt((xx-x)*(xx-x)+(yy-y)*((xy-x))*($ x $ x $) yy-y))<= radius)
{
return true;
}
else
{
return false;
}

如果圆圈被分成4个以上的区域,我该如何修改?

解决方案

首先,我们可以检查点是否在圆内。但是我不会把它和检查哪个象限结合起来(这就是为什么你有radius / 2?)

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

现在我们可以通过使用atan2函数来查看点位于哪个区域。 atan2 与Arctan类似,只是Arctangent函数始终返回介于-pi / 2和pi / 2(-90和+90度)之间的值。我们需要极坐标方式的实际角度。现在假设(x,y)是圆的中心,我们对点(xx,yy)的位置感兴趣,我们有

  double theta = Math.atan2(yy-y,xx-x); 
// theta现在处于-Math.PI至Math.PI
的范围内if(theta <0)
theta = Math.PI-theta;
//现在Theta在[0,2 * pi]
的范围内//使用这个值来确定点所在的圆的哪一片。
//例如:
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;
休息;
}
}
// whichSlice现在应该是一个从1到8的数字,表示圆圈的哪一部分
//该点所在的位置,其中切片的编号为1到numSlices以
开头//右侧中间(如果中心为(0,0),则为正x轴)


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?

解决方案

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

这篇关于Java - 如何检查一个点是否在一个圆圈内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆