Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

advice on performing ArcTan2 (atan2) function please 1

Status
Not open for further replies.

CADTenchy

Technical User
Dec 12, 2007
237
GB
I need to do an atan2(X,Y) calculation, and the function doesn't seem available in Delphi 2007? (I've added Math to my uses statement, for other trig functions I need)

I've had a fair Google, and found some references, but I haven't really found anything conclusive enough to do a sum.
Searching Delphi help does get references to atan2, but in javascript and C it seems.

Does anyone have an actual code snippet or know how do do it?

Steve (Delphi 2007 & XP)
 
(thanks daddy. I did see that page, but I have to be honest and say I missed the two small functions there, my eye being drawn by the bit below them, which was a bit whoosh, over my head.)

I've added the two functions now, and I assume they are working. I say assume, as the main part of my formula from which I can tell if the answer is correct, I still haven't got working.

this is my source non-Delphi formula:
Code:
tc1=mod(atan2(sin(lon2-lon1)*cos(lat2),cos(lat1)*sin(lat2)-sin(lat1)*cos(lat2)*cos(lon2-lon1)),2*pi)

This is how I have interpreted it for Delphi:
Code:
tc1 := atan2(sin(Lon-HomeLon)*cos(Lat),cos(HomeLat)*sin(Lat)-sin(HomeLat)*cos(Lat)*cos(Lon-HomeLon)) mod (2*pi);

Now I can see Delphi mod performs integer division, returning the remainder, so this line doesn't work.

Am I interpreting the original sum incorrectly, or am I implementing it incorrectly in Delphi?




Steve (Delphi 2007 & XP)
 
Looks like both. What's the definition of the mod function in the source formula?
 

This is from the source:

In the formula, atan2(y,x) is the arctangent, or inverse tangent, of
y/x, with the additional feature of deciding which quadrant the angle
belongs in, based on the signs of x and y. This is common in computer
programming languages.

Also, mod(a,2*pi) is the remainder you get when you divide a by 2*pi;
that is, subtract the largest multiple of 2*pi less than a from a,
and that's the answer.

I'm sure I could write the sums to do the mod now, that's assuming there isn't a built in function?


Steve (Delphi 2007 & XP)
 
Just for info, in Delphi 6 I have an ArcTan2 function in my Math unit as follows:
Code:
function ArcTan2(const Y, X: Extended): Extended;
asm
        FLD     Y
        FLD     X
        FPATAN
        FWAIT
end;

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
lol Clive,

I was so focused on Atan2 that I missed the ArcTan2 function.

[flush2]

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 

And better still I already tried that, by just guessing and trying if it was a function.
I got the red wiggly line error, figured wasn't a function for it then.
Since that I've added math unit to my uses, for one of the trig functions, and never retried ArcTan2.

Doh!


Steve (Delphi 2007 & XP)
 
for those that helped, and for possible future people searching, I have this which is working:

Code:
uses .... math....;
.
.
.

implementation

uses about, details;

{$R *.dfm}

procedure CalcBearing;
var
tc1, x: double;
begin
tc1 := ArcTan2(sin(Lon-HomeLon)*cos(Lat),cos(HomeLat)*sin(Lat)-sin(HomeLat)*cos(Lat)*cos(Lon-HomeLon));
x := int(tc1 / (2*pi));
tc1 := tc1 - x;
tc1 := tc1 * (180/pi);  // convert back to degrees
Bearing := round (tc1);
if Bearing < 0 then Bearing := 360 + Bearing;
end;

I don't think I need to explain the variable names (except to say Bearing is defined elsewhere to make it global)

Steve (Delphi 2007 & XP)
 
darn it, I'm sure that posted when I clicked preview...

Anyway, I was about to say, for some reason I had to add the if then line as western headings were expressed in negative bearings from north, but it works now, so....



Steve (Delphi 2007 & XP)
 
I've been trying to do something similar - distance and bearing between two sets of decimal lat/lon coordinates. What I found on the net is more than a bit baffling, as they have an online calculator which works in javascript, but produces slightly different numbers from my Delphi conversion of the code they use. I also refuse to believe that there is not a much simpler formula if you are only interested in locations a few miles apart and don't need results to twenty decimal places! The curvature of the earth doesn't come into it.

But the main difference between the formulae I found and the one that you are using for bearing is that as the trig functions in Delphi operate on radians, each time a Lat or Lon value is expressed in degrees is used they put in DegtoRad(value) (or rather a javascript user function equivalent). You haven't done that, do I assume you've converted your Lat/Lon values to radians in a routine you haven't mentioned?

There's also a RadToDeg function, by the way.
 
Hi. Yes I'm converting user input to Lat and Long in a routine that produces the answer in Radians.

You will find several small variations in the results, depending mostly on what each person has taken as the radius of the earth. Some may even factor in the fact that Earth is ellipsoid rather than perfectly spherical. (It's fatter round the equator than it is around the poles.)

Researching around, for my purposes, the following seems to give the best stab at it:

Code:
dist := Arccos( Cos(HomeLat) * Cos(HomeLon) * Cos(Lat) * Cos(Lon) + Cos(HomeLat) * Sin(HomeLon) * Cos(Lat) * Sin(Lon) + Sin(HomeLat) * Sin(Lat) ) * 6371.291;

Where 6371.291 is radius of Earth in Km (therefore result is Km too)

Also, the reason I use the bearing calc above is exactly because it is NOT dependent on distance, which some formulae are.

This is a good site for some sums:

Hope this is of use/interest.



Steve (Delphi 2007 & XP)
 
Steve,

It looks like you've already got a conversion but I'll add what works for me as an alternative.

Some years ago, long before GPS, I programmed a calculator with the following formula.

During the winter I transferred it to Delphi 7 as an exercise. My results are displayed in Statute, miles, Nautical miles, and in Kilometers. The calculation is straightforward if you bear in mind that this is Spherical Trig and not Plane.

Degrees are input in the decimal form. My interface will take either minutes or decimal and the conversion, if necessary, is fairly easy after recognition.
Recognition can be accomplished by simply leaving a separate box for minutes and seconds blank to default to decimal format. Either way, the degrees are converted to radians prior entering the function performing the actual calculation.

The actual calculation, it’s called ‘RealSolution’ simply because it’s a real number, is as follows:

RealSolution := ((ArcCos((SIN(LAT1)*SIN(LAT2))+(COS(LAT1) * COS(LAT2) * COS(LNG1-LNG2))))*GoRad)* (24879.6737/360);

Where LAT1&LON1 are the start and LAT2&LON2 are the stop.

The “GoRad” is the function (GoRad : = 180/3.14159265;) that converts the trig results back to degrees prior to being multiplied but the circumference.

The rather extensive circumference constant is in Statute miles but should work with any measure.

The calculation function returns Statute miles, which is then converted for display as indicated above.

It tested properly for Western half of the Northern hemisphere.
Additional measure requires a conversion to address the Eastern half as would the variance posed by the corresponding portions of the Northern and Southern hemispheres as well North to South.

I’d also like to try to calculate a bearing as part of the distance calculation maybe next winter I’ll go back to my old trig book and get to it.
It gets a little tricky there because of the spherical aspects of the Great Circle Route versus Mercator.

Given all the help I’ve received at this site, I hope I’m able to return something here.

Bill K
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top