Thought I'd have a go at the challenge, so after thinking through the algebra on a piece of paper, how about something like this:
Basically you pass any two points on the line, the center of the circle, and the radius to
GetIntersectionLineCircle() and the function will return the points of intersection (if there are any).
SuperStrict
Local tmpIntersectLineCircle#[][]
'tmpIntersectLineCircle = GetIntersectionLineCircle( [0.0,6.25], [1.0,7.0], [0.0, 0.0], 1.0 )
'tmpIntersectLineCircle = GetIntersectionLineCircle( [0.0,6.25], [1.0,7.0], [0.0, 0.0], 5.0 )
Local i%, tmpMax% = 1000 'Change this to find an average time for multiple calls
Local tmpTime% = MilliSecs()
For i% = 0 Until tmpMax
tmpIntersectLineCircle = GetIntersectionLineCircle( [0.0,6.25], [1.0,7.0], [3.0, 4.0], 25.0 )
Next
Print "Time Taken to Make " + i + " Calls: " + (MilliSecs()-tmpTime) + " ms"
Print "Average Time Taken per Call: " + ((MilliSecs()-tmpTime)/1.0/i) + " ms"
Select tmpIntersectLineCircle.length
Case 0; Print "Line does not intersect with circle!"
Case 1; Print "Line is a tangent to the circle, and touches at (" + tmpIntersectLineCircle[0][0] + ", " + tmpIntersectLineCircle[0][1] + ")"
Case 2; Print "Line goes through the circle at (" + tmpIntersectLineCircle[0][0] + ", " + tmpIntersectLineCircle[0][1] + ") and (" + ..
tmpIntersectLineCircle[1][0] + ", " + tmpIntersectLineCircle[1][1] + ")"
EndSelect
Function GetIntersectionLineCircle#[][]( pLineStart#[], pLineEnd#[], pCircleCenter#[], pCircleRadius# )
Local tmpIntersections#[][]
Local p# = pCircleCenter[0], q# = pCircleCenter[1]
Local m# = (pLineEnd[1]-pLineStart[1])/(pLineEnd[0]-pLineStart[0])
Local r# = pCircleRadius
Local t# = pLineEnd[1]- (m*pLineEnd[0])
Local s# = t-q
Local a# = m^2 + 1, b# = (2*m*s) - (2*p), c# = s^2 + p^2 - (r^2)
Local bsqminfourac# = b^2-4*a*c
If bsqminfourac > 0 Then
Local x1# = ((-b)+Sqr(bsqminfourac))/(2*a)
Local x2# = ((-b)-Sqr(bsqminfourac))/(2*a)
tmpIntersections = [[x1,(m*x1)+t],[x2,(m*x2)+t]]
ElseIf bsqminfourac = 0 Then
tmpIntersections = [[(-b)/(2*a),(-b*m)/(2*a)+t]]
EndIf
Return tmpIntersections
EndFunction
I've added a timer, so you can see how fast it is... On my Intel Core 2 Duo 2.00Ghz, it takes approx 0.002ms to find the two points of intersection!
Who say's high school maths doesn't come in handy?