@@ -221,6 +221,17 @@ pub fn (v Vec2[T]) magnitude_y() T {
221221}
222222
223223// dot returns the dot product of `v` and `u`.
224+ // The dot product is a scalar value that represents the magnitude of one vector
225+ // projected onto another vector.
226+ // It is calculated by multiplying the corresponding components of the vectors
227+ // and summing the results.
228+ // example:
229+ // ```v
230+ // v := vec2[f32](3, 4) //magnitude = 5
231+ // u := vec2[f32](5, 6) //magnitude = 7.81
232+ // dot := v.dot(u) // 3*5 + 4*6 = 15 + 24 = 39
233+ // (dot) // Output: 39
234+ // ```
224235pub fn (v Vec2[T]) dot (u Vec2 [T]) T {
225236 return (v.x * u.x) + (v.y * u.y)
226237}
@@ -253,9 +264,22 @@ pub fn (v Vec2[T]) perpendicular(u Vec2[T]) Vec2[T] {
253264}
254265
255266// project returns the projected vector.
267+ // The projection of vector `u` onto vector `v` is the orthogonal projection
268+ // of `u` onto a straight line parallel to `v` that passes through the origin.
269+ // This is equivalent to the vector projection of `u` onto the unit vector in the direction of `v`.
270+ // and is given by the formula: proj_v(u) = (u · v / |v|^2) * v
271+ // where "·" denotes the dot product and |v| is the magnitude of vector `v`.
272+ // If `u` is a zero vector, the result will also be a zero vector.
273+ // example:
274+ // ```v
275+ // v := vec2[f32](3, 4)
276+ // u := vec2[f32](5, 6)
277+ // proj := v.project(u)
278+ // println(proj) // Output: vec2[f32](3.61, 4.81)
279+ // ```
256280pub fn (v Vec2[T]) project (u Vec2 [T]) Vec2 [T] {
257- percent := v .dot (u ) / u .dot (v)
258- return u .mul_scalar (percent )
281+ scale := u .dot (v ) / v .dot (v)
282+ return v .mul_scalar (scale )
259283}
260284
261285// rotate_around_cw returns the vector `v` rotated *clockwise* `radians` around an origin vector `o` in Cartesian space.
@@ -351,6 +375,15 @@ pub fn (p1 Vec2[T]) angle_towards(p2 Vec2[T]) T {
351375}
352376
353377// angle returns the angle in radians of the vector.
378+ // example:
379+ // ```v
380+ // v := vec2[f32](3.0, 4.0)
381+ // a := v.angle()
382+ // assert a == 0.64 (approximate value in radians)
383+ // w := vec2[f32](0.0, 1.0)
384+ // b := w.angle()
385+ // assert b == 1.57 (approximate value in radians)
386+ // ```
354387pub fn (v Vec2[T]) angle () T {
355388 $if T is f64 {
356389 return math.atan2 (v.y, v.x)
@@ -361,12 +394,8 @@ pub fn (v Vec2[T]) angle() T {
361394
362395// abs sets `x` and `y` field values to their absolute values.
363396pub fn (mut v Vec2[T]) abs () {
364- if v.x < 0 {
365- v.x = math.abs (v.x)
366- }
367- if v.y < 0 {
368- v.y = math.abs (v.y)
369- }
397+ v.x = math.abs (v.x)
398+ v.y = math.abs (v.y)
370399}
371400
372401// clean returns a vector with all fields of this vector set to zero (0) if they fall within `tolerance`.
@@ -392,6 +421,14 @@ pub fn (mut v Vec2[T]) clean_tolerance[U](tolerance U) {
392421}
393422
394423// inv returns the inverse, or reciprocal, of the vector.
424+ // If a field is zero, its inverse is also set to zero to avoid division by zero.
425+ // the direction the vector points is generally not preserved, but
426+ // the magnitude of each field is inverted.
427+ // example:
428+ // ```v
429+ // v := vec2[f32](2.0, 4.0)
430+ // inv_v := v.inv() // inv_v == vec2[f32](0.5, 0.25)
431+ // ```
395432pub fn (v Vec2[T]) inv () Vec2 [T] {
396433 return Vec2 [T]{
397434 x: if v.x != 0 { T (1 ) / v.x } else { 0 }
@@ -400,6 +437,13 @@ pub fn (v Vec2[T]) inv() Vec2[T] {
400437}
401438
402439// normalize normalizes the vector.
440+ // A normalized vector has the same direction as the original vector but a magnitude of 1.
441+ // If the vector has a magnitude of 0, a zero vector is returned since we cannot find the direction of a zero-length vector.
442+ // example:
443+ // ```v
444+ // v := vec2[f32](3.0, 4.0)//magnitude = 5.0
445+ // n := v.normalize() // n == vec2[f32](0.6, 0.8) // magnitude = 1.0
446+ // ```
403447pub fn (v Vec2[T]) normalize () Vec2 [T] {
404448 m := v.magnitude ()
405449 if m == 0 {
@@ -412,6 +456,12 @@ pub fn (v Vec2[T]) normalize() Vec2[T] {
412456}
413457
414458// sum returns a sum of all the fields.
459+ // example:
460+ // ```v
461+ // v := vec2[f32](3.0, 4.0)
462+ // s := v.sum()
463+ // assert s == 7.0
464+ // ```
415465pub fn (v Vec2[T]) sum () T {
416466 return v.x + v.y
417467}
0 commit comments