给定中心坐标和半径> 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
这个想法是将圆心和直线之间的垂直距离与圆的半径进行比较。
算法: 如何找到垂直距离? 有关以上公式的详细信息,请参阅Wiki。 输出 :
1.找到圆心和给定线之间的垂直线(例如p)。
2.将该距离p与半径r进行比较。
……a)如果p> r,则线位于圆的外部。
……b)如果p = r,则直线接触圆。
……c)如果p
一条线到一个点的距离可以使用以下公式计算: C++
// CPP program to check if a line touches or
// intersects or outside a circle.
#include
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