[R] Angle between two points with coordinates

William Dunlap wdunlap at tibco.com
Thu Jan 28 20:39:47 CET 2016


In addition to the other fine answers, you might find it convenient
to represent the points as complex numbers and use the Arg function
to get the angle (and abs() or Mod() the distance).

  > z <- complex(real=0.8660254, imaginary=0.5)
  > Arg(z) / base::pi * 180
  [1] 30
  > Arg(-z) / base::pi * 180
  [1] -150



Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Thu, Jan 28, 2016 at 9:09 AM, 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)
>
>
> Thank you very much in advance for your answers!
>
> 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
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list