The Wayback Machine - https://web.archive.org/web/20240928070511/https://www.geeksforgeeks.org/check-line-touches-intersects-circle/
Open In App

Check if a line touches or intersects a circle

Last Updated : 05 Sep, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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 collides with the circle or not. There are three possibilities : 

  1. Line intersects the circle.
  2. Line touches the circle.
  3. Line is outside the circle
     

Check if a line touches or intersects a 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.

Examples : 

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 : Intersect
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.
Algorithm: 
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:
\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);
     
    }
}
 
//

                    

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.

                    

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>

                    

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
?>

                    

Output
Touch

Time Complexity : O(log(a*a + b*b)) as it is using inbuilt sqrt function
Auxiliary Space : O(1)


This article is contributed by Anuj Chauhan.



Previous Article
Next Article

Similar Reads

Program to calculate area of inner circle which passes through center of outer circle and touches its circumference
Given a circle C1 and it's a radius r1. And one another circle C2 whose passes through center of circle C1 and touch the circumference of circle C1. The task is to find out the area of circle C2. Examples: Input: r1 = 4Output:Area of circle c2 = 12.56Input: r1 = 7Output:Area of circle c2 = 38.465 Approach: Radius r2 of circle C2 is [Tex]\ r2 = r1/2
6 min read
Check if any two intervals intersects among a given set of intervals
An interval is represented as a combination of start time and end time. Given a set of intervals, check if any two intervals intersect. Examples: Input: arr[] = {{1, 3}, {5, 7}, {2, 4}, {6, 8}} Output: true The intervals {1, 3} and {2, 4} overlap Input: arr[] = {{1, 3}, {7, 9}, {4, 6}, {10, 13}} Output: false No pair of intervals overlap. Expected
13 min read
Minimize segments required to be removed such that at least one segment intersects with all remaining segments
Given an array arr[] consisting of N pairs [L, R], where L and R denotes the start and end indices of a segment, the task is to find the minimum number of segments that must be deleted from the array such that the remaining array contains at least one segment which intersects with all other segments present in the array. Examples: Input: arr[] = {{
10 min read
Count intervals that intersects with a given meeting time
Given an array arr[][2] consisting of N pair of strings representing the starting and ending time (in 12 hours format) and a string P which represents the time of the meeting, the task is to find the count of intervals that contains the time P. Examples: Input: P = “12:01:AM”, arr[][2] = {{“12:00:AM”, “11:55:PM”}, {“12:01:AM”, “11:50:AM”}, {“12:30:
9 min read
Check if a circle lies inside another circle or not
Given two circles with radii and centres given. The task is to check whether the smaller circle lies inside the bigger circle or not.  Examples:   Input: x1 = 10, y1 = 8, x2 = 1, y2 = 2, r1 = 30, r2 = 10 Output: The smaller circle lies completely inside the bigger circle without touching each other at a point of circumference. Input :x1 = 7, y1 = 8
6 min read
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
Given are two congruent circles with two equal chords. The angle subtended to the center from the chord of one of the circles is given. The task is to find the angle subtended by the chord to the center of another circle.Examples: Input: z = 48 Output: 48 degrees Input: z = 93 Output: 93 degrees Approach: In triangle AOB and PXQ AO = PX(radii of co
3 min read
Find area of the larger circle when radius of the smaller circle and difference in the area is given
Given two integers r and d where r is the radius of the smaller circle and d is the difference of the area of this circle with some larger radius circle. The task is to find the area of the larger circle.Examples: Input: r = 4, d = 5 Output: 55.24 Area of the smaller circle = 3.14 * 4 * 4 = 50.24 55.24 - 50.24 = 5Input: r = 12, d = 3 Output: 455.16
4 min read
Equation of circle when three points on the circle are given
Given three coordinates that lie on a circle, (x1, y1), (x2, y2), and (x3, y3). The task is to find the equation of the circle and then print the centre and the radius of the circle. The equation of circle in general form is x² + y² + 2gx + 2fy + c = 0 and in radius form is (x - h)² + (y -k)² = r², where (h, k) is the centre of the circle and r is
11 min read
Puzzle | Place numbers 1 to 9 in a Circle such that sum of every triplet in straight line is 15
Place the numbers 1 to 9 in a circle, so that wherever there are three in a straight line they shall add up to 15. SOLUTION:[video mp4="https://media.geeksforgeeks.org/wp-content/uploads/20200710130155/wpv.mp4"][/video]The sum of first number and last number is ten ( 1 + 9 = 10 ).Similarly, the sum of second number and second last element is also t
2 min read
Length of intercept cut off from a line by a Circle
Given six integers, a, b, c, i, j, and k representing the equation of the circle [Tex](x^2 + y^2 + ax + by + c = 0)[/Tex] and equation of the line [Tex](ix + jy + k = 0)[/Tex], the task is to find the length of the intercept cut off from the given line to the circle. Examples: Input: a = 0, b = 0, c = -4, i = 2, j = -1, k = 1 Output: 3.89872 Input:
11 min read
Slope of the line parallel to the line with the given slope
Given an integer m which is the slope of a line, the task is to find the slope of the line which is parallel to the given line. Examples: Input: m = 2 Output: 2 Input: m = -3 Output: -3 Approach: Let P and Q be two parallel lines with equations y = m1x + b1, and y = m2x + b2 respectively. Here m1 and m2 are the slopes of the lines respectively. Now
3 min read
Equation of straight line passing through a given point which bisects it into two equal line segments
Given a straight line which passes through a given point (x0, y0) such that this point bisects the line segment in two equal line segments. The task is to find the equation of this straight line.Examples: Input: x0 = 4, y0 = 3 Output: 3x + 4y = 24Input: x0 = 7, y0 = 12 Output: 12x + 7y = 168 Approach: Let PQ be the line and AB be the line segment b
4 min read
Equation of a straight line passing through a point and making a given angle with a given line
Given four integers a, b, c representing coefficients of a straight line with equation (ax + by + c = 0), the task is to find the equations of the two straight lines passing through a given point [Tex](x1, y1) [/Tex]and making an angle ? with the given straight line. Examples: Input: a = 2, b = 3, c = -7, x1 = 4, y1 = 9, ? = 30Output: y = -0.49x +1
15+ min read
Level order traversal line by line | Set 3 (Using One Queue)
Given a Binary Tree, print the nodes level-wise, each level on a new line. Output: 1 2 3 4 5Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. We have discussed two solutions in the articles below. Print level order traversal line by line | Set 1 Level order traversal line by line | Set 2 (Using Two Queues)In this p
6 min read
Print level order traversal line by line | Set 1
Given root of a binary tree, The task is to print level order traversal in a way that nodes of all levels are printed in separate lines. Examples: Input: Output: 208 224 1210 14 Input: 1 / \ 2 3 / \ 4 5 Output: 12 34 5 Recommended PracticeLevel order traversal Line by LineTry It! Note: that this is different from simple level order traversal where
13 min read
Level order traversal line by line | Set 2 (Using Two Queues)
Given a Binary Tree, print the nodes level wise, each level on a new line. Output: 1 2 3 4 5Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. We have discussed one solution in below article. Print level order traversal line by line | Set 1 In this post, a different approach using two queues is discussed. We can ins
7 min read
Check if two circles intersect such that the third circle passes through their points of intersections and centers
Given centres and the radii of three circles A, B, and C in the form of {X, Y, R}, where (X, Y) is the centre of the circle and R is the radius of that circle. The task is to check if any two circles intersect such that the third circle passes through the intersecting points and the centres of the two circles. If found to be true, then print "Yes".
9 min read
Check if a given circle lies completely inside the ring formed by two concentric circles
Given two circles of radius r and R, both have their centre at the origin. Now, given another circle of radius r1 and centre at (x1, y1). Check, if the third circle(circle of radius r1) lies completely inside the ring formed by two circles of radius r and R.Examples : Input : r = 8 R = 4 r1 = 2 x1 = 6 y1 = 0 Output : yes Input : r = 8 R = 4 r1 = 2
6 min read
Queries to check if it is possible to join boxes in a circle
Consider n boxes numbered arranged in a circle in increasing order in clockwise direction (numbered from 1 to n). You are given q queries, each containing two integer i and j. The task is to check whether it is possible to connect box i to box j by a rod without intersecting rods used to join other boxes in previous queries. Also, every box can be
11 min read
Check if elements of an array can be arranged in a Circle with consecutive difference as 1
Given an array of [Tex]N [/Tex]numbers. The task is to check if it is possible to arrange all the numbers in a circle so that any two neighboring numbers differ exactly by 1. Print "YES" if it is possible to get such arrangement and "NO" otherwise. Examples: Input: arr[] = {1, 2, 3, 2} Output: YES The circle formed is: 1 2 2 3 Input: arr[] = {3, 5,
3 min read
Check whether it is possible to join two points given on circle such that distance between them is k
Given two circles and a length, K. Find whether we can join two points (one on perimeter of each circle), so that distance between the points is K. (Coordinates of both points need not be an integer value). Examples: Input: Circle-1 Center (0, 0) Radius = 5 Circle-2 Center (8, 3) Radius = 2 K = 3 Output: Yes Maximum Distance: 15 Minimum Distance: 2
10 min read
Check if any point overlaps the given Circle and Rectangle
Given two opposite diagonal points of a rectangle (X1, Y1), (X2, Y2) and the center, radius of the circle R, (Xc, Yc), the task is to check if there exists any point P that belongs to both the circle as well as the rectangle. Examples: Input: R = 2, Xc = 0, Yc = 0, X1 = 1, Y1 = 0, X2 = 3, Y2 = 3 Output: true Explanation: Clearly, from the below ill
7 min read
Check if it is possible to reach any point on the circumference of a given circle from origin
Given a string S representing a sequence of moves(L, R, U, and D) and an integer R representing the radius of a circle whose center is the origin (0, 0), the task is to check if it is possible to reach any point on the circumference of the given circle from the origin by choosing any subsequence of moves from the string S. If it is possible to reac
10 min read
Check if chords of a Circle are symmetric after some rotation
Given two Integers N and M, N indicating equidistant points on circumference of a circle and M indicating number of chords formed with those points. Also given is a vector of pairs C containing position of chords. The task is to rotate the circle by any degree, say X, where 0 < X < 360, and check if the chords of are still symmetric to the or
10 min read
Check whether a point exists in circle sector or not.
We have a circle centered at origin (0, 0). As input we are given with starting angle of the circle sector and the size of the circle sector in percentage. Examples: Input : Radius = 8 StartAngle = 0 Percentage = 12 x = 3 y = 4 Output : Point (3, 4) exists in the circle sector Input : Radius = 12 Startangle = 45 Percentage = 25 x = 3 y = 4 Output :
5 min read
Check whether given circle resides in boundary maintained by two other circles
Given outer circle radius R and inner circle radius r, making circles from the same center and forming the boundary between them. Now, given X and Y coordinates which denote the center of the new circle to be formed with radius rad, your task is to check whether the circle with coordinates X and Y as the center can fit in the boundary of circles fo
6 min read
Check if a line at 45 degree can divide the plane into two equal weight parts
Given a set of n points (xi, yi) in 2D coordinate. Each point has some weight wi. The task is to check whether a line at 45 degrees can be drawn so that sum of weights of points on each side is equal.Examples: Input : x1 = -1, y1 = 1, w1 = 3 x2 = -2, y2 = 1, w2 = 1 x3 = 1, y3 = -1, w3 = 4 Output : Yes Input : x1 = 1, y1 = 1, w1 = 2 x2 = -1, y2 = 1,
8 min read
Check if it is possible to draw a straight line with the given direction cosines
Given three direction cosines l, m and n of a 3-D plane, the task is to check if it is possible to draw a straight line with them or not. Print Yes if possible else print No. Examples: Input: l = 0.258, m = 0.80, n = 0.23 Output: No Input: l = 0.70710678, m = 0.5, n = 0.5 Output: Yes Approach: If a straight line forms angle a with positive X-axis,
4 min read
Check whether two points (x1, y1) and (x2, y2) lie on same side of a given line or not
Given three integers a, b and c which represents coefficients of the equation of a line a * x + b * y - c = 0. Given two integer points (x1, y1) and (x2, y2). The task is to determine whether the points (x1, y1) and (x2, y2) lie on the same side of the given line or not.Examples: Input : a = 1, b = 1, c = 1, x1 = 1, y1 = 1, x2 = 1, y2 = 2 Output :
6 min read
Check whether a straight line can be formed using N co-ordinate points
Given an array arr[] of N co-ordinate points, the task is to check whether a straight line can be formed using these co-ordinate points. Input: arr[] = {{0, 0}, {1, 1}, {2, 2}} Output: Yes Explanation: Slope of every two points is same. That is 1. Therefore, a straight line can be formed using these points. Input: arr[] = {{0, 1}, {2, 0}} Output: Y
6 min read
Article Tags :
Practice Tags :