Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 320 lines (267 sloc) 7.245 kB
98db488 @Mon-Ouie Initial commit
authored
1 module Ray
2 class Vector2
3 class << self
4 alias :[] :new
5 end
6
7 # @return [true, false] True if the receive is contained in the rect
8 def inside?(rect)
9 rect.to_rect.contain? self
10 end
11
12 # Opposite of #inside?
13 def outside?(rect)
14 !inside?(rect)
15 end
16
17 # @param [Ray::Vector2, #to_vector2] other
18 # @return [Ray::Vector2] (x + other.x, y + other.y)
19 def +(other)
20 other = other.to_vector2
21 Ray::Vector2[x + other.x, y + other.y]
22 end
23
24 # @param [Ray::Vector2, #to_vector2] other
25 # @return [Ray::Vector2] (x - other.x, y - other.y)
26 def -(other)
27 other = other.to_vector2
28 Ray::Vector2[x - other.x, y - other.y]
29 end
30
31 # @overload *(float)
32 # @param [Float] float
33 # @return [Ray::Vector2] (x * float, y * float)
34 #
35 # @overload *(vector)
36 # @param [Ray::Vector2, #to_vector2] vector
37 # @return [Ray::Vector2] (a.x * b.x, a.y * b.y)
38 def *(other)
39 if other.respond_to? :to_vector2
40 other = other.to_vector2
41 Ray::Vector2[x * other.x, y * other.y]
42 else
43 Ray::Vector2[x * other, y * other]
44 end
45 end
46
47 # @overload /(float)
48 # @param [Float] float
49 # @return [Ray::Vector2] (x / float, y / float)
50 #
51 # @overload /(vector)
52 # @param [Ray::Vector2, #to_vector2] vector
53 # @return [Ray::Vector2] (a.x / b.x, a.y / b.y)
54 def /(other)
55 if other.respond_to? :to_vector2
56 other = other.to_vector2
57 Ray::Vector2[x / other.x, y / other.y]
58 else
59 Ray::Vector2[x / other, y / other]
60 end
61 end
62
63
64 # @param [Ray::Vector2, #to_vector2]
65 # @return [Float] a.x * b.x + a.y * b.y
66 def dot(vector)
67 vector = vector.to_vector2
68 x * vector.x + y * vector.y
69 end
70
71 # @return [Ray::Vector2] (-x, -y)
72 def -@; Ray::Vector2[-x, -y] end
73
74 # @return [Ray::Vector2] (x, y)
75 def +@; self; end
76
77 # @return [Float] The length of the vector
78 def length
79 Math.sqrt(x * x + y * y)
80 end
81
82 alias :norm :length
83
84 # @return [Ray::Vector2] Normalized vector (i.e. length will be 1)
85 def normalize
86 self / length
87 end
88
89 # Normalizes the vector, by dividing it by its length.
90 def normalize!
91 length = self.length
92
93 self.x /= length
94 self.y /= length
95
96 self
97 end
98
99 # @param [Ray::Vector2, #to_vector2] other
100 # @return [Float] The distance between two vectors
101 def dist(other)
102 (self - other).length
103 end
9cdd06d @Mon-Ouie Aliased dist to distance (fixes #8)
authored
104 alias distance dist
98db488 @Mon-Ouie Initial commit
authored
105
106 def ==(other)
107 if other.is_a? Vector2
108 x == other.x && y == other.y
109 elsif other.is_a? Array
110 if other.size <= 2
111 other = other.to_vector2
112 x == other.x && y == other.y
113 else
114 false
115 end
116 elsif other.respond_to? :to_vector2
117 other = other.to_vector2
118 x == other.x && y == other.y
119 else
120 false
121 end
122 end
123
124 def eql?(other)
125 self.class == other.class && self == other
126 end
127
128 def hash
129 to_a.hash
130 end
131
132 def to_a
133 [x, y]
134 end
135
136 def to_vector2
137 self
138 end
139
140 def to_s
141 "(%g, %g)" % [x, y]
142 end
143
237bc82 @Mon-Ouie Added pretty_print to vector2
authored
144 def pretty_print(q)
145 q.text "("
b75a150 @Mon-Ouie Fixed warnings in 1.8.7
authored
146 q.pp(("%g" % x).to_f) # hides simple-precision inacurracy
237bc82 @Mon-Ouie Added pretty_print to vector2
authored
147 q.text ", "
b75a150 @Mon-Ouie Fixed warnings in 1.8.7
authored
148 q.pp(("%g" % y).to_f)
237bc82 @Mon-Ouie Added pretty_print to vector2
authored
149 q.text ")"
150 end
151
98db488 @Mon-Ouie Initial commit
authored
152 alias :w :x
153 alias :h :y
154
155 alias :width :w
156 alias :height :h
157
158 alias :w= :x=
159 alias :h= :y=
160
161 alias :width= :x=
162 alias :height= :y=
163 end
164
165 class Vector3
166 class << self
167 alias :[] :new
168 end
169
170 # @param [Ray::Vector3, #to_vector3] other
171 # @return [Ray::Vector3] (x + other.x, y + other.y, z + other.z)
172 def +(other)
173 other = other.to_vector3
174 Ray::Vector3[x + other.x, y + other.y, z + other.z]
175 end
176
177 # @param [Ray::Vector3, #to_vector3] other
178 # @return [Ray::Vector3] (x - other.x, y - other.y, z - other.z)
179 def -(other)
180 other = other.to_vector3
181 Ray::Vector3[x - other.x, y - other.y, z - other.z]
182 end
183
184 # @overload *(float)
185 # @param [Float] float
186 # @return [Ray::Vector3] (x * float, y * float, z * float)
187 #
188 # @overload *(vector)
189 # @param [Ray::Vector3, #to_vector3] vector
190 # @return [Ray::Vector3] (a.x * b.x, a.y * b.y, a.z * b.z)
191 def *(other)
192 if other.respond_to? :to_vector3
193 other = other.to_vector3
194 Ray::Vector3[x * other.x, y * other.y, z * other.z]
195 else
196 Ray::Vector3[x * other, y * other, z * other]
197 end
198 end
199
200 # @overload /(float)
201 # @param [Float] float
202 # @return [Ray::Vector3] (x / float, y / float, z / float)
203 #
204 # @overload /(vector)
205 # @param [Ray::Vector3, #to_vector3] vector
206 # @return [Ray::Vector3] (a.x / b.x, a.y / b.y, a.z / b.z)
207 def /(other)
208 if other.respond_to? :to_vector3
209 other = other.to_vector3
210 Ray::Vector3[x / other.x, y / other.y, z / other.z]
211 else
212 Ray::Vector3[x / other, y / other, z / other]
213 end
214 end
215
216 # @param [Ray::Vector3, #to_vector3] vector
217 # @return [Float] Dot product (i.e. x * vector.x + y * vector.y + z * vector.z)
218 def dot(vector)
219 vector = vector.to_vector3
220 x * vector.x + y * vector.y + z * vector.z
221 end
222
223 # @return [Ray::Vector3] (-x, -y, -z)
224 def -@; Ray::Vector3[-x, -y, -z] end
225
226 # @return [Ray::Vector3] (x, y, y)
227 def +@; self; end
228
229 # @return [Float] The length of the vector
230 def length
231 Math.sqrt(x * x + y * y + z * z)
232 end
233
234 alias :norm :length
235
236 # @return [Ray::Vector3] Normalized vector (i.e. length will be 1)
237 def normalize
238 self / length
239 end
240
241 # Normalizes the vector, by dividing it by its length.
242 def normalize!
243 length = self.length
244
245 self.x /= length
246 self.y /= length
247 self.z /= length
248
249 self
250 end
251
252 # @param [Ray::Vector3, #to_vector3] other
253 # @return [Float] The distance between two vectors
254 def dist(other)
255 (self - other).length
256 end
9cdd06d @Mon-Ouie Aliased dist to distance (fixes #8)
authored
257 alias distance dist
98db488 @Mon-Ouie Initial commit
authored
258
259 def ==(other)
260 if other.is_a? Vector3
261 x == other.x && y == other.y && z == other.z
262 elsif other.is_a? Array
263 if other.size <= 3
264 other = other.to_vector3
265 x == other.x && y == other.y && z == other.z
266 else
267 false
268 end
269 elsif other.respond_to? :to_vector3
270 other = other.to_vector3
271 x == other.x && y == other.y && z == other.z
272 else
273 false
274 end
275 end
276
277 def eql?(other)
278 self.class == other.class && self == other
279 end
280
281 def hash
282 to_a.hash
283 end
284
285 def to_s
286 "(%g, %g, %g)" % [x, y, z]
287 end
288
356131a @Mon-Ouie Added pretty print to vector3
authored
289 def pretty_print(q)
290 q.text "("
b75a150 @Mon-Ouie Fixed warnings in 1.8.7
authored
291 q.pp(("%g" % x).to_f) # hides simple-precision inacurracy
356131a @Mon-Ouie Added pretty print to vector3
authored
292 q.text ", "
b75a150 @Mon-Ouie Fixed warnings in 1.8.7
authored
293 q.pp(("%g" % y).to_f)
356131a @Mon-Ouie Added pretty print to vector3
authored
294 q.text ", "
b75a150 @Mon-Ouie Fixed warnings in 1.8.7
authored
295 q.pp(("%g" % z).to_f)
356131a @Mon-Ouie Added pretty print to vector3
authored
296 q.text ")"
297 end
298
98db488 @Mon-Ouie Initial commit
authored
299 def to_a
300 [x, y, z]
301 end
302
303 def to_vector3
304 self
305 end
306 end
307 end
308
309 class Array
310 # @return [Ray::Vector2] Converts an array into a vector2.
311 def to_vector2
312 Ray::Vector2[*self]
313 end
314
315 # @return [Ray::Vector3] Converts an array into a vector3.
316 def to_vector3
317 Ray::Vector3[*self]
318 end
319 end
Something went wrong with that request. Please try again.