# I calculated the intersection of a line with a circle, got projection instead.

I have 2 points that form a line, along with a center point for a circle and its radius. The formula for my circle is:

$(x - C_x)^2 + (y - C_y)^2 = r^2 ,$ where $C$ denotes CircleCenter

The formula for my line (since it's 2 points) is:

$(y - y_1) = m (x-x_1),$ where $m = y_2/y_1 - x_2/x_1$

$y = m(x-x_1) + y_1$

I substituted the formula so that all of them are variables of $x$ (substitute formula 2 in 1), then I calculated both values of $x,$ which I then reused in the original formula to calculate the corresponding $y$ values:

$y = m(x - x_1) + y_1,$ where $x$ is the 2 values

The output I'm getting, however, is an projection rather than an intersection, such as the points of intersection are ALWAYS counted even though the points are outside/inside the circle.

Provided pictures with examples.

Correct Intersection Picture

Incorrect Intersection Picture #1

Incorrect Intersection Picture #2

Edit: I've been asked to post the formulas that I substituted:

$x^2 - 2xC_x + C_x^2 + (m(x-x_1) + y_1)^2 - 2(m(x-x_1) + y_1) C_y + C_y^2 - r^2 = 0$

• @dxiv I added that into the main post – thethiny Apr 21 '18 at 23:24

## 1 Answer

You are getting "projected" points of intersection because

$$y = m(x - x_1) + y_1$$

is the formula of a line. You can increase or decrease $x$ indefinitely and find values of $y$ for every value of $x,$ not just the values of $x$ between $x_1$ and $x_2.$

If you want to find intersections only with the segment, you can do it in two steps. First, find all intersections with the line. Then discard any intersection whose $x$ coordinate is not between $x_1$ and $x_2.$

Incidentally, in computing the equation of your line, $m \neq y_2/y_1 - x_2/x_1.$ The correct formula is $$m = \frac{y_2 - y_1}{x_2 - x_1}.$$

• Thank you it worked just fine! And for the slope, yes it's a typo. Thanks! – thethiny Apr 22 '18 at 0:12