bivector 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/bivector.rb +76 -64
- metadata +1 -1
data/lib/bivector.rb
CHANGED
@@ -1,64 +1,76 @@
|
|
1
|
-
class Bivector
|
2
|
-
attr_accessor :x, :y
|
3
|
-
|
4
|
-
def initialize(xCoordinate, yCoordinate)
|
5
|
-
@x, @y = xCoordinate, yCoordinate
|
6
|
-
end
|
7
|
-
|
8
|
-
def +(another)
|
9
|
-
if another.is_a? Bivector
|
10
|
-
Bivector.new(x + another.x, y + another.y)
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
def -(another)
|
15
|
-
if another.is_a? Bivector
|
16
|
-
Bivector.new(x - another.x, y - another.y)
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
def *(aNumber)
|
21
|
-
end
|
22
|
-
|
23
|
-
def norm
|
24
|
-
(x**2 + y**2)**0.5
|
25
|
-
end
|
26
|
-
alias :length :norm
|
27
|
-
|
28
|
-
def zero?
|
29
|
-
return (x == 0 && y ==0) ? true : false
|
30
|
-
end
|
31
|
-
alias :zero_vector? :zero?
|
32
|
-
alias :null_vector? :zero?
|
33
|
-
|
34
|
-
def dot_product(another)
|
35
|
-
if another.is_a? Bivector
|
36
|
-
x * another.x + y * another.y
|
37
|
-
end
|
38
|
-
end
|
39
|
-
alias :inner_product :dot_product
|
40
|
-
|
41
|
-
def cross_product(another)
|
42
|
-
if another.is_a? Bivector
|
43
|
-
Bivector.new()
|
44
|
-
end
|
45
|
-
end
|
46
|
-
alias :outer_product :cross_product
|
47
|
-
|
48
|
-
def unit_vector
|
49
|
-
return nil if self.zero?
|
50
|
-
Bivector.new(x / norm, y / norm)
|
51
|
-
end
|
52
|
-
|
53
|
-
def angles_to(another, style = 'rad')
|
54
|
-
if another.is_a? Bivector
|
55
|
-
angle = Math.acos(dot_product(another) / ( norm * another.norm))
|
56
|
-
case style
|
57
|
-
when 'rad'
|
58
|
-
return angle
|
59
|
-
when 'ang'
|
60
|
-
return angle *
|
61
|
-
end
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
1
|
+
class Bivector
|
2
|
+
attr_accessor :x, :y
|
3
|
+
|
4
|
+
def initialize(xCoordinate, yCoordinate)
|
5
|
+
@x, @y = xCoordinate, yCoordinate
|
6
|
+
end
|
7
|
+
|
8
|
+
def +(another)
|
9
|
+
if another.is_a? Bivector
|
10
|
+
Bivector.new(x + another.x, y + another.y)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def -(another)
|
15
|
+
if another.is_a? Bivector
|
16
|
+
Bivector.new(x - another.x, y - another.y)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def *(aNumber)
|
21
|
+
end
|
22
|
+
|
23
|
+
def norm
|
24
|
+
(x**2 + y**2)**0.5
|
25
|
+
end
|
26
|
+
alias :length :norm
|
27
|
+
|
28
|
+
def zero?
|
29
|
+
return (x == 0 && y ==0) ? true : false
|
30
|
+
end
|
31
|
+
alias :zero_vector? :zero?
|
32
|
+
alias :null_vector? :zero?
|
33
|
+
|
34
|
+
def dot_product(another)
|
35
|
+
if another.is_a? Bivector
|
36
|
+
x * another.x + y * another.y
|
37
|
+
end
|
38
|
+
end
|
39
|
+
alias :inner_product :dot_product
|
40
|
+
|
41
|
+
def cross_product(another)
|
42
|
+
if another.is_a? Bivector
|
43
|
+
Bivector.new()
|
44
|
+
end
|
45
|
+
end
|
46
|
+
alias :outer_product :cross_product
|
47
|
+
|
48
|
+
def unit_vector
|
49
|
+
return nil if self.zero?
|
50
|
+
Bivector.new(x / norm, y / norm)
|
51
|
+
end
|
52
|
+
|
53
|
+
def angles_to(another, style = 'rad')
|
54
|
+
if another.is_a? Bivector
|
55
|
+
angle = Math.acos(dot_product(another) / ( norm * another.norm))
|
56
|
+
case style
|
57
|
+
when 'rad'
|
58
|
+
return angle
|
59
|
+
when 'ang'
|
60
|
+
return angle * 180 / Math::PI
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def parallels_to?(another)
|
66
|
+
if another.is_a? Bivector
|
67
|
+
self.zero? || another.zero? || x * another.y - y * another.x == 0
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def perpendicular_to?(another)
|
72
|
+
if another.is_a? Bivector
|
73
|
+
self.dot_product(another) == 0
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|