Find the angle of a point from origion

11-12-2001, 02:46 AM
ok this is a bit of a maths question
I have the origion point. And another point(P).
I am trying to find the angle between the points
I have formed an equation which doesnt seem to work but i have no idea why.
Degrees = Atn(Abs(Py/ Px))

basically this is degrees = ARCTAN(|pY/pX|)

11-12-2001, 06:05 AM
I think we need to see your entire function in order to tell you whats wrong with it.


11-12-2001, 06:08 AM
The VB sin/cos/tan functions all work with Radians instead of degrees, so have to convert in/output from radians to degrees yourself.

11-12-2001, 07:26 AM
To convert from radians to degrees and back, here are the formulae:

pi = 3.14159
Radians = (Degrees/360)* 2 * pi
Degrees = (Radians * 180) / pi

11-12-2001, 09:03 AM
Actually, you'd need three points to get an angle. 2 points designates a line.

If you're using the origin as one point, then it's possible.

11-12-2001, 09:06 AM
Actually, Mill, I think andrewo is trying to find the actual angle of a point in comparison to the origin. For example, if the origin is at 0,0 and the point in question is at 3,3 then the angle would be 45 degrees or pi/4 radians. If the point in question is at -5, 0 then the angle is 180 degrees or pi radians.

Andrewo, please let me know if I assumed incorrectly.

11-12-2001, 09:14 AM
My bad - I misread a little bit.

Assuming that you're trying to find the angle from the point, using the origin, to the horizontal, then your function would yield the correct value, in radians.

As others have mentioned, VB measures angles in radians. There are 2*PI radians in a circle, and 360 degrees in a circle, so you'd have to convert from radians to degrees to get the value that you want.

Degrees = Atn(Abs(Py/ Px)) * 360 / (2*PI) or
Degrees = Atn(Abs(Py/ Px)) * 180 / PI

11-12-2001, 06:33 PM
Yeah thats right teric

The equation i posted at the top should work in theory but i cant see why it wont work

yea i know about the radians, there shouldnt be any problem with that

i dont think i have to convert degrees to radians as i am just using radians as it makes the code simpler

11-13-2001, 04:25 AM
Maybe you can define what you mean by "won't work".

I did a test and it seems to work for me, although the value in degrees is always between 0-90 but that is expected due to the ABS term.....

Here is my code:

<pre><font color=blue>Option Explicit</font color=blue>

<font color=blue>Private Sub</font color=blue> Form_Click()
Unload Me
<font color=blue>End Sub</font color=blue>

<font color=blue>Private Sub</font color=blue> Form_Load()
Me.Height = 5000
Me.Width = 5000
Me.ScaleLeft = -100
Me.ScaleWidth = 200
Me.ScaleTop = 100
Me.ScaleHeight = -200
Me.DrawMode = 7 <font color=green>'xor pen</font color=green>
Me.BackColor = 0
Me.ForeColor = vbWhite
<font color=blue>End Sub</font color=blue>

<font color=blue>Private Sub</font color=blue> Form_MouseMove(Button <font color=blue>As Integer</font color=blue>, Shift <font color=blue>As Integer</font color=blue>, X <font color=blue>As Single</font color=blue>, Y <font color=blue>As Single</font color=blue>)
<font color=blue>Static</font color=blue> OldX!, OldY!
Me.Caption = FindAngle(X, Y)
Me.Line (0, 0)-(OldX, OldY)
Me.Line (0, 0)-(X, Y)
OldX = X
OldY = Y
<font color=blue>End Sub</font color=blue>

<font color=blue>Private Function</font color=blue> FindAngle(<font color=blue>ByVal</font color=blue> X <font color=blue>As Single</font color=blue>, Y <font color=blue>As Single</font color=blue>) <font color=blue>As Double</font color=blue>
FindAngle = Atn(Abs(Y / X)) * 180 / 3.141
<font color=blue>End Function</font color=blue>

11-13-2001, 10:10 PM
thanx but got it workin :)

i ended up just putting the 0,0 in the middle of the screen

11-16-2001, 02:12 AM
GetCursorPos MousePoint
ScreenToClient Screen.hwnd, MousePoint

fx = MousePoint.X - Screen.ScaleWidth / 2
fy = MousePoint.Y - Screen.ScaleHeight / 2

If fx &lt; 0 And fy &lt; 0 Or fx &lt; 0 And fy &gt; 0 Then
Quadrent = pi
Quadrent = 0
End If
Facing = Atn((fy / fx)) + Quadrent 'Text1.Text / 180 * pi 'Quadrent ' * pi '+ (pi / 2)
End Sub

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum