I am calculating angles for each point with 'for' loop. But somehow the matrix is not populated as desirable. Only the first element in matrix is populated with calculated value.
w1 = c(-2.40154, -1.82937)
w2 = c(-2.079041, 101,0663)
xy = as.data.frame(coordinates(p))
angle = function (w1, w2 , p, ...) {
iterations = 2500
a = sqrt ((w1[1]-w2[1])^2 + (w1[2]-w2[2])^2)
tr <- matrix(ncol=1, nrow=iterations)
for (i in 1:iterations) {
b[i] = sqrt ((p$coords.x1[i]-w1[1])^2 + (p$coords.x2[i]-w1[2])^2)
c[i] = sqrt ((p$coords.x1[i]-w2[1])^2 + (p$coords.x2[i]-w2[2])^2)
tr[i,] = (acos ((b[i]^2 + c[i]^2 - a^2)/ (2*b[i]*c[i]))*180)/pi
print(tr)
}
return(tr)
}
t = angle(w1,w2,xy)
tr
is getting only the first value. Please correct the code.