Finding Intersections Between Circles and Lines: A Programmer's Guide

  • Context: Undergrad 
  • Thread starter Thread starter Pazil
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around finding intersection points between circles and lines, specifically in the context of programming a collision detection library. Participants explore mathematical formulations and coordinate transformations necessary for calculating these intersections.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Homework-related

Main Points Raised

  • One participant describes their goal of implementing collision detection for lines and circles using the equations of a line and a circle.
  • Another participant suggests translating the circle's coordinates to simplify the intersection calculation.
  • A participant expresses confusion about deriving the quadratic form Ax^2 + Bx + C = 0 from their equations and seeks clarification on how to identify the coefficients.
  • It is noted that if B is zero, the quadratic equation simplifies, and the conditions for real roots are discussed.
  • Participants discuss the correct formulation of the quadratic equation after substituting the line equation into the circle equation.
  • One participant shares their derived equation and asks for feedback on its correctness.
  • Another participant encourages testing the derived equation with specific values to verify its accuracy.
  • Questions arise about how to handle vertical lines in the context of the equations being discussed.
  • Participants consider parametrizing the line equations and express a need for further assistance with parametric forms.

Areas of Agreement / Disagreement

Participants generally agree on the mathematical principles involved but express varying levels of understanding and clarity regarding specific transformations and formulations. There is no consensus on the best approach for handling vertical lines or parametrization.

Contextual Notes

Some participants mention the need to distinguish between cases when dealing with vertical lines and the implications of different forms of line equations. There are also unresolved questions about the parametrization of lines and circles.

Pazil
Messages
8
Reaction score
0
Hi!

I'm new to these forums, and a grade 10 Flash programmer. I'm currently building a collision detection library, with which I currently have Lines and Circles.

My problem is that I don't know how to get the intersection points between a circle and a line. I'm using mx+b for the line, in other words infinite lines. I already have intersection with the equation x^2 + y^2 = r^2 good to go, but I have no idea how to translate this into:
(x - h)^2 + (y - k)^2 = r^2, in other words add coordinates to the intersection calculation. I only need the calculations up to the point where I can get A, B, and C variables for the quadratic formula (from then on it's simple plugging in).

Any help what so ever is greatly appreciated! I just found these forums, and it seems as though I'll be in heaven from now on!

P.
 
Physics news on Phys.org
If you define new coordinates (by translation):

x1 = x-h
y1 = y-k

then you have reduced to the case you already know (center at origin):

x1^2 + y1^2 = r^2

y1+k=m(x1+h)+b, i.e y1=mx1+(b+mh-k)

Compute the intersection and convert the result back to (x,y)-coordinates.
 
Alright, I'll try that!

Thank you!

Edit:

I'm still a bit confused, since I have no idea how to get that into Ax^2 + Bx + C = 0 form...?
I get to (1 + m^2)x1^2 + (b + mh - k)^2 = r^2
But I don't see how to get a B for Bx, since there's no x1 terms.
 
Last edited:
Then B= 0. In fact, that form is particularly easy to solve. If [itex]ax^2+ bx+ c= 0[/itex] and b= 0, [itex]ax^2+ c= 0[/itex] or [itex]ax^2= -c[/itex] so [itex]x^2= -c/a[/itex] which has real roots if and only if -c/a is non-negative and in that case [itex]x= \pm\sqrt{-c/a}[/itex].
 
Last edited by a moderator:
Alright then. I guessed that much, and I realized that B would then be excluded, but I just don't understand how this form, which includes the x/y coordinates factors to only:
Ax^2 + c = 0
When if you do:

x^2 + y^2 = r^2
y = mx + b

x^2 + (mx+b)^2 = r^2

(1+m^2)x^2 + 2mbx + b^2 - r^2 = 0
Therefore:
A = 1+m^2
B = 2mb
C = b^2 - r^2

I realize that it has to do with assigning x1 = x - h and same for the y, but I just don't understand how you would factor it still...I mean, I do, but...I hate when I do something in Math, and I don't understand what's happening.
Please go easy on me, since I'm in grade 10 (I mean, not easy...but just, explain a bit more please?)

P.
 
Pazil said:
(1+m^2)x^2 + 2mbx + b^2 - r^2 = 0
Therefore:
A = 1+m^2
B = 2mb
C = b^2 - r^2

That is correct.

I get to (1 + m^2)x1^2 + (b + mh - k)^2 = r^2

Here you forgot the 2mx1(b+mh-k) term.
 
Well, I apologize for the seemingly empty posts I've been writing, but I haven't had enough time to totally just sit down and work out the things you threw at me.
I tried to collect all like terms again, and this is what I got (Ax^2 + Bx + C = 0):

(1 + m^2)x^2 + (-2h + 2bm - 2km)x + h^2 + b^2 - 2kb + k^2 - r^2 = 0
This is from (x - h)^2 + (mx+b - k)^2 = r^2

Does it look alright to you guys?

P.
 
Pazil said:
Well, I apologize for the seemingly empty posts I've been writing, but I haven't had enough time to totally just sit down and work out the things you threw at me.
I tried to collect all like terms again, and this is what I got (Ax^2 + Bx + C = 0):

(1 + m^2)x^2 + (-2h + 2bm - 2km)x + h^2 + b^2 - 2kb + k^2 - r^2 = 0
This is from (x - h)^2 + (mx+b - k)^2 = r^2

Does it look alright to you guys?

P.

Looks good :) It's also good to test the solution by plugging in a few values for m,b,etc and see if the solutions satisfy the equations of the line and the circle.

How do the results change if the line is expressed as ax+by+c=0 (this includes the vertical line when b=0).
 
I just tested in my collision engine and it works perfectly also!

How would I plugin in the ax + by + c = 0 into the (x - h)^2 + (y - k)^2 = r^2 equation?
Supporting vertical lines would of course be a big bonus!

P.

Or, how could I parametrize this, so that x becomes a function of t?
 
Last edited:
  • #10
Pazil said:
How would I plugin in the ax + by + c = 0 into the (x - h)^2 + (y - k)^2 = r^2 equation?
Supporting vertical lines would of course be a big bonus!

I think that at some point you will have to distinguish between two cases. If b is nonzero in ax+by+c you can solve for y in terms of x. In the other case you could just temporarily switch the x and y coordinates.
 
  • #11
I thought of that just after I posted!

I'm going to try to parametrize my line equations by myself, but I'll definitely need help for parametric circles vs. lines and circles vs. circles...

P.

By the way, in some equations, I spot this: x = 2[ y + jh - 5] or something...what does the 2[] mean? I'm guessing it's just like normal brackets?
 

Similar threads

  • · Replies 14 ·
Replies
14
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
6K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 20 ·
Replies
20
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K