📜  检查直线是否接触或相交圆

📅  最后修改于: 2021-04-29 06:44:52             🧑  作者: Mango

给定中心坐标和半径> 1的圆和直线方程。任务是检查给定的线是否与圆碰撞。有三种可能性:

  1. 线与圆相交。
  2. 线接触圆。
  3. 线在圆的外面。

注意:线的一般方程为a * x + b * y + c = 0,因此输入中仅给出常数a,b,c。

例子 :

Input : radius = 5, center = (0, 0), 
        a = 1, b = -1, c = 0.
Output : Intersect

Input :  radius = 5, center = (0, 0), 
         a = 5, b = 0, c = 0.
Output : Touch

Input : radius = 5, center = (0, 0),
         a = 1, b = 1, c = -16.
Output : Outside

这个想法是将圆心和直线之间的垂直距离与圆的半径进行比较。

算法:
1.找到圆心和给定线之间的垂直线(例如p)。
2.将该距离p与半径r进行比较。
……a)如果p> r,则线位于圆的外部。
……b)如果p = r,则直线接触圆。
……c)如果p

如何找到垂直距离?
一条线到一个点的距离可以使用以下公式计算:

\frac{ax_{0}+by_{0}+c}{\sqrt{a^{2}+b^{2}}}

有关以上公式的详细信息,请参阅Wiki。

C++
// CPP program to check if a line touches or 
// intersects or outside a circle.
#include 
using namespace std;
  
void checkCollision(int a, int b, int c, 
                  int x, int y, int radius)
{
    // Finding the distance of line from center.
    int dist = (abs(a * x + b * y + c)) / 
                     sqrt(a * a + b * b);
  
    // Checking if the distance is less than, 
    // greater than or equal to radius.
    if (radius == dist)
        cout << "Touch" << endl;
    else if (radius > dist)
        cout << "Intersect" << endl;
    else
        cout << "Outside" << endl;
}
  
// Driven Program
int main()
{
    int radius = 5;
    int x = 0, y = 0;
    int a = 3, b = 4, c = 25;
    checkCollision(a, b, c, x, y, radius);
    return 0;
}


Java
// Java program to check if a line touches or 
// intersects or outside a circle.
  
import java.io.*;
  
class GFG {
      
    static void checkCollision(int a, int b, int c, 
                               int x, int y, int radius)
    {
        // Finding the distance of line from center.
        double dist = (Math.abs(a * x + b * y + c)) / 
                        Math.sqrt(a * a + b * b);
      
        // Checking if the distance is less than, 
        // greater than or equal to radius.
        if (radius == dist)
            System.out.println ( "Touch" );
        else if (radius > dist)
            System.out.println( "Intersect") ;
        else
            System.out.println( "Outside") ;
    }
      
    // Driven Program
    public static void main (String[] args) 
    {
        int radius = 5;
        int x = 0, y = 0;
        int a = 3, b = 4, c = 25;
        checkCollision(a, b, c, x, y, radius);
      
    }
}
  
// This article is contributed by vt_m.


Python3
# python program to check if a line
# touches or  intersects or outside
# a circle.
  
import math
  
def checkCollision(a, b, c, x, y, radius):
      
    # Finding the distance of line 
    # from center.
    dist = ((abs(a * x + b * y + c)) /
            math.sqrt(a * a + b * b))
  
    # Checking if the distance is less 
    # than, greater than or equal to radius.
    if (radius == dist):
        print("Touch")
    elif (radius > dist):
        print("Intersect")
    else:
        print("Outside")
  
# Driven Program
radius = 5
x = 0
y = 0
a = 3
b = 4
c = 25
checkCollision(a, b, c, x, y, radius)
  
# This code is contributed by Sam007


C#
// C# program to check if a line touches or 
// intersects or outside a circle.
using System;
  
class GFG {
      
    static void checkCollision(int a, int b, int c, 
                            int x, int y, int radius)
    {
        // Finding the distance of line from center.
        double dist = (Math.Abs(a * x + b * y + c)) / 
                        Math.Sqrt(a * a + b * b);
      
        // Checking if the distance is less than, 
        // greater than or equal to radius.
        if (radius == dist)
            Console.WriteLine ("Touch");
        else if (radius > dist)
            Console.WriteLine("Intersect");
        else
            Console.WriteLine("Outside");
    }
      
    // Driven Program
    public static void Main () 
    {
        int radius = 5;
        int x = 0, y = 0;
        int a = 3, b = 4, c = 25;
          
        checkCollision(a, b, c, x, y, radius);
      
    }
}
  
// This article is contributed by vt_m.


PHP
 $dist)
        echo "Intersect";
    else
        echo "Outside" ;
}
  
// Driver Code
$radius = 5;
$x = 0;
$y = 0;
$a = 3;
$b = 4;
$c = 25;
checkCollision($a, $b, $c, $x, $y, $radius);
  
// This code is contributed by Sam007
?>


输出 :

Touch