Click to See Complete Forum and Search --> : [RESOLVED] Angles of Lines Tangent to Circle
sgrya1
Oct 2nd, 2006, 06:29 PM
Hi all.
Anyone here able help me out?
I need to find the angles of the two lines that are tangent to a circle and pass through the origin. Problem is I need the angles to be an angular direction ie 0 to 2pi from the origin which is obviously be dependant on the location of the circle relative to the origin.
I can work out the angles of the lines (0 to +-pi) but not the angular directions.
Guess I have to put in quite a few ifs and buts about the positions of the tangent points.
krtxmrtz
Oct 3rd, 2006, 03:19 AM
The angles are
Theta1 = Atn [(ab + rSqr(K)) / M]
Theta2 = Atn [(ab - rSqr(K)) / M]
where
K = a2 + b2 - r2
M = a2 - r2
r is the radius of the circle and (a,b) the coordinates of its center.
Because the VB Atn function returns values between +Pi/2 and -Pi/2 you must indeed add some ifs for the various cases.
First, to avoid divisions by zero, the case a = r must be dealt with. In this case, Theta1 = Pi/2 and Theta 2 turns out to be an expression of the type 0/0 which can be determined by calculating the limit:
Theta2 = lim(a -> r) Atn[(ab - rSqr(K)) / M] = b/r - r/b
Now:
If a>0 and b>0, we're done.
If a>0 and b<0, report the angles as 2*Pi + Theta1 and 2*Pi + Theta2
If a<0 the angles are Pi - Theta1 and Pi - Theta2 regardless of the sign of b.
Check this out carefully, I can't guarantee there aren't any mistakes.
sgrya1
Oct 3rd, 2006, 07:10 AM
Thanks krtxmrtz ,
I had to change the if statement for
If a<0 the angles are Pi - Theta1 and Pi - Theta2
to
If a<0 the angles are Pi + Theta1 and Pi + Theta2
Can you or someone help me debug it a bit further please. Out of my league. Didn't even have your equations correct.
Attached is a simple code with graphical output which should help. I'm still having problems with :
for r = .5 and
a = 0.5, b = 1
a = 0, b = 1
a = -0.5, b = 1
and
a = 0.5, b = -1
a = 0, b = -1
a = - 0.5, b = -1
krtxmrtz
Oct 3rd, 2006, 10:04 AM
I know it's a bit messy but I think it's working, check it out. Incidentally I found a few mistakes in my derivation above.
The phrase you referred to in my signature is not latin but portuguese and I came across it in a Brazilian web site. It means, "he who can't forgive is destroying the bridge over which he himself must cross". I think it's wise.
sgrya1
Oct 3rd, 2006, 11:53 AM
:) OMG. Who was that man?
I can't thank you enough for that.
You've even given coding advice.
There's no way I would have gotten past that one.
krtxmrtz
Oct 3rd, 2006, 01:21 PM
:) OMG. Who was that man?
I can't thank you enough for that.
You've even given coding advice.
There's no way I would have gotten past that one.
Well, then I'd appreciate some credit (-> "rate this post") ;)
At any rate, I'm sorry I didn't have much time to make it tidier, probably it can all be arranged in a much simpler fashion.
sgrya1
Oct 3rd, 2006, 05:13 PM
Sorry, didn't realise I could do that.
Make it tidier? Man, that is a one liner and crystal clear compared to what I have put together. You'd be horrified. Self taught and obvious. Thank god computers these days are fast enough not to care about my coding deficiencies.
Thanks again. Wish I could repay the favour but sadly seems unlikely.
krtxmrtz
Oct 4th, 2006, 02:49 AM
I think this is a clearer approach (not too different, though):
Let K1 and K2 the tangents of the 2 angles, i.e. the slopes of the tangent lines:
K1 = [ab + r*Sqr(a2 + b2 - r2)] / (a2 - r2)
K2 = [ab - r*Sqr(a2 + b2 - r2)] / (a2 - r2)
The idea is to calculate the coordinates of the two points of tangency and from these derive the angles (but you don't need the angles for the plot, once you have the coordinates).
Now, first of all consider the special case a=r. Now K2 tends to infinity and
K1 = 0.5*(b/r - r/b) if a>0
K1 = -0.5*(b/r - r/b) if a<0
For K2 -> infinity the coordinates are:
x = 0
y = b
For the case K1 with a=r and all the other cases (a <> r) the coordinates are calculated from K:
x = (a + bK1)
y = K1x
x = (a + bK2)
y = K2x
krtxmrtz
Oct 4th, 2006, 02:57 AM
...and I have finally dug out this code snippet from the mess of my hard disk.
Function angvec(X, Y, Success) As Single
'Returns the angle from 0 to 2*Pi between the x axis
'and the vector of coordinates X,Y
'Pi has been declared elsewhere
Success= False
If X = 0 And Y = 0 Then Exit Function
Success = True
If X = 0 Then
If Y > 0 Then
angvec = Pi / 2
ElseIf Y < 0 Then
angvec = 3 * Pi / 2
End If
Exit Function
Else
angvec = Atn(Y / X)
If X > 0 And Y < 0 Then
angvec = 2 * Pi + angvec
ElseIf X < 0 Then
angvec = Pi + angvec
End If
End If
End Function
sgrya1
Oct 17th, 2006, 11:19 AM
krtxmrtz,
Would you be able to have a last look at the tangent program. I had a good go at updating it such that the tangent points are calculated according to the method you suggested out but failed at it so it's still pretty much in the original state.
Still have a problem that when A=0 (or more particularly when the circle crosses the y axis) I get one angle reporting incorrectly. I can't get my head around it.
Would you please help tweak it for me?
sgrya1
Oct 17th, 2006, 04:49 PM
Wasn't that difficult and should have given it some more thought. Thanks again.
k = A * A + B * B - r * r
M = A * A - r * r
If Abs(A) = Abs(r) Then
If B > 0 Then
angle1 = 0.5 * pi
Else
angle1 = 1.5 * pi
End If
angle2 = Math.Atn(0.5 * (B / r - r / B))
If A > 0 And B < 0 Then
angle2 = 2 * pi + angle2
ElseIf A < 0 Then
angle2 = pi - angle2
End If
Else
angle1 = Math.Atn((A * B + r * Sqr(k)) / M)
angle2 = Math.Atn((A * B - r * Sqr(k)) / M)
If A - r < 0 And A + r > 0 And B > 0 Then
angle1 = pi + angle1
ElseIf A - r < 0 And A + r > 0 And B < 0 Then
angle1 = 2 * pi + angle1
angle2 = pi + angle2
ElseIf A > 0 And B < 0 Then
angle1 = 2 * pi + angle1
angle2 = 2 * pi + angle2
ElseIf A < 0 Then
angle1 = pi + angle1
angle2 = pi + angle2
End If
End If
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.