×

Search anything:

Check if line intersects circle

Binary Tree book by OpenGenus

Open-Source Internship opportunity by OpenGenus for programmers. Apply now.

In this article, we have explained how to check if line intersects circle and have provided three mathematical proofs along with implementation.

Table of Contents

  • Introduction
  • Algebraic Proof
  • Geometric Proof
  • Vector Proof
  • Python Implementation
  • Overview

Introduction

In this article we will explore the distance from a point to a line formula, it is a useful formula that can be used within coordinate geometry to check if a line intersects with a circle, hence making it a tangent. We shall derive the formula using various methods and then write code for it using an example problem.

The formula between the line ax + by + c = 0 and point (x0,y0) can be given as:

baff35b2a747a59d5a113dcabea8fda7d2c09288

Where a, b, c are constant real numbers, representing the equation of the line, and a, b != 0.

This formula is finding the perpendicular distance between the centre of the circle and the line using the radius of the cirlce.

  • If the distance is greater than the radius, then the line is outside the circle
  • If the distance equals the radius, then the line is touching the circle
  • If the distance is less than the radius, the line is intersecting the circle

We shall prove this formula below.

Algebraic Proof

We can derive this formula algebraically if the line is not horizontal or vertical, and a and b != 0.

We know that the line ax+by+c=0 has the gradient -a/b -> this means that a perpendicular line will have the gradient of b/a as it is the negative reciprocal.

If the point (n,m) is the point of intersection of our line that it passess through the point (x0,y0). This line through the point of intersection and the point we are checking will be perpendicular to the original line. This gives us:

3db0f24914ca21ebdc4c967c574873c826f05e07

We can represent the fact that (m,n) is on the line ax+by+c=0 as such by squaring:

19963f774e32cb8a862e984caf835ab0496a49a9

Which implies that:

1c36a73095f9eac38af806539c14ec3c2f81a49f

If we find the distance between the two points using distance formula, we get:

CodeCogsEqn--6-

Finally we can rearrange this equation to get the original that we set out at the start of the article:

CodeCogsEqn--7-

Geometric Proof

We can also prove this geometrically if, again, the line isn't horizontal or vertical.

maxresdefault-1

Let the red line be the line with the equation ax+by+c=0 and the point highlighted in green P, with the coordinates of (x0,y0).

We can drop a perpendicular to the line ax+by+c=0, labeled d from point P. Let the foot of this perpendicular be R. We can also draw a vertical line through point P (highlighted in blue) and label the point of intersection S.

maxresdefault--3-

At any point (T) on the line, we can draw a right angle triange TVU with horizontal and vertical line segments with the hypotenuse TU on a line and horizontal side of |B|. This means the vertical side of the traingel will have length |A| since the gradient of the line is -A/B.

maxresdefault--2-

We have constructed two triangles PRS and TVU, they aer similar as they are both right angle triangles and ∠PSR approx. ∠TUV as they corresponding angles of a traversal of the parallel lines PS and UV. These sides of the triangle are in the same ratio, therefore we can deduce:

bb45959b62f2bd5e9300a43cb7aacef50901cd1b

Let point S have the coordinates (x0,m) -> |PS| = |y0-m|. The distance from P to the line can be represented as:

00abd0a03809d493bbb1b22d4f4deae3559da5da

We know that S is on the line, therefore we can find the value of m by:

fa851437803692ec637ffbd40a759f854342488a

Once we have m, this gives us the original equation of:

a878404f855da9c4998968ae89db6777d5047375

Vector Proof

Let P be the point with our coordinates (x0,y0) and the line equation as ax+by+c=0. We can also create another point Q, with the coordinates (x1,y1) which can be any point on the line and a vector v (a,b) which starts on point Q. Vector v is perpendicular to our line. The distance from P to Q is equal to the length of the orthagonal projection

29b78737274c81c6169dfd12569caf426f66f0dc on vector v.

We can write this projection length as:

9518b8d989390c2413efe2dac0498da4e0f68573
We can find 29b78737274c81c6169dfd12569caf426f66f0dc-1 by subtracting points -> CodeCogsEqn--8-

Using this, we can deduce: CodeCogsEqn--9- and CodeCogsEqn--10-

We know that Q is a point on the line so we can write c as -ax1 -by1. Finally from this we can get the original equation:

357e6a175063453f5eff24b8f24d777ae70279c9

We can see a visual representation of this below.

Vectorpoint-to-line.svg-1

Python Implementation

You can read the python implementation below:

import math

def check_intersection(a,b,c,x,y,rad):

    dist = ((abs(a*x+b*y+c))/math.sqrt(a**2+b**2))

    if (rad == dist):
        print("Touching")
    elif (rad > dist):
        print("Intersection")
    else:
        print("Lies outside")

rad = 10
a = 2
b = 3
c = 4
x = 5
y = 0
check_intersection(a,b,c,x,y,rad)

This algorithm is simple in design, first, we input values into the formula using math library, then use branching statements to determine whether the distance is greater, equal to or less than the radius. We then use this information to print what position the line is in relation to the point on the circle.

In our case: CodeCogsEqn--5-

Using the values we input, the perpendicular distance was approximately 3.88, if we compare this to the radius of the circle which was 10, this is clearly smaller, therefore we can say that this line intersects with the circle.

Overview

In this article at OpenGenus, we have explained and derived the distance from a point to a line formula in various ways (from algebra, geometry and vectors) then described how to use it in the context of circles. We have also implemented this formula in python and gave an example problem, after reading this article, you should have a strong grasp of this formula and be able to use it for yourself and understand its principles.

Check if line intersects circle
Share this