Calculating angle of a polyline

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Calculating angle of a polyline

javad bayat
Dear R users
I am trying to find a formula to calculate the angle of a polyline. Is
there a way to do this?
Many thanks.

--
Best Regards
Javad Bayat
M.Sc. Environment Engineering
Alternative Mail: [hidden email]

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Calculating angle of a polyline

Jeff Newmiller
A polyline by definition has many angles, so your question is ill-formed. And this is a question about math, not R, so is off topic here. I suggest reading Wikipedia.
--
Sent from my phone. Please excuse my brevity.

On January 29, 2018 11:10:02 PM PST, javad bayat <[hidden email]> wrote:
>Dear R users
>I am trying to find a formula to calculate the angle of a polyline. Is
>there a way to do this?
>Many thanks.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Calculating angle of a polyline

Eric Berger
Assuming your polyline is defined by two vectors, one for the x
coordinates, one for the y coordinates, you can try the following

library(NISTunits)
polyangles <- function(xV,yV) {
  stopifnot( (length(xV)==length(yV)) && (length(xV) >= 3))
  v <- function(i) { c( xV[i]-xV[i-1], yV[i]-yV[i-1])}
  vlen <- function(v) { sqrt(sum(v*v)) }

  lV <- rep(NA_real_,length(xV))
  for ( i in 2:(length(xV)-1) )
    lV[i] <- acos( sum(v(i)*v(i+1))/(vlen(v(i))*vlen(v(i+1))) )
  angleV <- NISTunits::NISTradianTOdeg(lV)
  angleV
}

# example
x <- c(0:3)
y <- c(0,0,1,1)
polyangles( x, y )

# NA 45.0 45.0 NA

Note, I have included the NA's at the beginning and end of the polyline as
a reminder that there is no angle defined there.

HTH,
Eric


On Tue, Jan 30, 2018 at 4:34 PM, Jeff Newmiller <[hidden email]>
wrote:

> A polyline by definition has many angles, so your question is ill-formed.
> And this is a question about math, not R, so is off topic here. I suggest
> reading Wikipedia.
> --
> Sent from my phone. Please excuse my brevity.
>
> On January 29, 2018 11:10:02 PM PST, javad bayat <[hidden email]>
> wrote:
> >Dear R users
> >I am trying to find a formula to calculate the angle of a polyline. Is
> >there a way to do this?
> >Many thanks.
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Calculating angle of a polyline

R help mailing list-2
I like to use complex numbers for 2-dimensional geometry.  E.g.,

> polyAngles2
function (xV, yV)
{
    stopifnot((length(xV) == length(yV)) && (length(xV) >= 3))
    z <- complex(re = xV, im = yV)
    c(NA, diff(Arg(diff(z))), NA) # radians, positive is counter-clockwise
}
> x <- c(0:3)
> y <- c(0,0,1,1)
> polyAngles2(x,y) / pi * 180
[1]  NA  45 -45  NA



Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Tue, Jan 30, 2018 at 7:09 AM, Eric Berger <[hidden email]> wrote:

> Assuming your polyline is defined by two vectors, one for the x
> coordinates, one for the y coordinates, you can try the following
>
> library(NISTunits)
> polyangles <- function(xV,yV) {
>   stopifnot( (length(xV)==length(yV)) && (length(xV) >= 3))
>   v <- function(i) { c( xV[i]-xV[i-1], yV[i]-yV[i-1])}
>   vlen <- function(v) { sqrt(sum(v*v)) }
>
>   lV <- rep(NA_real_,length(xV))
>   for ( i in 2:(length(xV)-1) )
>     lV[i] <- acos( sum(v(i)*v(i+1))/(vlen(v(i))*vlen(v(i+1))) )
>   angleV <- NISTunits::NISTradianTOdeg(lV)
>   angleV
> }
>
> # example
> x <- c(0:3)
> y <- c(0,0,1,1)
> polyangles( x, y )
>
> # NA 45.0 45.0 NA
>
> Note, I have included the NA's at the beginning and end of the polyline as
> a reminder that there is no angle defined there.
>
> HTH,
> Eric
>
>
> On Tue, Jan 30, 2018 at 4:34 PM, Jeff Newmiller <[hidden email]>
> wrote:
>
> > A polyline by definition has many angles, so your question is ill-formed.
> > And this is a question about math, not R, so is off topic here. I suggest
> > reading Wikipedia.
> > --
> > Sent from my phone. Please excuse my brevity.
> >
> > On January 29, 2018 11:10:02 PM PST, javad bayat <[hidden email]>
> > wrote:
> > >Dear R users
> > >I am trying to find a formula to calculate the angle of a polyline. Is
> > >there a way to do this?
> > >Many thanks.
> >
> > ______________________________________________
> > [hidden email] 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]]
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Calculating angle of a polyline

Eric Berger
nice

On Tue, Jan 30, 2018 at 7:05 PM, William Dunlap <[hidden email]> wrote:

> I like to use complex numbers for 2-dimensional geometry.  E.g.,
>
> > polyAngles2
> function (xV, yV)
> {
>     stopifnot((length(xV) == length(yV)) && (length(xV) >= 3))
>     z <- complex(re = xV, im = yV)
>     c(NA, diff(Arg(diff(z))), NA) # radians, positive is counter-clockwise
> }
> > x <- c(0:3)
> > y <- c(0,0,1,1)
> > polyAngles2(x,y) / pi * 180
> [1]  NA  45 -45  NA
>
>
>
> Bill Dunlap
> TIBCO Software
> wdunlap tibco.com
>
> On Tue, Jan 30, 2018 at 7:09 AM, Eric Berger <[hidden email]>
> wrote:
>
>> Assuming your polyline is defined by two vectors, one for the x
>> coordinates, one for the y coordinates, you can try the following
>>
>> library(NISTunits)
>> polyangles <- function(xV,yV) {
>>   stopifnot( (length(xV)==length(yV)) && (length(xV) >= 3))
>>   v <- function(i) { c( xV[i]-xV[i-1], yV[i]-yV[i-1])}
>>   vlen <- function(v) { sqrt(sum(v*v)) }
>>
>>   lV <- rep(NA_real_,length(xV))
>>   for ( i in 2:(length(xV)-1) )
>>     lV[i] <- acos( sum(v(i)*v(i+1))/(vlen(v(i))*vlen(v(i+1))) )
>>   angleV <- NISTunits::NISTradianTOdeg(lV)
>>   angleV
>> }
>>
>> # example
>> x <- c(0:3)
>> y <- c(0,0,1,1)
>> polyangles( x, y )
>>
>> # NA 45.0 45.0 NA
>>
>> Note, I have included the NA's at the beginning and end of the polyline as
>> a reminder that there is no angle defined there.
>>
>> HTH,
>> Eric
>>
>>
>> On Tue, Jan 30, 2018 at 4:34 PM, Jeff Newmiller <[hidden email]
>> >
>> wrote:
>>
>> > A polyline by definition has many angles, so your question is
>> ill-formed.
>> > And this is a question about math, not R, so is off topic here. I
>> suggest
>> > reading Wikipedia.
>> > --
>> > Sent from my phone. Please excuse my brevity.
>> >
>> > On January 29, 2018 11:10:02 PM PST, javad bayat <[hidden email]>
>> > wrote:
>> > >Dear R users
>> > >I am trying to find a formula to calculate the angle of a polyline. Is
>> > >there a way to do this?
>> > >Many thanks.
>> >
>> > ______________________________________________
>> > [hidden email] 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]]
>>
>> ______________________________________________
>> [hidden email] 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/posti
>> ng-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.