Signed Angle Between 2 Vectors?

Signed Angle Between 2 Vectors?
$\begingroup$

on this link I found the following formula:

signed_angle = atan2(b.y,b.x) - atan2(a.y,a.x)

or I can rewrite it as:

atan2(y, x) = atan(y / x)

vectors:
a = {x1, y1}
b = {x2, y2}

signed_angle = atan(y2 / x2) - atan(y1 / x1)

And it doesn't work properly for example when:

a = {-1, 0} and b = {1, 0}

Additionally I know a direction from first angle to a second one (clockwise or counterclockwise) but I don't know how to apply it to a first formula.

How to solve this issue?

$\endgroup$
9

2 Answers

$\begingroup$

My current solution:

angleAC = ... (angle of vector A with Ox)
angleBC = ... (angle of vector B with Ox)
dangle = (angleBC - angleAC) / n (n is a number enough to split a big angle to oblique angles)

if (dangle > 0 and clockwise) or (dangle < 0 and counterclockwise) then check angleAC and angleBC. If they are less than 0 then add 2 * Pi to them.

This action looks strange but it really allows to get angle from A to B according to clockwise/counterclockwise direction

$\endgroup$
$\begingroup$

I have dealt with the same problem.

Let us say that you have the vectors u = (u1, u2), v = (v1, v2) in the plane. What would be a good definition of a signed angle between the vectors u and v? One possible definition is to define it by the rotation angle that applied to vector u results in a vector with same direction and sense of v.

For example, if u = (1, 1) and v = (-1, 1), then the angle theta(u, v) would be + pi/2 (90 degrees), since if rotating u counterclockwise 90 degrees you get v. Observe that theta(v,u) is -pi/2, since you need to rotate v 90 degrees clockwise to obtain u.

Observe also that you could also rotate u by -3*pi/2 (-270 degrees) to get v. So, in order to make the definition unique, you should pick up the angle that lies in (-pi, pi]. Observe that for the vectors u = c(1,1) and v = (-1,-1), the angle could be +pi (+180) or -pi (-180), so you need to choose one, e.g., +pi.

In general, we have the following properties of the theta function:

  1. theta(u,v) = -theta(v,u)
  2. theta(-u, v) = theta(u,v) - pi
  3. theta(u,-v) = theta(u,v) - pi

One R script to implement that function using atan2 function is given below:

theta <- function(u, v) {
    # u, v: matrices of bivariate vectors with 2 columns
    u <- matrix(u, ncol = 2)
    v <- matrix(v, ncol = 2)
    if (nrow(u) != nrow(v)) stop("u and v must have the same number of 
                                  rows")
    result <- as.numeric(atan2(v[,2], v[,1]) - atan2(u[,2], u[,1]))
    ind1 <- which(result > pi)
    ind2 <- which(result <= -pi)
    result[ind1] <- result[ind1] - 2*pi
    result[ind2] <- result[ind2] + 2*pi
    return(result) 
} # end-function

I hope this helps,

Luis Ernesto

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

James H. Sterling
Author

James H. Sterling

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.