Angles Between Continuous 2D Vectors
<p>I'm having some trouble calculating the clockwise angles between continuous 2D vectors. My computed angles do not seem right when I compare them by eye on a plot. What follows is my process in R. </p>

<p>If necessary, install and enable the "circular" package:</p>

<pre><code>install.packages('circular')
library(circular)
</code></pre>

<p>Generate a small data frame of 2D coordinates:</p>

<pre><code>functest &lt;- data.frame(x=c(2,8,4,9,10,7),y=c(6,8,2,5,1,4))
</code></pre>

<p>Plot the points for reference:</p>

<pre><code>windows(height=8,width=8)
par(pty="s")
plot(functest, main = "Circular Functions Test")
## draw arrows from point to point :
s &lt;- seq(length(functest$x)-1)  # one shorter than data
arrows(functest$x[s], functest$y[s], functest$x[s+1], functest$y[s+1], col = 1:3)
</code></pre>

<p>Create a function that computes the angle between two vectors:</p>

<pre><code>angle &lt;- function(m)
{   # m is a matrix
  dot.prod &lt;- crossprod(m[, 1], m[, 2])
  norm.x &lt;- norm(m[, 1], type="2")
  norm.y &lt;- norm(m[, 2], type="2")
  theta &lt;- acos(dot.prod / (norm.x * norm.y))
  as.numeric(theta) # returns the angle in radians
}
</code></pre>

<p>Generate a vector of compass angles in degrees (clockwise rotation):</p>

<pre><code>functest_matrix &lt;- cbind(x = functest$x,y = functest$y)
moves &lt;- apply(functest_matrix, 2, diff)
tst &lt;- lapply(seq(nrow(moves) - 1), function(idx) moves[c(idx, idx + 1), ])
functest_angles &lt;- vapply(tst, angle, numeric(1))
functest_object &lt;- circular(functest_angles, type="angles", units="radians", zero=0, rotation = "counter")
functest_convert &lt;- conversion.circular(functest_object, type = "angles", units = "degrees", rotation = "clock", zero = pi/2)
functest_compass &lt;- lapply(functest_convert, function(x) {if (x &lt; 0) x+360 else x}) # converts any negative rotations to positive
</code></pre>

<p>I suspect something wrong may be occuring in my last three lines of code when I try to convert "normal" counterclockwise angles in radians to clockwise compass angles in degrees. Any help would be greatly appreciated!</p>