Easy way to get an angle in X-Y plane.

Easy way to get an angle in X-Y plane.

Anonymous
Not applicable
359 Views
6 Replies
Message 1 of 7

Easy way to get an angle in X-Y plane.

Anonymous
Not applicable
Hi,

I've been trying to get an angle between two points, and this

calcAngle = Atn(deltaY1 / deltaX1)

doesn't work since it only give angle between 0 and 90°, and I need to
figure out the actual direction.

I even try creating a line then get the angle property of this line, and
that works perfectly, but it will slow the process to a crawl.


Any suggestions?


Thanks.
0 Likes
360 Views
6 Replies
Replies (6)
Message 2 of 7

Anonymous
Not applicable
This might hurt a bit but ....

Draw a line between the two points.

Joe
--
0 Likes
Message 3 of 7

Anonymous
Not applicable
Have you tried the "AngleFromXAxis" method?

The snip below is directly from the help file.

Gary


' This example finds the angle, in radians, between the X axis
' and a line defined by two points.

Dim pt1(0 To 2) As Double
Dim pt2(0 To 2) As Double
Dim retAngle As Double

pt1(0) = 2: pt1(1) = 5: pt1(2) = 0
pt2(0) = 5: pt2(1) = 2: pt2(2) = 0

' Return the angle

retAngle = ThisDrawing.Utility.AngleFromXAxis(pt1, pt2)

' Create the line for a visual reference
Dim lineObj As AcadLine
Set lineObj = ThisDrawing.ModelSpace.AddLine(pt1, pt2)
ZoomAll

' Display the angle found
MsgBox "The angle in radians between the X axis and the line is " &
retAngle, , "AngleFromXAxis Example"
0 Likes
Message 4 of 7

Anonymous
Not applicable
Thanks, but if you read my message I figured it out
myself allready, but it slows the process, creating a line for nothing, and
erasing it afterward just to get an angle.

 

 

 


style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
This
might hurt a bit but ....

Draw a line between the two points.

Joe
--

0 Likes
Message 5 of 7

Anonymous
Not applicable
Ooops, sorry missed that. However it's still the easiest method and all things considered, probably the best overall method as well.

Joe
--
0 Likes
Message 6 of 7

Anonymous
Not applicable
I'm not very good at doing things the easy (translate: smart) way, so here's
the brute-force approach.
It's in a function so it should be plug-n-play for you.

Disclaimer: I made every effort to be correct, but do not design any
100-story buildings using this code until this function has been tested to
YOUR satisfaction. You bear all responsibility for the use of this code,
including, but not limited to, structural collapse, explosion,
dismemberment, death, tornado, unintentional nuclear war.

James

Sub test_atn()
Debug.Print better_Atn(0, 0) * 180 / 3.14
Debug.Print better_Atn(1, -1) * 180 / 3.14
End Sub

Function better_Atn(deltaY1 As Double, deltaX1 As Double)

Dim PI As Double: PI = 4 * Atn(1)
Dim calcAngle As Double

If deltaX1 = 0 Then
'on vertical axis
If deltaY1 > 0 Then
calcAngle = PI / 2
ElseIf deltaY1 < 0 Then
calcAngle = 3 / 2 * PI
Else 'deltaX1 = 0 and deltaY1 = 0
MsgBox "Zero-length vector. " & _
"Angle can not be determined. You " & _
"should have the function throw an error here."
End If
ElseIf deltaX1 > 0 Then
'quadrants I and IV
If deltaY1 >= 0 Then 'quadrant I
calcAngle = Atn(deltaY1 / deltaX1)
Else 'quadrant IV
calcAngle = 2 * PI + Atn(deltaY1 / deltaX1)
End If
ElseIf deltaX1 < 0 Then
'quadrants II and III
calcAngle = PI + Atn(deltaY1 / deltaX1)
End If

better_Atn = calcAngle

End Function
0 Likes
Message 7 of 7

Anonymous
Not applicable
what do you know... Utility.AngleFromXAxis... the *smart* way....

well... at least *my* function flagged a zero-length line instead of just
assuming it was at a 0deg angle...


well... back to my other invention... I don't want to give away too much,
but I can tell you it's this circular thingy that's great for moving heavy
objects...

James
0 Likes