Hi all,
I've got some complicated physics equations I need to use in VB, and I need to figure out the angle of a line, from 0° to 180°...
Here is a screen shot of the line:
Printable View
Hi all,
I've got some complicated physics equations I need to use in VB, and I need to figure out the angle of a line, from 0° to 180°...
Here is a screen shot of the line:
what data are you given? startpoint and end point or line?
yes? you have the starting point, ending point of the line, it's X1, X2, Y1, and Y2...
VB Code:
Private Function GetAngle(X1 As Long, Y1 As Long, X2 As Long, Y2 As Long) As Double Dim PI As Double Dim theta As Double PI = Atn(1) * 4 'get angle of line in radians 'deltay/deltax = tan(angle) theta = Atn((Y2 - Y1) / (X2 - X1)) 'convert to degrees 'there are 180 degrees per PI radians GetAngle = theta * 180 / PI End Function
umm, what is Atn function? sorry i forgot.Quote:
Originally Posted by moeur
got it, inverse of TANGENT!!
:ehh: am i right??
Atn is called ArcTangent (Gupta is correct)
Atn(x) is the angle who's Tangent is x
mouer, sorry this time you confused me. is Atn different from Atn(x)??Quote:
Originally Posted by moeur
No, they're the same thing. He was just using it in a sentence.
thnx moeur, mendhakQuote:
Originally Posted by mendhak
Moeur,
I dont think this is working correctly because this happens:
that should be 180 degrees
What are your X1, Y1 and X2,Y2?
here they are
they can change when the user clicks and drags the line:
VB Code:
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) If Moving Then Line1.X2 = X Line1.Y2 = Y angle = GetAngle(Line1.Y2, Line1.Y1, Line1.X2, Line1.X1) lblDegrees.Caption = angle & "°" End If End Sub
Try putting the parameters in the right order ;)
VB Code:
angle = GetAngle(Line1.X1, Line1.Y1, Line1.X2, Line1.Y2)
yeah that'd probably help :wave:
ok now it works better, but on the right side of 90 degrees it goes negatively...
use the abs() ? even though it shouldnt return negative...
Or swap x2 and x1 of x2>x1, and the same for y
the negative is not the only problem, when you move the line to the right of the 90 degrees it should return numbers from 91 - 180 degrees...Quote:
Originally Posted by |2eM!x
I tried this also and I get the same exact thing for whichever I try :confused:Quote:
Originally Posted by dglienna
VB Code:
Private Function GetAngle(X1 As Long, Y1 As Long, X2 As Long, Y2 As Long) As Double Dim PI As Double Dim theta As Double Dim deltaX As Long Dim deltaY As Long 'get angle of line in radians 'deltay/deltax = tan(angle) deltaY = Y1 - Y2 deltaX = X2 - X1 PI = Atn(1) * 4 'adjust to our coordinate system If deltaX = 0 Then 'fix division by zero errors If Y1 > Y2 Then theta = 180 Else theta = 90 End If ElseIf deltaX > 0 And deltaY >= 0 Then '1st quadrant theta = Atn((Y1 - Y2) / (X2 - X1)) ElseIf deltaX < 0 And deltaY >= 0 Then '2nd quadrant theta = Atn((Y1 - Y2) / (X2 - X1)) + PI ElseIf deltaX < 0 And deltaY < 0 Then '3rd quadrant theta = Atn((Y1 - Y2) / (X2 - X1)) + PI ElseIf deltaX > 0 And deltaY < 0 Then '4th quadrant theta = Atn((Y1 - Y2) / (X2 - X1)) + 2 * PI End If 'convert to degrees 'there are 180 degrees per PI radians 'if theta < 0 then theta = GetAngle = theta * 180 / PI End Function
great thanks moeur :thumb:
moeur, could you possible explain that? I thought that it had something to do with quadrants, but didn't want to try to guess at the answer. If not, that's ok, also. I was thinking that 0 degrees was at 9'oclock, and that was the issue. Maybe I was close.
The problem is how the ArcTangent is defined
-90° < Atn(x) < 90°
for
-infinity < x < infinity
But that is not what we wanted so I had to adjust
Well, I wish that I could have seen an explanation, but I found this link, and it didn't help much.
http://mathworld.wolfram.com/InverseTangent.html
I looked up tangent, and saw the same images.
http://mathworld.wolfram.com/Tangent.html
I guess I don't know what I'm looking at.
I used to know the formula to draw a circle with COS and SIN, so thought this wouldn't be too hard to grasp