📌  相关文章
📜  相对于圆找到坐标的象限

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

给定圆心的半径和坐标。如果该点位于圆内,则找到相对于圆心的另一个给定坐标(X,Y)所在的象限。否则打印错误“谎言不在圆圈内”。
如果该点位于圆的中心,则输出0,或者如果该点位于圆的任何轴上,并且在圆的内部,则在逆时针方向上输出下一个象限。

例子:

方法:
设中心为(x’,y’)
圆的等式为(x-x')^2 + (y-y')^2 - r^2 = 0 –(等式1)
根据这个等式,
如果 (x-x')^2 + (y-y')^2 > r点(x,y)位于圆外
如果 (x-x')^2 + (y-y')^2 = 0 点(x,y)在圆上
如果 (x-x')^2 + (y-y')^2 < r 点(x,y)位于圆内

检查点相对于圆的位置:-

1. Put given coordinates in equation 1.
2. If it is greater than 0 coordinate lies outside circle.
3. If point lies inside circle find the quadrant within the circle. Check the point 
   with respect to centre of circle.

下面是上述想法的实现:

C++
// CPP Program to find the quadrant of
// a given coordinate with respect to the
// centre of a circle
#include 
  
using namespace std;
  
// Thus function returns the quadrant number
int getQuadrant(int X, int Y, int R, int PX, int PY)
{
    // Coincides with center
    if (PX == X && PY == Y)
        return 0;
  
    int val = pow((PX - X), 2) + pow((PY - Y), 2);
  
    // Outside circle
    if (val > pow(R, 2))
        return -1;
  
    // 1st quadrant
    if (PX > X && PY >= Y)
        return 1;
  
    // 2nd quadrant
    if (PX <= X && PY > Y)
        return 2;
  
    // 3rd quadrant
    if (PX < X && PY <= Y)
        return 3;
  
    // 4th quadrant
    if (PX >= X && PY < Y)
        return 4;
}
  
// Driver Code
int main()
{
    // Coordinates of centre
    int X = 0, Y = 3;
  
    // Radius of circle
    int R = 2;
  
    // Coordinates of the given point
    int PX = 1, PY = 4;
  
    int ans = getQuadrant(X, Y, R, PX, PY);
    if (ans == -1)
        cout << "Lies Outside the circle" << endl;
    else if (ans == 0)
        cout << "Coincides with centre" << endl;
    else
        cout << ans << " Quadrant" << endl;
    return 0;
}


Java
// Java Program to find the quadrant of
// a given coordinate with respect to the
// centre of a circle
import java.io.*;
class GFG {
  
// Thus function returns 
// the quadrant number
static int getQuadrant(int X, int Y,
                       int R, int PX,
                       int PY)
{
      
    // Coincides with center
    if (PX == X && PY == Y)
        return 0;
  
    int val = (int)Math.pow((PX - X), 2) + 
              (int)Math.pow((PY - Y), 2);
  
    // Outside circle
    if (val > Math.pow(R, 2))
        return -1;
  
    // 1st quadrant
    if (PX > X && PY >= Y)
        return 1;
  
    // 2nd quadrant
    if (PX <= X && PY > Y)
        return 2;
  
    // 3rd quadrant
    if (PX < X && PY <= Y)
        return 3;
  
    // 4th quadrant
    if (PX >= X && PY < Y)
        return 4;
        return 0;
}
  
    // Driver Code
    public static void main (String[] args) 
    {
          
        // Coordinates of centre
        int X = 0, Y = 3;
      
        // Radius of circle
        int R = 2;
      
        // Coordinates of the given point
        int PX = 1, PY = 4;
      
        int ans = getQuadrant(X, Y, R, PX, PY);
        if (ans == -1)
            System.out.println( "Lies Outside the circle");
        else if (ans == 0)
            System.out.println( "Coincides with centre");
        else
            System.out.println( ans +" Quadrant");
    }
}
  
