• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 10395
  • Last Modified:

Northing Easting Calculations..

Im using a Postcode component giving me an OS grid Easting and Northing positions. I have 2 coordinates, I need to measure the distance between the 2 points and also get the direction of travel. I have sussed out the direction problem (I think) but im struggling with the direction travelled. I need the direction to be in degrees so I can then calculate if they are travelling North, South or North-East etc?

Here is the code for distance:

Public Function Distance(x1 As Double, y1 As Double, x2 As Double, y2 As Double) As Double

Dim lowX As Double
Dim lowY As Double
Dim highX As Double
Dim highY As Double

'Get highest first x
If x1 > x2 Then
    highX = x1
    lowX = x2
Else
    highX = x2
    lowX = x1
End If

'Get highest first y
If y1 > y2 Then
    highY = y1
    lowY = y2
Else
    highY = y2
    lowY = y1
End If

dDistance = Format(Sqr(((highX - lowX) ^ 2 + (highY - lowY) ^ 2)) / 1609.3, "0.00")

End Function

I would be gratefully of any help on this one please?
0
Reddz
Asked:
Reddz
1 Solution
 
BrosCommented:
If you dont nees the highX and highY i can tell you that thay dont make any diffrence

becouse for example
x1 = 625
x2 = 45

(x1 - x2) ^ 2 =>
(625 - 45) ^ 2 = 336400

vice versa

(x2 - x1) ^ 2 =>
(45 - 625) ^ 2 = 336400

---
so if you make an calculation x^2 you will allways get the resualt x>=0 but x can be both x and -x
---

anyway im not so god at trigonometri " yet :) "

but i think you can use this code:

'-------------------Functions----------------------------
Public Function aTan(Cosine)
'Inverse tangent in degrees
aTan = Atn(Cosine) / (3.14159265358979 / 180)
End Function

Public Function TanD(Degrees)
'Tangent in degrees
Tane = Tan(Degrees * (3.14159265358979 / 180))
End Function
'---------------------------------------------------

angle = aTan((Line1.x2 - Line1.x1) / (Line1.y2 - Line1.y1))

i hope it will be usefull anyway...
0
 
BrosCommented:
sorry, my mistake

change this:
angle = aTan((Line1.x2 - Line1.x1) / (Line1.y2 - Line1.y1))

to:
angle = aTan((x2 - x1) / (y2 - y1))
0
 
navneet77Commented:
you can find the slope by (y2-y1)/(x2-x1) which will be tangent. To find the degree from that use atn() and than convert them to degree

pi = 3.14159265358979
t = (Y2 - Y1) / (X2 - X1)
degree = Atn(t) * (180 / pi)

now if degree is greater than 45 degree he is traveling northward.

Hope that helps
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
redfordbCommented:
             B (s:t)
              .
             /|
            /b|
           /  |
          /   |
         /    |
        /a   c|  
       .------.
       A      C
      (m;n)  (s;n)
 
Given position A (m;n) and B(s;t), it follows that position C has co-ordinates (s;n).

The length of AC is therefore s-m
The length of BC is therefore t-n

The length of AB can be determines using pythagoras ie.
AB = Sqr(AC^2 + BC^2)
 
Add the following function to your project...

Function ASin(value As Double) As Double
    If Abs(value) <> 1 Then
        ASin = Atn(value / Sqr(1 - value * value))
    Else
        ASin = 1.5707963267949 * Sgn(value)
    End If
End Function

Define a variable to hold the angle na done for the bearing ie.

Dim Angle   As Double
Dim Bearing As Double

To determine angle a, call this function like this :
Angle = ASin(BC / AB)

Note that the value in "Angle" is in radians to convert it to degrees, multiply it by 57.29578 ie.
Angle = Angle * 57.29578

Now you have angle a. But thats the angle inside the triangle, you need to get the complentary angle to determine the bearing ie.

If Ax < Bx Then
    If Ay < By Then
       Bearing = 90 - Angle
    Else
       Bearing = 180 - Angle
    End If
Else
    If Ay < By Then
       Bearing = 360 - Angle
    Else
       Bearing = 270 - Angle
    End If
End If

And now you have the distance between the points, as well as the angle/bearing.








0
 
ReddzAuthor Commented:
Thank Guy I have sussed it out with a little more looking around the net, here's what ive come up with...


Dim Pi As Double
Dim Bearing As Double

'calculate Pi
    Pi = Atn(1) * 4

