-
I have two points, X1,Y1 and X2,Y2. I want to find the value from true north of X1,Y1 that X2,Y2 is.
How do I do it? I know you have to find the distance between the points, eg. X1 - X2, Y1 - Y2, but beyond that I have no idea. ;-)
If you don't know what I mean, then look here:
........o
....|
....o/
| is true north, / is the angle. o are the points. Ignore the .'s. =)
-Git
[Edited by git on 10-08-2000 at 05:10 AM]
-
SOHCAHTOA
I have tried to depict your question in the "code" below.
Code:
N---o
| /
|t/
|/
x
where x is the point represented by x1,y1 and o is the point x2,y2. t is the angle you are after, N represents North.
The line xN has magnitude y2-y1
The line No has magnitude x2-x1
Trigonometery tells us that:
tan t = No / xN
Therefor, t = arctan (No / xN)
Also consider the case:
N
|
|
x t
|\
| \
S--o
Here, t is the ange we want, but the way to get it is to work out the angle Sxo and subtract this from 180. Again, trig tells us:
tan Sxo = So/Sx
=> Sxo = arctan(So/Sx)
and t = 180 - arctan(So/Sx)
Now in VB you would need to do something like the
following. Do not get put off by the fact that VB's 0,0
coordinate is by default at the top left of the screen.
This can be ignored by our calculations. Also, assuming
you want to report the ange as -180 to 180 degrees (where -
180/180 represent due south) then the following sample code
should help you.
Code:
Option Explicit
Dim x1 As Long
Dim y1 As Long
Dim x2 As Long
Dim y2 As Long
Private Sub Command1_Click()
Dim xdiff As Long
Dim ydiff As Long
Dim angle As Single
Dim pi As Single
pi = Atn(1) * 4
xdiff = Abs(x1 - x2)
ydiff = Abs(y2 - y1)
angle = Atn(xdiff / ydiff)
angle = angle * 180 / pi
If y2 > y1 Then angle = 180 - angle
If x2 < x1 Then angle = 0 - angle
Debug.Print angle
x1 = 0
y1 = 0
x2 = 0
y2 = 0
End Sub
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If x1 = 0 Then
Cls
x1 = X
y1 = Y
Else
x2 = X
y2 = Y
End If
Circle (X, Y), 50, 0
End Sub
Please don't curse me if I've screwed something up as it's
late and I probably should have thought twice about
answering...But hey, I like maths so what can I say :)
Regards
-
Didn't quite work... when I clicked above the soldier (in my game) it was 180, and below it game me an overflow, cause the diff's had values of 0's when it tried to divide.
-Git
-
Well fix it then :)
I can see that I didn't account for the case where we were on the line of due north, but surely the example wasn't a complete loss?
You can easily check to see if x1=x2 and if so, compare the
y values. If x1=x2 then the angle will be either 0 or 180,
so you have to hard code this test.
I was of the assumption that the problem you were having
was in all the other possible places to click. You
wouldn't have needed anyones help to figure out that if you
click directly below, it is 180 deg, and directly above is
0 deg?
Examples posted here are meant to get you started on your
own discovery of a solution. I for one never assume I can
provide code that is 100% guaranteed to fit your project :)
I believe the math in my example should get you going..If
not, well then I guess it is beyond us both huh ?
hehe