-
Notifications
You must be signed in to change notification settings - Fork 0
/
transformation.rb
75 lines (67 loc) · 1.42 KB
/
transformation.rb
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
require "matrix"
class Integer
def to_r
self * (Math::PI / 180)
end
end
class Matrix
def to_s
s = ""
self.to_a.each {|r| s+=r.inspect + "\n"}
s
end
end
def T(x, y, z)
Matrix[
[1, 0, 0, x],
[0, 1, 0, y],
[0, 0, 1, z],
[0, 0, 0, 1]
]
end
def R(angle:, axis:)
raise ArgumentError, "Invalid Angle!" unless angle.is_a? Numeric
angle = angle.to_r
case axis
when "x"
Matrix[
[1, 0, 0 ,0],
[0, Math.cos(angle).round, -Math.sin(angle).round, 0],
[0, Math.sin(angle).round, Math.cos(angle).round, 0],
[0, 0, 0, 1]
]
when "y"
Matrix[
[Math.cos(angle).round, 0, Math.sin(angle).round, 0],
[0, 1, 0, 0],
[-Math.sin(angle).round, 0, Math.cos(angle).round, 0],
[0, 0, 0, 1]
]
when "z"
Matrix[
[Math.cos(angle).round, -Math.sin(angle).round, 0, 0],
[Math.sin(angle).round, Math.cos(angle).round, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]
]
else
raise ArgumentError, "Wrong Axis!"
end
end
def S(x, y, z)
Matrix[
[x, 0, 0, 0],
[0, y, 0, 0],
[0, 0, z, 0],
[0, 0, 0, 1]
]
end
def P(x, y, z, d=1)
Matrix[
[x],
[y],
[z],
[d]
]
end
# start with irb -r ./transformation.rb