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
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) > > >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 >stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide >R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code.[[alternative HTML version deleted]]
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 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) > > > > > >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 > >stat.ethz.ch/mailman/listinfo/r-help > >PLEASE do read the posting guide > >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 > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > 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]]
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 > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Thank you very much for your quick answers! The %% operator seems the easiest way to go; it works perfectly. Best regards, Gwenna?l Le 28/01/2016 20:39, William Dunlap a ?crit :> 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 <tibco.com> > > On Thu, Jan 28, 2016 at 9:09 AM, Gwenna?l Bataille > <gwennael.bataille at uclouvain.be > <mailto: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 <mailto:R-help at r-project.org> mailing list -- > To UNSUBSCRIBE and more, see > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > >-- Gwenna?l BATAILLE, PhD student - Teaching assistant Earth and Life Institute Universit? Catholique de Louvain SST/ELI/ELIB B?timent Carnoy, c.145 Croix du sud 4-5, bte L7.07.04 1348 Louvain-la-Neuve BELGIUM [[alternative HTML version deleted]]