// This code is contributed by anuj_67.


Python3
# Python3 Program to find the 
# quadrant of a given coordinate 
# w.rt. the centre of a circle
import math
  
# Thus function returns the
# quadrant number
def getQuadrant(X, Y, R, PX, PY):
      
    # Coincides with center
    if (PX == X and PY == Y):
        return 0;
  
    val = (math.pow((PX - X), 2) + 
           math.pow((PY - Y), 2));
  
    # Outside circle
    if (val > pow(R, 2)):
        return -1;
  
    # 1st quadrant
    if (PX > X and PY >= Y):
        return 1;
  
    # 2nd quadrant
    if (PX <= X and PY > Y):
        return 2;
  
    # 3rd quadrant
    if (PX < X and PY <= Y):
        return 3;
  
    # 4th quadrant
    if (PX >= X and PY < Y):
        return 4;
  
# Driver Code
# Coordinates of centre
X = 0; 
Y = 3;
  
# Radius of circle
R = 2;
  
# Coordinates of the given po
PX = 1;
PY = 4;
  
ans = getQuadrant(X, Y, R, PX, PY);
if (ans == -1) : print("Lies Outside the circle");
elif (ans == 0) : print("Coincides with centre");
else:print(ans, "Quadrant");
  
# This code is contributed by mits


C#
// C# Program to find the quadrant of
// a given coordinate with respect to
// the centre of a circle
using System;
  
class GFG {
  
    // Thus function returns 
    // the quadrant number
    static int getQuadrant(int X, int Y,
                  int R, int PX, int PY)
    {
          
        // Coincides with center
        if (PX == X && PY == Y)
            return 0;
      
        int val = (int)Math.Pow((PX - X), 2)
               + (int)Math.Pow((PY - Y), 2);
      
        // Outside circle
        if (val > Math.Pow(R, 2))
            return -1;
      
        // 1st quadrant
        if (PX > X && PY >= Y)
            return 1;
      
        // 2nd quadrant
        if (PX <= X && PY > Y)
            return 2;
      
        // 3rd quadrant
        if (PX < X && PY <= Y)
            return 3;
      
        // 4th quadrant
        if (PX >= X && PY < Y)
            return 4;
            return 0;
    }
  
    // Driver Code
    public static void Main () 
    {
      
        // Coordinates of centre
        int X = 0, Y = 3;
      
        // Radius of circle
        int R = 2;
      
        // Coordinates of the given point
        int PX = 1, PY = 4;
      
        int ans = 
             getQuadrant(X, Y, R, PX, PY);
        if (ans == -1)
            Console.WriteLine( "Lies Outside"
                            + " the circle");
        else if (ans == 0)
            Console.WriteLine( "Coincides "
                          + "with centre");
        else
            Console.WriteLine( ans + 
                               " Quadrant");
    }
}
  
// This code is contributed by anuj_67.


PHP
 pow($R, 2))
        return -1;
  
    // 1st quadrant
    if ($PX > $X and $PY >= $Y)
        return 1;
  
    // 2nd quadrant
    if ($PX <= $X and $PY > $Y)
        return 2;
  
    // 3rd quadrant
    if ($PX < $X and $PY <= $Y)
        return 3;
  
    // 4th quadrant
    if ($PX >= $X and $PY < $Y)
        return 4;
}
  
    // Driver Code
    // Coordinates of centre
    $X = 0; $Y = 3;
  
    // Radius of circle
    $R = 2;
  
    // Coordinates of the given po$
    $PX = 1;
    $PY = 4;
  
    $ans = getQuadrant($X, $Y, $R,
                         $PX, $PY);
    if ($ans == -1)
        echo "Lies Outside the circle" ;
    else if ($ans == 0)
        echo "Coincides with centre" ;
    else
        echo $ans , " Quadrant" ;
  
// This code is contributed by anuj_67.
?>


输出:

1 Quadrant