-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlinear-algebra.r
More file actions
executable file
·59 lines (47 loc) · 1.03 KB
/
Copy pathlinear-algebra.r
File metadata and controls
executable file
·59 lines (47 loc) · 1.03 KB
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
51
52
53
54
55
56
57
58
59
π = pi
degrees <- function(x){
return((x*180)/π)
}
radians <- function(x){
return((x*π)/(180))
}
euclidean.dist <- function(v1,v2){
return(sqrt(sum((v1-v2)^2)))
}
dot.product <- function(v1,v2){
return(v1[1]*v2[1]+v1[2]*v2[2])
}
magnitude <- function(v){
return(sqrt(v[1]^2+v[2]^2))
}
heading <- function(v){
return(-1*atan2(-v[2],v[1]))
}
angle.between <- function(v1,v2){
return(acos(dot.product(v1,v2)/(magnitude(v1)*magnitude(v2))))
}
mid.point <- function(v1,v2){
return((v1+v2)/2)
}
map <- function(value,istart,istop,ostart,ostop){
return(ostart+(ostop-ostart)*((value-istart)/(istop-istart)))
}
normalise <- function(v){
if(magnitude(v)>0){
v=v/magnitude(v)
}
return(v)
}
rotate <- function(v,c,θ) {
r.θ = radians(θ)
cos.r.θ = cos(r.θ)
sin.r.θ = sin(r.θ)
x = (cos.r.θ*(v[1]-c[1]))-(sin.r.θ*(v[2]-c[2]))+c[1]
y = (sin.r.θ*(v[1]-c[1]))+(cos.r.θ*(v[2]-c[2]))+c[2]
return(c(x,y))
}
scale <- function(v1,v2,scalar){
v <- v2-v1
r <- magnitude(v)*scalar
return (normalise(v)*r)
}