Calculate difference in Azimuth

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I need to calculate the difference in reported headings (azimuth), with a 360
degree max. I am currently doing it by subtracting the lower number from the
greater.
IIf ([heading1]>[heading2], [heading1]-[heading2], [heading2]-[heading1]).

That works just fine but now someone wants to see it in the shortest degree
around. In current calc heading1 is 332 degrees and heading2 is 14 degrees my
result is 318 degree difference. But that is calculating it the long way
around. Is there a way to get the 42 degree result?

Thanks
Dave
 
Why do your headings contain data?

A heading would typically be the same as the field which displays data in
that column. field names should not be 'data'.

More details please. I think your database structure may be badly flawed.
 
nuttingd said:
I need to calculate the difference in reported headings (azimuth),
with a 360 degree max. I am currently doing it by subtracting the
lower number from the greater.
IIf ([heading1]>[heading2], [heading1]-[heading2],
[heading2]-[heading1]).

That works just fine but now someone wants to see it in the shortest
degree around. In current calc heading1 is 332 degrees and heading2
is 14 degrees my result is 318 degree difference. But that is
calculating it the long way around. Is there a way to get the 42
degree result?

I don't know anything about azimuths, but I'd guess you could just
compare the result of your existing expression to 180 and, if it's
greater than 180, use 360 - the calculated value. So if you have to do
it as a single expression ...

=IIf(IIf([H1]>[H2], [H1]-[H2], [H2]-[H1]) > 180,
360 - IIf([H1]>[H2], [H1]-[H2], [H2]-[H1]),
IIf([H1]>[H2], [H1]-[H2], [H2]-[H1]))

That's awfully ugly and inefficient-looking, though. Unless there's a
way to simplify it, I'd write a function and use that instead:

'----- start of code -----
Public Function AzimuthDifference( _
Heading1 As Variant, Heading2 As Variant) _
As Variant

Dim lngDifference As Long

' Check for Null input; should yield Null output.
If IsNull(Heading1) Or IsNull(Heading2) Then
Exit Function
End If

' Validate arguments.
If Heading1 > 360 Or Heading2 > 360 Then
Err.Raise 5
End If
If Heading1 < 0 Or Heading2 < 0 Then
Err.Raise 5
End If

' Calculate the difference.
If Heading1 > Heading2 Then
lngDifference = Heading1 - Heading2
Else
lngDifference = Heading2 - Heading1
End If

' Adjust result for shortest angular difference.
If lngDifference > 180 Then
AzimuthDifference = 360 - lngDifference
Else
AzimuthDifference = lngDifference
End If

End Function
'----- end of code -----
 
Did you ever stop to thing that Heading1 and Heading2 might be the names of
fields, Rick? As in fields that store information about headings
(directions)?

I'll agree that Heading1 and Heading2 implies a non-normalized database, but
sometimes you're a little too quick to pounce on people!

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Rick B said:
Why do your headings contain data?

A heading would typically be the same as the field which displays data in
that column. field names should not be 'data'.

More details please. I think your database structure may be badly flawed.

--
Rick B



nuttingd said:
I need to calculate the difference in reported headings (azimuth), with
a
360
degree max. I am currently doing it by subtracting the lower number from the
greater.
IIf ([heading1]>[heading2], [heading1]-[heading2], [heading2]-[heading1]).

That works just fine but now someone wants to see it in the shortest degree
around. In current calc heading1 is 332 degrees and heading2 is 14
degrees
my
result is 318 degree difference. But that is calculating it the long way
around. Is there a way to get the 42 degree result?

Thanks
Dave
 
No pouncing. that is why I asked for more details. Also why I said "I
believe your structure..."

In this case, it does appear these were names and my assumption was not
correct.


--
Rick B



Douglas J Steele said:
Did you ever stop to thing that Heading1 and Heading2 might be the names of
fields, Rick? As in fields that store information about headings
(directions)?

I'll agree that Heading1 and Heading2 implies a non-normalized database, but
sometimes you're a little too quick to pounce on people!

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Rick B said:
Why do your headings contain data?

