R Code
2003-12-06 22:31:38 UTC
There is a common requirement of an angle from North to a point in
rectangular coordinates. Of course this is a clockwise angle off the
positive y axis to the point.
Using ArcTan(y/x) instead of ArcTan(x/y) *, exploiting core output of
positive or negative values, and simplifying conditions, the code can be as
simple as:
If (x = 0) Then x:= 0.000000000000001;
If (x > 0) Then artt:= (Pi*0.5) - ArcTan(y/x)
Else artt:= (Pi*1.5) - ArcTan(y/x);
Of course the nature of the input must be known not to be entirely very
small values...otherwise more logic is needed.
Expanding application, the x coordinate of Point B minus the x coordinate of
Point A will produce a translated x coordinate for input while the y
coordinate of Point B minus the y coordinate of Point A will produce a
translated y coordinate for input where the translated coordinates are
relative to an origin for purpose of computation of the direction of Point A
to Point B...
* (ArcTan(x/y) is often core usage in a four-quadrant compass direction
system.)
rectangular coordinates. Of course this is a clockwise angle off the
positive y axis to the point.
Using ArcTan(y/x) instead of ArcTan(x/y) *, exploiting core output of
positive or negative values, and simplifying conditions, the code can be as
simple as:
If (x = 0) Then x:= 0.000000000000001;
If (x > 0) Then artt:= (Pi*0.5) - ArcTan(y/x)
Else artt:= (Pi*1.5) - ArcTan(y/x);
Of course the nature of the input must be known not to be entirely very
small values...otherwise more logic is needed.
Expanding application, the x coordinate of Point B minus the x coordinate of
Point A will produce a translated x coordinate for input while the y
coordinate of Point B minus the y coordinate of Point A will produce a
translated y coordinate for input where the translated coordinates are
relative to an origin for purpose of computation of the direction of Point A
to Point B...
* (ArcTan(x/y) is often core usage in a four-quadrant compass direction
system.)