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

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

null
  1. 直线与圆相交。
  2. 线与圆接触。
  3. 线在圆圈外。

Check if a line touches or intersects a circle

注:直线的一般方程为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}}}  参考 维基 有关上述公式的详细信息。

C++

// CPP program to check if a line touches or
// intersects or outside a circle.
#include <bits/stdc++.h>
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

<?php
// PHP program to check if a line
// touches or intersects or outside
// a circle.
function checkCollision( $a , $b , $c ,
$x , $y , $radius )
{
// Finding the distance
// of line from center.
$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 )
echo "Touch" ;
else if ( $radius > $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

<script>
// JavaScript program to check if a line touches or
// intersects or outside a circle.
function checkCollision(a, b, c, x, y, radius)
{
// Finding the distance of line from center.
let 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)
document.write ( "Touch" );
else if (radius > dist)
document.write( "Intersect" ) ;
else
document.write( "Outside" ) ;
}
// Driver Code
let radius = 5;
let x = 0, y = 0;
let a = 3, b = 4, c = 25;
checkCollision(a, b, c, x, y, radius);
// This code is contributed by susmitakundugoaldanga.
</script>


输出:

Touch

本文由 阿努伊·乔汉 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享