-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVectors.rb
155 lines (118 loc) · 2.18 KB
/
Vectors.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
class Vector2D
def initialize(x, y)
@x, @y = x, y
end
def x
@x
end
def x=(value)
@x=value
end
def y
@y
end
def y=(value)
@y=value
end
def length
exponent_x = @x*@x
exponent_y = @y*@y
xy = exponent_x + exponent_y
Math.sqrt(xy)
end
def normalize
@x/=length
@y/=length
end
def ==(other)
@x == other.x
@y == other.y
end
def +(other)
Position.new (((@x+x.other)^2) + ((@y*y.other)^2))**0.5
end
def +(other)
Vector2D.new @x + other.x, @y + other.y
end
def -(other)
Vector2D.new @x - other.x, @y - other.y
end
def *(scalar)
Vector2D.new @x * other.x, @y * other.y
end
def /(scalar)
Vector2D.new @x / other.x, @y / other.y
end
def dot(other)
result = @x*other.x + @y*other.y
end
def to_s
"(#{@x}, #{@y})"
end
end
class Vector
def initialize(*components)
@components = components.flatten
end
def dimension
dimension = @components.length
p "#{dimension}D"
end
def length
sum = 0
@components.chars.each do |ch|
sum += x*2
end
length = sqrt(sum)
end
def normalize
@components /= length
end
def [](index)
@components[index]
end
def []=(index, value)
@components[index] = value
end
def ==(other)
if @components.length == other.length
index = 0
equal = true
while index < @components.length
if @components[index] == other[index]
index += 1
elsif
equal = false
end
end
if equal == true
true
else
false
end
end
end
def +(vector_of_same_dimension_or_scalar)
end
def -(vector_of_same_dimension_or_scalar)
end
def *(scalar)
v = Vector.new
(0...dimension).each { |index| v[index] = self[index] * scalar }
v
end
def /(scalar)
v = Vector.new
(0...dimension).each { |index| v[index] = self[index] / scalar.to_f }
v
end
def dot(vector_of_same_dimension_or_scalar)
end
def to_s
result = '('
@components.each { |element| result << "#{element}, "}
result.strip!
result[result.length - 1] = ')'
result
end
end