-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix_2d_extensions.rb
68 lines (52 loc) · 1.37 KB
/
matrix_2d_extensions.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
require 'matrix'
def p(x, y)
Matrix.point2d(x, y)
end
class Matrix
def self.rotation2d(r)
Matrix[ [Math.cos(r), -Math.sin(r), 0], [Math.sin(r), Math.cos(r), 0], [0, 0, 1] ]
end
def self.tranlsation2d(x, y)
Matrix[ [1, 0, x], [0, 1, y], [0, 0, 1] ]
end
def self.reflection2d(r)
Matrix[ [Math.cos(2*r), Math.sin(2*r), 0], [Math.sin(2*r), -Math.cos(2*r), 0], [0, 0, 1] ]
end
def self.reflection2dp(p1, p2)
p1.to_tranlsation2d*reflection2d(p1.angle2d(p2))*(-p1).to_tranlsation2d
end
def self.scale2d(factor)
Matrix[ [factor, 0, 0], [0, factor, 0], [0, 0, 1] ]
end
def self.point2d(x, y)
Matrix.column_vector([x, y, 1])
end
def x
self[0, 0]
end
def y
self[1, 0]
end
def to_tranlsation2d
Matrix.tranlsation2d(x, y)
end
def translate2d(by)
by.to_tranlsation2d*self
end
def rotate2d(radians, around=Matrix.point2d(0, 0))
(around.to_tranlsation2d*Matrix.rotation2d(radians)*(-around).to_tranlsation2d*self)
end
def scale2d(factor, around=Matrix.point2d(0, 0))
(around.to_tranlsation2d*Matrix.scale2d(factor)*(-around).to_tranlsation2d*self)
end
def distance2d(other)
(self-other).column(0).magnitude
end
def angle2d(other)
diff = (other-self)
Math.atan2(diff.y, diff.x)
end
def to2d_s(precision=3)
"#{'%g' % x.round(precision)} #{'%g' % y.round(precision)}"
end
end