File: rotation.R

package info (click to toggle)
r-cran-lava 1.8.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,816 kB
  • sloc: sh: 13; makefile: 2
file content (50 lines) | stat: -rw-r--r-- 1,095 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
##' Performs a rotation in the plane
##'
##' @title Performs a rotation in the plane
##' @aliases rotate2 rot2D rot3D
##' @param x Matrix to be rotated (2 times n)
##' @param theta Rotation in radians
##' @return Returns a matrix of the same dimension as \code{x}
##' @author Klaus K. Holst
##' @export
##' @examples
##' rotate2(cbind(c(1,2),c(2,1)))
##' @keywords hplot
`rotate2` <-
function(x,theta=pi) {
  R <- matrix(c(cos(theta), -sin(theta), sin(theta), cos(theta)), byrow=TRUE, ncol=2)
  x%*%R
}

## clockwise rotation 2d:
##' @export
rot2D <- function(theta) {
  matrix(c(cos(theta),sin(theta),-sin(theta),cos(theta)),2)
}

##' @export
rot3D <- function(x=0,y=0,z=0) {
  Rx <- function() {
    R2 <- rot2D(x)
    R <- diag(3)
    R[2:3,2:3] <- R2
    return(R)
  }
  Ry <- function() {
    R2 <- rot2D(y)
    R <- diag(3)
    R[c(1,3),c(1,3)] <- R2
    return(R)
  }
  Rz <- function() {
    R2 <- rot2D(z)
    R <- diag(3)
    R[1:2,1:2] <- R2
    return(R)
  } 
  res <- diag(3)
  if (x!=0) res <- res%*%Rx()
  if (y!=0) res <- res%*%Ry()
  if (z!=0) res <- res%*%Rz()
  return(res)
}