'Calculate the Distance Travelled
    dDistance = Sqr(((x2 - x1) ^ 2 + (y2 - y1) ^ 2))

'Convert Distance from metres to miles
    dDistance = dDistance / 1609.3

'Round Distance to nearest Mile .1
    dDistance = Round(dDistance, 1)


'Calculate Direction Travelled in degrees

    dx = x2 - x1
    dy = y2 - y1

'Check which quadrant we are in and find the degrees
    If dx > 0 Then
        Bearing = 90 - (180 / Pi) * Atn(dy / dx)
    End If
   
    If dx < 0 Then
        Bearing = 270 - (180 / Pi) * Atn(dy / dx)
    End If
   
    If dx = 0 Then
        If dy > 0 Then Bearing = 0
            If dy < 0 Then Bearing = 180
                If dy = 0 Then
                    dDirection = "None"
                    Exit Function
                End If
    End If
   
‘From the degrees work out if we are travelling North, South etc

    If Bearing > 0 And Bearing < 22.5 Then
        dDirection = "North"
    End If
   
    If Bearing > 22.5 And Bearing < 67.5 Then
        dDirection = "North East"
    End If
   
    If Bearing > 67.5 And Bearing < 112.5 Then
        dDirection = "East"
    End If
   
    If Bearing > 112.5 And Bearing < 157.5 Then
        dDirection = "South East"
    End If

    If Bearing > 157.5 And Bearing < 202.5 Then
        dDirection = "South"
    End If
   
    If Bearing > 202.5 And Bearing < 247.5 Then
        dDirection = "South West"
    End If
   
    If Bearing > 247.5 And Bearing < 292.5 Then
        dDirection = "West"
    End If
   
    If Bearing > 292.5 And Bearing < 337.5 Then
        dDirection = "North West"
    End If
   
    If Bearing > 337.5 And Bearing < 360 Then
        dDirection = "North"
    End If
0
 
ReddzAuthor Commented:
Thank Guy I have sussed it out with a little more looking around the net, here's what ive come up with...


Dim Pi As Double
Dim Bearing As Double

'calculate Pi
    Pi = Atn(1) * 4

'Calculate the Distance Travelled
    dDistance = Sqr(((x2 - x1) ^ 2 + (y2 - y1) ^ 2))

'Convert Distance from metres to miles
    dDistance = dDistance / 1609.3

'Round Distance to nearest Mile .1
    dDistance = Round(dDistance, 1)


'Calculate Direction Travelled in degrees

    dx = x2 - x1
    dy = y2 - y1

'Check which quadrant we are in and find the degrees
    If dx > 0 Then
        Bearing = 90 - (180 / Pi) * Atn(dy / dx)
    End If
   
    If dx < 0 Then
        Bearing = 270 - (180 / Pi) * Atn(dy / dx)
    End If
   
    If dx = 0 Then
        If dy > 0 Then Bearing = 0
            If dy < 0 Then Bearing = 180
                If dy = 0 Then
                    dDirection = "None"
                    Exit Function
                End If
    End If
   
‘From the degrees work out if we are travelling North, South etc

    If Bearing > 0 And Bearing < 22.5 Then
        dDirection = "North"
    End If
   
    If Bearing > 22.5 And Bearing < 67.5 Then
        dDirection = "North East"
    End If
   
    If Bearing > 67.5 And Bearing < 112.5 Then
        dDirection = "East"
    End If
   
    If Bearing > 112.5 And Bearing < 157.5 Then
        dDirection = "South East"
    End If

    If Bearing > 157.5 And Bearing < 202.5 Then
        dDirection = "South"
    End If
   
    If Bearing > 202.5 And Bearing < 247.5 Then
        dDirection = "South West"
    End If
   
    If Bearing > 247.5 And Bearing < 292.5 Then
        dDirection = "West"
    End If
   
    If Bearing > 292.5 And Bearing < 337.5 Then
        dDirection = "North West"
    End If
   
    If Bearing > 337.5 And Bearing < 360 Then
        dDirection = "North"
    End If
0
 
CleanupPingCommented:
Reddz:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Experts: Post your closing recommendations!  Who deserves points here?
0
 
DanRollinsCommented:
Reddz, an EE Moderator will handle this for you.
Moderator, my recommended disposition is:

    Refund points and save as a 0-pt PAQ.

DanRollins -- EE database cleanup volunteer
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now