Java 检查某个点是否存在于圆扇区内(Check whether a point exists in circle sector or not)

我们有一个以原点 (0, 0) 为中心的圆。作为输入,我们给出了圆扇区的起始角度和圆扇区的大小(以百分比表示)。 

例子: 

输入:半径 = 8
         起始角 = 0
         百分比 = 12 
         x = 3 y = 4
         
输出:点 (3, 4) 位于圆
         扇区内

输入:半径 = 12
        起始角 = 45
        百分比 = 25   
        x = 3 y = 4
        
输出:点 (3, 4) 不位于
         圆扇区内

在此图像中,起始角度为 0 度,半径为 r,假设彩色区域百分比为 12%,则我们计算结束角度为360/百分比 + 起始角度。

为了确定点 (x, y) 是否存在于圆扇区(以原点为中心)内,我们需要找到该点的极坐标,然后执行以下步骤:

    1、使用这个将 x, y 转换为极坐标角度 = atan(y/x); 半径 = sqrt(x * x + y * y);

    2、那么角度必须介于 StartingAngle(起始角) 和 EndingAngle(终止角) 之间,并且半径必须介于 0 和您的半径之间。

示例代码:

// Java program to check if
// a point lies inside a circle
// sector.
 
class GFG
{
static void checkPoint(int radius, int x, int y, float percent,
                                         float startAngle)
{
 
    // calculate endAngle
    float endAngle = 360/percent + startAngle;
  
    // Calculate polar co-ordinates
    double polarradius = Math.sqrt(x*x+y*y);
    double Angle = Math.atan(y/x);
  
    // Check whether polarradius is
    // less then radius of circle
    // or not and Angle is between
    // startAngle and endAngle
    // or not
    if (Angle>=startAngle && Angle<=endAngle && polarradius<radius)
        System.out.print("Point"+"("+x+","+y+")"+
        " exist in the circle sector\n");
    else
        System.out.print("Point"+"("+x+","+y+")"+
        " exist in the circle sector\n");
}
 
// Driver Program to test above function
public static void main(String arg[])
{
    int radius = 8, x = 3, y = 4;
    float percent  = 12, startAngle = 0;
    checkPoint(radius, x, y, percent, startAngle);
}
}
 
// This code is contributed
// by Anant Agarwal. 

输出 : 

点(3,4)位于圆扇区内

时间复杂度:  O(1)

辅助空间: O(1)

如果您喜欢此文章,请收藏、点赞、评论,谢谢,祝您快乐每一天。 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值