Given coordinate of the center and radius > 1 of a circle and the equation of a line. The task is to check if the given line collide with the circle or not. There are three possibilities :
- Line intersect the circle.
- Line touches the circle.
- Line is outside the circle.
Note: General equation of a line is a*x + b*y + c = 0, so only constant a, b, c are given in the input.
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
The idea is to compare the perpendicular distance between center of circle and line with the radius of the circle.
1. Find the perpendicular (say p) between center of circle and given line.
2. Compare this distance p with radius r.
……a) If p > r, then line lie outside the circle.
……b) If p = r, then line touches the circle.
……c) If p < r, then line intersect the circle.
How to find the perpendicular distance ?
Distance of a line from a point can be computed using below formula:
Refer Wiki for details of above formula.
This article is contributed by Anuj Chauhan. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Program to calculate area of inner circle which passes through center of outer circle and touches its circumference
- Check if a circle lies inside another circle or not
- Equation of circle when three points on the circle are given
- Angle subtended by the chord to center of the circle when the angle subtended by the another equal chord of a congruent circle is given
- Find area of the larger circle when radius of the smaller circle and difference in the area is given
- Area of the circle that has a square and a circle inscribed in it
- Slope of the line parallel to the line with the given slope
- Check whether a point exists in circle sector or not.
- Check whether given circle resides in boundary maintained by two other circles
- Check if a given circle lies completely inside the ring formed by two concentric circles
- Queries to check if it is possible to join boxes in a circle
- Check whether it is possible to join two points given on circle such that distance between them is k
- Check if any point overlaps the given Circle and Rectangle
- How to check if two given line segments intersect?
- Check if a line passes through the origin
- Check if a line at 45 degree can divide the plane into two equal weight parts
- Check if it is possible to draw a straight line with the given direction cosines
- Check whether the point (x, y) lies on a given line
- Check whether two points (x1, y1) and (x2, y2) lie on same side of a given line or not
- Check whether a straight line can be formed using N co-ordinate points
Improved By : Sam007