📜  检查圆扇区中是否存在点。

📅  最后修改于: 2021-10-23 08:05:01             🧑  作者: Mango

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

例子:

Input :  Radius = 8 
         StartAngle = 0 
         Percentage = 12 
         x = 3 y = 4 
Output : Point (3, 4) exists in the circle 
         sector

Input : Radius = 12 
        Startangle = 45
        Percentage = 25  
        x = 3 y = 4 
Output : Point (3, 4) does not exist in 
         the circle sector
来源:wikibooks.org

来源:wikibooks.org

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

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

  1. 使用此将 x, y 转换为极坐标
    角度 = atan(y/x);半径 = sqrt(x * x + y * y);
  2. 然后角度必须在起始角和结束角之间,半径必须在 0 和你的半径之间。
C++
// C++ program to check if a point lies inside a circle
// sector.
#include
using namespace std;
 
void checkPoint(int radius, int x, int y, float percent,
                                         float startAngle)
{
    // calculate endAngle
    float endAngle = 360/percent + startAngle;
 
    // Calculate polar co-ordinates
    float polarradius = sqrt(x*x+y*y);
    float Angle = 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


Java
// 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


Python3
# Python3 program to check if a point
# lies inside a circle sector.
import math
 
def checkPoint(radius, x, y, percent, startAngle):
 
    # calculate endAngle
    endAngle = 360 / percent + startAngle
 
    # Calculate polar co-ordinates
    polarradius = math.sqrt(x * x + y * y)
    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 and Angle <= endAngle
                        and polarradius < radius):
        print("Point (", x, ",", y, ") "
              "exist in the circle sector")
    else:
        print("Point (", x, ",", y, ") "
              "does not exist in the circle sector")
 
# Driver code
radius, x, y = 8, 3, 4
percent, startAngle = 12, 0
 
checkPoint(radius, x, y, percent, startAngle)
 
# This code is contributed by
# Smitha Dinesh Semwal


C#
// C# program to check if a point lies
// inside a circle sector.
using System.IO;
using System;
 
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
        float polarradius =
                    (float)Math.Sqrt(x * x + y * y);
                     
        float Angle = (float)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)
            Console.Write("Point ({0}, {1}) exist in "
                         + "the circle sector", x, y);
        else
            Console.Write("Point ({0}, {1}) does not "
                + "exist in the circle sector", x, y);
    }
     
    // Driver code
    public static void Main()
    {
        int radius = 8, x = 3, y = 4;
        float percent = 12, startAngle = 0;
        checkPoint(radius, x, y, percent, startAngle);
    }
}
 
// This code is contributed by Smitha Dinesh Semwal


Javascript


输出 :

Point(3, 4) exists in the circle sector

时间复杂度 = O(1)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程