Skip to contents

Angle between two vectors

Source: R/angle.R
angle.Rd

angle calculates the angle between two vectors.

Usage

angle(x, y, degree = TRUE)

Arguments

x

a numeric vector

y

a numeric vector

degree

logical; should the angle be computed in degrees? If FALSE the result is returned in radians

Value

a scalar containing the angle between the vectors

See also

Examples

x <- c (2,1)
y <- c (1,1)
angle(x, y) # degrees
#> [,1]
#> [1,] 18.43495
angle(x, y, degree = FALSE) # radians
#> [,1]
#> [1,] 0.3217506

# visually
xlim <- c (0,2.5)
ylim <- c (0,2)
# proper geometry requires asp=1
plot ( xlim, ylim, type="n", xlab="X", ylab="Y", asp=1,
 main = expression (theta == 18.4))
abline (v=0, h=0, col="gray")
vectors (rbind (x,y), col=c ("red", "blue"), cex.lab=c (2, 2)) 
text (.5, .37, expression (theta))



####
x <- c (-2,1)
y <- c (1,1)
angle(x, y) # degrees
#> [,1]
#> [1,] 108.4349
angle(x, y, degree = FALSE) # radians
#> [,1]
#> [1,] 1.892547

# visually
xlim <- c (-2,1.5)
ylim <- c (0,2)
# proper geometry requires asp=1
plot ( xlim, ylim, type="n", xlab="X", ylab="Y", asp=1,
 main = expression (theta == 108.4))
abline (v=0, h=0, col="gray")
vectors (rbind (x,y), col=c ("red", "blue"), cex.lab=c (2, 2)) 
text (0, .4, expression (theta), cex=1.5)

AltStyle によって変換されたページ (->オリジナル) /