给定圆心坐标和半径 > 1 和直线方程。任务是检查给定的线是否与圆相撞。有三种可能:
- 线与圆相交。
- 线触及圆。
- 线在圆外。
注:一条直线的一般方程为 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 < r,则线与圆相交。
如何求垂直距离?
线到点的距离可以使用以下公式计算:
有关上述公式的详细信息,请参阅 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
?>
Javascript
输出 :
Touch
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。