Rotate a set of XY coordinates by an angle (in radians)

rotation(xy, angle)

Arguments

xy

A 2-columns matrix or data frame containing a set of X and Y coordinates.

angle

Numeric. A scalar giving the angle at which the points should be rotated. The angle is in radians.

Value

A 2-columns matrix of the same size as xy giving the rotated coordinates.

Author

F. Guillaume Blanchet

Examples


### Create a set of coordinates
coords<-cbind(runif(20),runif(20))

### Create a series of angles
rad<-seq(0,pi,l=20)

for(i in rad){
  coords.rot<-rotation(coords,i)
  plot(coords.rot)
}





















### Rotate the coordinates by an angle of 90 degrees
coords.90<-rotation(coords,90*pi/180)
coords.90
#>              [,1]       [,2]
#>  [1,] -0.78026771 0.26797393
#>  [2,] -0.46533468 0.57608248
#>  [3,] -0.29428256 0.47024426
#>  [4,] -0.81341496 0.31994784
#>  [5,] -0.65086368 0.17457951
#>  [6,] -0.85414181 0.02481635
#>  [7,] -0.64967448 0.79057054
#>  [8,] -0.39241969 0.94760864
#>  [9,] -0.30072139 0.40604069
#> [10,] -0.01069501 0.91210663
#> [11,] -0.44004620 0.71016697
#> [12,] -0.89412994 0.22819366
#> [13,] -0.82368761 0.47040926
#> [14,] -0.31790044 0.56830036
#> [15,] -0.33209263 0.21428404
#> [16,] -0.33660731 0.74699933
#> [17,] -0.20832929 0.94392034
#> [18,] -0.58814905 0.91819588
#> [19,] -0.61408831 0.07796966
#> [20,] -0.19934996 0.24442939

plot(coords,xlim=range(rbind(coords.90,coords)[,1]),ylim=range(rbind(coords.90,coords)[,2]),asp=1)
points(coords.90,pch=19)