Skip to content

Commit c1bbe9f

Browse files
committed
Finished vector methods
1 parent 1011dc0 commit c1bbe9f

24 files changed

+3593
-0
lines changed

src/main/scala/vecmatlib/vector/Vec2d.scala

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,84 @@ case class Vec2d(x: Double, y: Double) extends Double2 with VecDouble[Vec2d] {
9191

9292
override def lengthSquared: Double = this dot this
9393

94+
/**
95+
* Returns the angle in radians between this vector and the one with the given components.
96+
*
97+
* @param x X component of the second vector
98+
* @param y Y component of the second vector
99+
* @return The angle in radians between this vector and the one with the given components
100+
*/
101+
def angle(x: Double, y: Double): Double = this.angle(Vec2d(x, y))
102+
103+
/**
104+
* Returns the normalized vector pointing from this vector to the one with the given components.
105+
*
106+
* Using `a.directionTo(b)` is equivalent to using `(b - a).normalized`.
107+
*
108+
* @param x X component of the second vector
109+
* @param y Y component of the second vector
110+
* @return The normalized vector pointing from this vector to the given one
111+
*/
112+
def directionTo(x: Double, y: Double): Vec2d = this.directionTo(Vec2d(x, y))
113+
114+
/**
115+
* Returns the squared distance between this vector and the one with the given components.
116+
*
117+
* Using `a.distanceSquaredTo(b)` is equivalent to using `(b - a).lengthSquared`.
118+
*
119+
* @param x X component of the second vector
120+
* @param y Y component of the second vector
121+
* @return The squared distance between this vector and the one with the given components
122+
*/
123+
def distanceSquaredTo(x: Double, y: Double): Double = this.distanceSquaredTo(Vec2d(x, y))
124+
125+
/**
126+
* Returns the distance between this vector and the one with the given components.
127+
*
128+
* Using `a.distanceTo(b)` is equivalent to using `(b - a).length`.
129+
*
130+
* @param x X component of the second vector
131+
* @param y Y component of the second vector
132+
* @return The distance between this vector and the one with the given components
133+
*/
134+
def distanceTo(x: Double, y: Double): Double = this.distanceTo(Vec2d(x, y))
135+
136+
/**
137+
* Returns this vector reflected from a plane defined by the given normal (must be normalized).
138+
*
139+
* @param x X component of the normal
140+
* @param y Y component of the normal
141+
* @return This vector reflected from a plane defined by the given normal
142+
*/
143+
def reflect(x: Double, y: Double): Vec2d = this.reflect(Vec2d(x, y))
144+
145+
/**
146+
* Returns this vector "bounced off" from a plane defined by the given normal (must be normalized).
147+
*
148+
* @param x X component of the normal
149+
* @param y Y component of the normal
150+
* @return This vector "bounced off" from a plane defined by the given normal
151+
*/
152+
def bounce(x: Double, y: Double): Vec2d = this.bounce(Vec2d(x, y))
153+
154+
/**
155+
* Returns this vector projected onto the one with the given components.
156+
*
157+
* @param x X component of the vector to project onto
158+
* @param y Y component of the vector to project onto
159+
* @return This vector projected onto the one with the given components
160+
*/
161+
def project(x: Double, y: Double): Vec2d = this.project(Vec2d(x, y))
162+
163+
/**
164+
* Returns this vector slid along a plane defined by the normal with the given components.
165+
*
166+
* @param x X component of the normal
167+
* @param y Y component of the normal
168+
* @return This vector slid along a plane defined by the given normal
169+
*/
170+
def slide(x: Double, y: Double): Vec2d = this.slide(Vec2d(x, y))
171+
94172
/**
95173
* Casts this vector to an int vector.
96174
*

src/main/scala/vecmatlib/vector/Vec2f.scala

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,131 @@ case class Vec2f(x: Float, y: Float) extends Float2 with VecFloat[Vec2f] with Ve
169169

170170
override def toDouble: Vec2d = Vec2d(this.x.toDouble, this.y.toDouble)
171171

172+
/**
173+
* Returns the angle in radians between this vector and the one with the given components.
174+
*
175+
* @param x X component of the second vector
176+
* @param y Y component of the second vector
177+
* @return The angle in radians between this vector and the one with the given components
178+
*/
179+
def angle(x: Double, y: Double): Double = this.angle(Vec2d(x, y))
180+
181+
/**
182+
* Returns the normalized vector pointing from this vector to the one with the given components.
183+
*
184+
* Using `a.directionTo(b)` is equivalent to using `(b - a).normalized`.
185+
*
186+
* @param x X component of the second vector
187+
* @param y Y component of the second vector
188+
* @return The normalized vector pointing from this vector to the given one
189+
*/
190+
def directionTo(x: Double, y: Double): Vec2d = this.directionTo(Vec2d(x, y))
191+
192+
/**
193+
* Returns the squared distance between this vector and the one with the given components.
194+
*
195+
* Using `a.distanceSquaredTo(b)` is equivalent to using `(b - a).lengthSquared`.
196+
*
197+
* @param x X component of the second vector
198+
* @param y Y component of the second vector
199+
* @return The squared distance between this vector and the one with the given components
200+
*/
201+
def distanceSquaredTo(x: Float, y: Float): Float = this.distanceSquaredTo(Vec2f(x, y))
202+
203+
/**
204+
* Returns the squared distance between this vector and the one with the given components.
205+
*
206+
* Using `a.distanceSquaredTo(b)` is equivalent to using `(b - a).lengthSquared`.
207+
*
208+
* @param x X component of the second vector
209+
* @param y Y component of the second vector
210+
* @return The squared distance between this vector and the one with the given components
211+
*/
212+
def distanceSquaredTo(x: Double, y: Double): Double = this.distanceSquaredTo(Vec2d(x, y))
213+
214+
/**
215+
* Returns the distance between this vector and the one with the given components.
216+
*
217+
* Using `a.distanceTo(b)` is equivalent to using `(b - a).length`.
218+
*
219+
* @param x X component of the second vector
220+
* @param y Y component of the second vector
221+
* @return The distance between this vector and the one with the given components
222+
*/
223+
def distanceTo(x: Double, y: Double): Double = this.distanceTo(Vec2d(x, y))
224+
225+
/**
226+
* Returns this vector reflected from a plane defined by the given normal (must be normalized).
227+
*
228+
* @param x X component of the normal
229+
* @param y Y component of the normal
230+
* @return This vector reflected from a plane defined by the given normal
231+
*/
232+
def reflect(x: Float, y: Float): Vec2f = this.reflect(Vec2f(x, y))
233+
234+
/**
235+
* Returns this vector reflected from a plane defined by the given normal (must be normalized).
236+
*
237+
* @param x X component of the normal
238+
* @param y Y component of the normal
239+
* @return This vector reflected from a plane defined by the given normal
240+
*/
241+
def reflect(x: Double, y: Double): Vec2d = this.reflect(Vec2d(x, y))
242+
243+
/**
244+
* Returns this vector "bounced off" from a plane defined by the given normal (must be normalized).
245+
*
246+
* @param x X component of the normal
247+
* @param y Y component of the normal
248+
* @return This vector "bounced off" from a plane defined by the given normal
249+
*/
250+
def bounce(x: Float, y: Float): Vec2f = this.bounce(Vec2f(x, y))
251+
252+
/**
253+
* Returns this vector "bounced off" from a plane defined by the given normal (must be normalized).
254+
*
255+
* @param x X component of the normal
256+
* @param y Y component of the normal
257+
* @return This vector "bounced off" from a plane defined by the given normal
258+
*/
259+
def bounce(x: Double, y: Double): Vec2d = this.bounce(Vec2d(x, y))
260+
261+
/**
262+
* Returns this vector projected onto the one with the given components.
263+
*
264+
* @param x X component of the vector to project onto
265+
* @param y Y component of the vector to project onto
266+
* @return This vector projected onto the one with the given components
267+
*/
268+
def project(x: Float, y: Float): Vec2f = this.project(Vec2f(x, y))
269+
270+
/**
271+
* Returns this vector projected onto the one with the given components.
272+
*
273+
* @param x X component of the vector to project onto
274+
* @param y Y component of the vector to project onto
275+
* @return This vector projected onto the one with the given components
276+
*/
277+
def project(x: Double, y: Double): Vec2d = this.project(Vec2d(x, y))
278+
279+
/**
280+
* Returns this vector slid along a plane defined by the normal with the given components.
281+
*
282+
* @param x X component of the normal
283+
* @param y Y component of the normal
284+
* @return This vector slid along a plane defined by the given normal
285+
*/
286+
def slide(x: Float, y: Float): Vec2f = this.slide(Vec2f(x, y))
287+
288+
/**
289+
* Returns this vector slid along a plane defined by the normal with the given components.
290+
*
291+
* @param x X component of the normal
292+
* @param y Y component of the normal
293+
* @return This vector slid along a plane defined by the given normal
294+
*/
295+
def slide(x: Double, y: Double): Vec2d = this.slide(Vec2d(x, y))
296+
172297
/**
173298
* Checks if the components of this vector are equal to the given ones.
174299
*

src/main/scala/vecmatlib/vector/Vec2i.scala

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,178 @@ case class Vec2i(x: Int, y: Int) extends Int2 with VecInt[Vec2i] with VecAsFloat
231231

232232
override def lengthSquared: Int = this dot this
233233

234+
/**
235+
* Returns the angle in radians between this vector and the one with the given components.
236+
*
237+
* @param x X component of the second vector
238+
* @param y Y component of the second vector
239+
* @return The angle in radians between this vector and the one with the given components
240+
*/
241+
def angle(x: Double, y: Double): Double = this.angle(Vec2d(x, y))
242+
243+
/**
244+
* Returns the normalized vector pointing from this vector to the one with the given components.
245+
*
246+
* Using `a.directionTo(b)` is equivalent to using `(b - a).normalized`.
247+
*
248+
* @param x X component of the second vector
249+
* @param y Y component of the second vector
250+
* @return The normalized vector pointing from this vector to the given one
251+
*/
252+
def directionTo(x: Double, y: Double): Vec2d = this.directionTo(Vec2d(x, y))
253+
254+
/**
255+
* Returns the squared distance between this vector and the one with the given components.
256+
*
257+
* Using `a.distanceSquaredTo(b)` is equivalent to using `(b - a).lengthSquared`.
258+
*
259+
* @param x X component of the second vector
260+
* @param y Y component of the second vector
261+
* @return The squared distance between this vector and the one with the given components
262+
*/
263+
def distanceSquaredTo(x: Int, y: Int): Int = this.distanceSquaredTo(Vec2i(x, y))
264+
265+
/**
266+
* Returns the squared distance between this vector and the one with the given components.
267+
*
268+
* Using `a.distanceSquaredTo(b)` is equivalent to using `(b - a).lengthSquared`.
269+
*
270+
* @param x X component of the second vector
271+
* @param y Y component of the second vector
272+
* @return The squared distance between this vector and the one with the given components
273+
*/
274+
def distanceSquaredTo(x: Float, y: Float): Float = this.distanceSquaredTo(Vec2f(x, y))
275+
276+
/**
277+
* Returns the squared distance between this vector and the one with the given components.
278+
*
279+
* Using `a.distanceSquaredTo(b)` is equivalent to using `(b - a).lengthSquared`.
280+
*
281+
* @param x X component of the second vector
282+
* @param y Y component of the second vector
283+
* @return The squared distance between this vector and the one with the given components
284+
*/
285+
def distanceSquaredTo(x: Double, y: Double): Double = this.distanceSquaredTo(Vec2d(x, y))
286+
287+
/**
288+
* Returns the distance between this vector and the one with the given components.
289+
*
290+
* Using `a.distanceTo(b)` is equivalent to using `(b - a).length`.
291+
*
292+
* @param x X component of the second vector
293+
* @param y Y component of the second vector
294+
* @return The distance between this vector and the one with the given components
295+
*/
296+
def distanceTo(x: Double, y: Double): Double = this.distanceTo(Vec2d(x, y))
297+
298+
/**
299+
* Returns this vector reflected from a plane defined by the given normal (must be normalized).
300+
*
301+
* @param x X component of the normal
302+
* @param y Y component of the normal
303+
* @return This vector reflected from a plane defined by the given normal
304+
*/
305+
def reflect(x: Int, y: Int): Vec2i = this.reflect(Vec2i(x, y))
306+
307+
/**
308+
* Returns this vector reflected from a plane defined by the given normal (must be normalized).
309+
*
310+
* @param x X component of the normal
311+
* @param y Y component of the normal
312+
* @return This vector reflected from a plane defined by the given normal
313+
*/
314+
def reflect(x: Float, y: Float): Vec2f = this.reflect(Vec2f(x, y))
315+
316+
/**
317+
* Returns this vector reflected from a plane defined by the given normal (must be normalized).
318+
*
319+
* @param x X component of the normal
320+
* @param y Y component of the normal
321+
* @return This vector reflected from a plane defined by the given normal
322+
*/
323+
def reflect(x: Double, y: Double): Vec2d = this.reflect(Vec2d(x, y))
324+
325+
/**
326+
* Returns this vector "bounced off" from a plane defined by the given normal (must be normalized).
327+
*
328+
* @param x X component of the normal
329+
* @param y Y component of the normal
330+
* @return This vector "bounced off" from a plane defined by the given normal
331+
*/
332+
def bounce(x: Int, y: Int): Vec2i = this.bounce(Vec2i(x, y))
333+
334+
/**
335+
* Returns this vector "bounced off" from a plane defined by the given normal (must be normalized).
336+
*
337+
* @param x X component of the normal
338+
* @param y Y component of the normal
339+
* @return This vector "bounced off" from a plane defined by the given normal
340+
*/
341+
def bounce(x: Float, y: Float): Vec2f = this.bounce(Vec2f(x, y))
342+
343+
/**
344+
* Returns this vector "bounced off" from a plane defined by the given normal (must be normalized).
345+
*
346+
* @param x X component of the normal
347+
* @param y Y component of the normal
348+
* @return This vector "bounced off" from a plane defined by the given normal
349+
*/
350+
def bounce(x: Double, y: Double): Vec2d = this.bounce(Vec2d(x, y))
351+
352+
/**
353+
* Returns this vector projected onto the one with the given components.
354+
*
355+
* @param x X component of the vector to project onto
356+
* @param y Y component of the vector to project onto
357+
* @return This vector projected onto the one with the given components
358+
*/
359+
def project(x: Int, y: Int): Vec2i = this.project(Vec2i(x, y))
360+
361+
/**
362+
* Returns this vector projected onto the one with the given components.
363+
*
364+
* @param x X component of the vector to project onto
365+
* @param y Y component of the vector to project onto
366+
* @return This vector projected onto the one with the given components
367+
*/
368+
def project(x: Float, y: Float): Vec2f = this.project(Vec2f(x, y))
369+
370+
/**
371+
* Returns this vector projected onto the one with the given components.
372+
*
373+
* @param x X component of the vector to project onto
374+
* @param y Y component of the vector to project onto
375+
* @return This vector projected onto the one with the given components
376+
*/
377+
def project(x: Double, y: Double): Vec2d = this.project(Vec2d(x, y))
378+
379+
/**
380+
* Returns this vector slid along a plane defined by the normal with the given components.
381+
*
382+
* @param x X component of the normal
383+
* @param y Y component of the normal
384+
* @return This vector slid along a plane defined by the given normal
385+
*/
386+
def slide(x: Int, y: Int): Vec2i = this.slide(Vec2i(x, y))
387+
388+
/**
389+
* Returns this vector slid along a plane defined by the normal with the given components.
390+
*
391+
* @param x X component of the normal
392+
* @param y Y component of the normal
393+
* @return This vector slid along a plane defined by the given normal
394+
*/
395+
def slide(x: Float, y: Float): Vec2f = this.slide(Vec2f(x, y))
396+
397+
/**
398+
* Returns this vector slid along a plane defined by the normal with the given components.
399+
*
400+
* @param x X component of the normal
401+
* @param y Y component of the normal
402+
* @return This vector slid along a plane defined by the given normal
403+
*/
404+
def slide(x: Double, y: Double): Vec2d = this.slide(Vec2d(x, y))
405+
234406
override def toFloat: Vec2f = Vec2f(this.x.toFloat, this.y.toFloat)
235407

236408
override def toDouble: Vec2d = Vec2d(this.x.toDouble, this.y.toDouble)

0 commit comments

Comments
 (0)