[R] Angle between two points with coordinates

Dalthorp, Daniel ddalthorp at usgs.gov
Thu Jan 28 20:05:14 CET 2016

```Gwennaël,

Does the %% operator work for you?

It gives x mod y (or the remainder after dividing x into y...result is
guaranteed to be <=0 and >y)

E.g.

-150 %% 360      # 210
570 %% 360        # 210

https://stat.ethz.ch/R-manual/R-devel/library/base/html/Arithmetic.html

-Dan

On Thu, Jan 28, 2016 at 10:38 AM, Jeff Newmiller <jdnewmil at dcn.davis.ca.us>
wrote:

> Functions return one value.
>
> Look at ?atan2 to address ambiguity in identifying angles.
> --
> Sent from my phone. Please excuse my brevity.
>
> On January 28, 2016 9:09:53 AM PST, "Gwennaël Bataille" <
> gwennael.bataille at uclouvain.be> wrote:
> >Dear all,
> >I'd like to calculate the angle from one point (origin) to another
> >(target), whatever their coordinates.
> >But I encounter some problems (detailed below). The problem could be
> >solved if one of you could answer positively to one of the following
> >questions:
> >
> >1) Is there a function in R converting angles in a standardized manner?
> >
> >(for example, converting -150 or 570 (=210+360) into 210)
> >
> >2) If not, would you know a function arccos or arcsin returning two
> >different angles as an output instead of one?
> >
> >
> >
> >Details:
> >
> >I'd like to calculate the angle from one point (origin) to another
> >(target), whatever their coordinates.
> >For this, the acos and asin functions work pretty well when the end
> >point is located right and above the starting point (first quarter of
> >the trigonometric circle), but are problematic otherwise.
> >
> ># In the following example, the origin is (0,0) and the target
> >(0.8660254, 0.5) is located at an angle of 30° :
> >acos( (0.8660254 - 0) )*180/pi
> >asin( (0.5 - 0) )*180/pi
> ># Both acos and asin give the same answer : 30
> >
> ># If now, the origin is (0.8660254, 0.5) and the target is (0,0), the
> >target is located at an angle of -150° :
> >acos( (0 - 0.8660254) )*180/pi
> >asin( (0 - 0.5) )*180/pi
> ># Here the results are different : 150 and -30
> >
> ># In fact, there are two angle solutions giving the same cosinus : 150
> >and -(150)
> ># And for sinus as well : -30 and ( 180 - (-30) ) = 210° = -150°
> >-acos( (0 - 0.8660254) )*180/pi
> >180 - asin( (0 - 0.5) )*180/pi
> ># But I cannot test equality between the two :
> >-acos( (0 - 0.8660254) )*180/pi    ==    180 - asin( (0 - 0.5) )*180/pi
> ># FALSE, since 210 != -150  (it's only the case when those two are
> >angles)
> >
> >
> >
> >Best regards,
> >
> >
> >Gwennaël
> >
> >--
> >Gwennaël BATAILLE, PhD student - Teaching assistant
> >
> >Earth and Life Institute
> >Université Catholique de Louvain
> >1348 Louvain-la-Neuve
> >BELGIUM
> >
> >______________________________________________
> >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> >https://stat.ethz.ch/mailman/listinfo/r-help
> >http://www.R-project.org/posting-guide.html
> >and provide commented, minimal, self-contained, reproducible code.
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

--
Dan Dalthorp, PhD
USGS Forest and Rangeland Ecosystem Science Center
Forest Sciences Lab, Rm 189
3200 SW Jefferson Way
Corvallis, OR 97331
ph: 541-750-0953
ddalthorp at usgs.gov

[[alternative HTML version deleted]]

```