# Determine coordinate of intersection between a line and a circle

I'm putting together a simple script in processing to visualise layout possibilities within the fluid environment of a web page.

I need some help calculating a point on a circle:

The circle is as big as it can be, limited by the width or the visible height of the web browser viewport, whichever is less. A line intersects the circle through its centre from one corner of the viewport/rectangle to the opposite corner.

My question is how can I calculate the x & y coordinates of the line's intersections on the circle's circumference?

-

If the line and circle are specified in standard form $(x-a)^2+(y-b)^2=r^2$ and $y=mx+c$, you have two equations with two unknowns. Just plug the expression for $y$ from the line into the circle equation and you have a quadratic in $x$ which will give the (up to) two solutions. If your line and circle are specified differently, a similar technique will probably work, but you need to define how they are specified.

-
Of course... before you even try to compute intersections, you would want to verify first the inequality $$\frac{|ma-b+c|^2}{m^2+1}\leq r^2$$ ; if this isn't satisfied, then you've no (real) intersection points to speak of. On another note, $ux+vy+w=0$ might be a better standard form since this can handle verticals, which slope-intercept form cannot do. – J. M. Apr 27 '11 at 0:31
@J-M thanks, and thanks @Ross Millikan. Going to take to me a little while to grok this fully (I only have GCSE Maths, did physics instead after that!) – sanchothefat May 4 '11 at 18:44

Call the center of the circle $(0,0)$ and the corner of the window, which the line passes through, $(a,b)$. Let $r$ be the radius of the circle. Then just multiply $a$ and $b$ by $\frac{r}{\sqrt{a^2+b^2}}$ and you have the coordinates on the circle.

-

here are some vb codes: 'circle: (x-a)^2+(y-b)^2=r^2 'line: y=mx+c

    m = (y2-y1)/(x2-x1)

c = (-m * x1 + y1)
aprim = (1 + m ^ 2)
bprim = 2 * m * (c - b) - 2 * a
cprim = a ^ 2 + (c - b) ^ 2 - r ^ 2

delta = bprim ^ 2 - 4 * aprim * cprim

x1_e_intersection = (-bprim + Math.Sqrt(delta)) / (2 * aprim)
y1_e_intersection = m * x1_s_intersection + c

x2_e_intersection = (-bprim - Math.Sqrt(delta)) / (2 * aprim)
y2_e_intersection = m * x2_s_intersection + c

-
It might be better to present the solution mathematically, which will help the user code it in whichever language they deem appropriate. – Emily Apr 26 '13 at 15:50