A heading would typically be the same as the field which displays data in
that column. field names should not be 'data'.

More details please. I think your database structure may be badly flawed.
with
a
360
degree max. I am currently doing it by subtracting the lower number
from
the
greater.
IIf ([heading1]>[heading2], [heading1]-[heading2], [heading2]-[heading1]).

That works just fine but now someone wants to see it in the shortest degree
around. In current calc heading1 is 332 degrees and heading2 is 14
degrees
my
result is 318 degree difference. But that is calculating it the long way
around. Is there a way to get the 42 degree result?

Thanks
Dave
 
Thanks Dirk
This looks good, I'll give it a try. As for the other comments. I have
different sources reporting direction and location of an object. Heading1 and
Heading2 are my field names containing the direction the object is moving,
reported from each source. Anyway Dirk's code looks like it might work for
me, I'll let you know.

Thanks again for all the comments.


Dirk Goldgar said:
nuttingd said:
I need to calculate the difference in reported headings (azimuth),
with a 360 degree max. I am currently doing it by subtracting the
lower number from the greater.
IIf ([heading1]>[heading2], [heading1]-[heading2],
[heading2]-[heading1]).

That works just fine but now someone wants to see it in the shortest
degree around. In current calc heading1 is 332 degrees and heading2
is 14 degrees my result is 318 degree difference. But that is
calculating it the long way around. Is there a way to get the 42
degree result?

I don't know anything about azimuths, but I'd guess you could just
compare the result of your existing expression to 180 and, if it's
greater than 180, use 360 - the calculated value. So if you have to do
it as a single expression ...

=IIf(IIf([H1]>[H2], [H1]-[H2], [H2]-[H1]) > 180,
360 - IIf([H1]>[H2], [H1]-[H2], [H2]-[H1]),
IIf([H1]>[H2], [H1]-[H2], [H2]-[H1]))

That's awfully ugly and inefficient-looking, though. Unless there's a
way to simplify it, I'd write a function and use that instead:

'----- start of code -----
Public Function AzimuthDifference( _
Heading1 As Variant, Heading2 As Variant) _
As Variant

Dim lngDifference As Long

' Check for Null input; should yield Null output.
If IsNull(Heading1) Or IsNull(Heading2) Then
Exit Function
End If

' Validate arguments.
If Heading1 > 360 Or Heading2 > 360 Then
Err.Raise 5
End If
If Heading1 < 0 Or Heading2 < 0 Then
Err.Raise 5
End If

' Calculate the difference.
If Heading1 > Heading2 Then
lngDifference = Heading1 - Heading2
Else
lngDifference = Heading2 - Heading1
End If

' Adjust result for shortest angular difference.
If lngDifference > 180 Then
AzimuthDifference = 360 - lngDifference
Else
AzimuthDifference = lngDifference
End If

End Function
'----- end of code -----

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 
I'm commenting specifically to your first line: "Why do your headings
contain data?"

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)



Rick B said:
No pouncing. that is why I asked for more details. Also why I said "I
believe your structure..."

In this case, it does appear these were names and my assumption was not
correct.


--
Rick B



Douglas J Steele said:
Did you ever stop to thing that Heading1 and Heading2 might be the names of
fields, Rick? As in fields that store information about headings
(directions)?

I'll agree that Heading1 and Heading2 implies a non-normalized database, but
sometimes you're a little too quick to pounce on people!

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Rick B said:
Why do your headings contain data?

A heading would typically be the same as the field which displays data in
that column. field names should not be 'data'.

More details please. I think your database structure may be badly flawed.

--
Rick B



I need to calculate the difference in reported headings (azimuth),
with
a
360
degree max. I am currently doing it by subtracting the lower number from
the
greater.
IIf ([heading1]>[heading2], [heading1]-[heading2], [heading2]-[heading1]).

That works just fine but now someone wants to see it in the shortest
degree
around. In current calc heading1 is 332 degrees and heading2 is 14 degrees
my
result is 318 degree difference. But that is calculating it the long way
around. Is there a way to get the 42 degree result?

Thanks
Dave
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top