Skip to main content

raylib_sys/
vector_math.rs

1use crate::{Matrix, Quaternion, Vector2, Vector3, Vector4};
2
3// ─── Vector2 ─────────────────────────────────────────────────────────────────
4
5impl Vector2 {
6    /// The zero vector `(0, 0)`.
7    pub const ZERO: Self = Self { x: 0.0, y: 0.0 };
8    /// The vector `(1, 1)`.
9    pub const ONE: Self = Self { x: 1.0, y: 1.0 };
10
11    /// Construct a new Vector2.
12    #[inline]
13    pub fn new(x: f32, y: f32) -> Self {
14        Self { x, y }
15    }
16
17    /// Zero vector. (raymath `Vector2Zero`)
18    #[inline]
19    #[must_use]
20    pub fn zero() -> Self {
21        // SAFETY: Vector2Zero is a pure value-out raymath fn; no preconditions.
22        unsafe { crate::Vector2Zero() }
23    }
24
25    /// Vector with both components set to 1. (raymath `Vector2One`)
26    #[inline]
27    #[must_use]
28    pub fn one() -> Self {
29        // SAFETY: Vector2One is a pure value-out raymath fn; no preconditions.
30        unsafe { crate::Vector2One() }
31    }
32
33    /// Add scalar to both components. (raymath `Vector2AddValue`)
34    #[inline]
35    #[must_use]
36    pub fn add_value(self, add: f32) -> Self {
37        // SAFETY: pure value-in/out, no preconditions.
38        unsafe { crate::Vector2AddValue(self, add) }
39    }
40
41    /// Subtract scalar from both components. (raymath `Vector2SubtractValue`)
42    #[inline]
43    #[must_use]
44    pub fn sub_value(self, sub: f32) -> Self {
45        // SAFETY: pure value-in/out, no preconditions.
46        unsafe { crate::Vector2SubtractValue(self, sub) }
47    }
48
49    /// Vector length. (raymath `Vector2Length`)
50    #[inline]
51    #[must_use]
52    pub fn length(self) -> f32 {
53        // SAFETY: pure value-in/out, no preconditions.
54        unsafe { crate::Vector2Length(self) }
55    }
56
57    /// Squared vector length. (raymath `Vector2LengthSqr`)
58    #[inline]
59    #[must_use]
60    pub fn length_sqr(self) -> f32 {
61        // SAFETY: pure value-in/out, no preconditions.
62        unsafe { crate::Vector2LengthSqr(self) }
63    }
64
65    /// Dot product. (raymath `Vector2DotProduct`)
66    #[inline]
67    #[must_use]
68    pub fn dot(self, other: Vector2) -> f32 {
69        // SAFETY: pure value-in/out, no preconditions.
70        unsafe { crate::Vector2DotProduct(self, other) }
71    }
72
73    /// Cross product (scalar). (raymath `Vector2CrossProduct`)
74    #[inline]
75    #[must_use]
76    pub fn cross(self, other: Vector2) -> f32 {
77        // SAFETY: pure value-in/out, no preconditions.
78        unsafe { crate::Vector2CrossProduct(self, other) }
79    }
80
81    /// Distance between two vectors. (raymath `Vector2Distance`)
82    #[inline]
83    #[must_use]
84    pub fn distance(self, other: Vector2) -> f32 {
85        // SAFETY: pure value-in/out, no preconditions.
86        unsafe { crate::Vector2Distance(self, other) }
87    }
88
89    /// Squared distance between two vectors. (raymath `Vector2DistanceSqr`)
90    #[inline]
91    #[must_use]
92    pub fn distance_sqr(self, other: Vector2) -> f32 {
93        // SAFETY: pure value-in/out, no preconditions.
94        unsafe { crate::Vector2DistanceSqr(self, other) }
95    }
96
97    /// Angle between two vectors (radians). (raymath `Vector2Angle`)
98    #[inline]
99    #[must_use]
100    pub fn angle(self, other: Vector2) -> f32 {
101        // SAFETY: pure value-in/out, no preconditions.
102        unsafe { crate::Vector2Angle(self, other) }
103    }
104
105    /// Angle of line defined by two points. (raymath `Vector2LineAngle`)
106    #[inline]
107    #[must_use]
108    pub fn line_angle(start: Vector2, end: Vector2) -> f32 {
109        // SAFETY: pure value-in/out, no preconditions.
110        unsafe { crate::Vector2LineAngle(start, end) }
111    }
112
113    /// Scale vector by scalar. (raymath `Vector2Scale`)
114    #[inline]
115    #[must_use]
116    pub fn scale(self, scale: f32) -> Self {
117        // SAFETY: pure value-in/out, no preconditions.
118        unsafe { crate::Vector2Scale(self, scale) }
119    }
120
121    /// Component-wise multiply. (raymath `Vector2Multiply`)
122    #[inline]
123    #[must_use]
124    pub fn multiply(self, other: Vector2) -> Self {
125        // SAFETY: pure value-in/out, no preconditions.
126        unsafe { crate::Vector2Multiply(self, other) }
127    }
128
129    /// Negate vector. (raymath `Vector2Negate`)
130    #[inline]
131    #[must_use]
132    pub fn negate(self) -> Self {
133        // SAFETY: pure value-in/out, no preconditions.
134        unsafe { crate::Vector2Negate(self) }
135    }
136
137    /// Component-wise divide. (raymath `Vector2Divide`)
138    #[inline]
139    #[must_use]
140    pub fn divide(self, other: Vector2) -> Self {
141        // SAFETY: pure value-in/out, no preconditions.
142        unsafe { crate::Vector2Divide(self, other) }
143    }
144
145    /// Normalize vector. (raymath `Vector2Normalize`)
146    #[inline]
147    #[must_use]
148    pub fn normalize(self) -> Self {
149        // SAFETY: pure value-in/out, no preconditions.
150        unsafe { crate::Vector2Normalize(self) }
151    }
152
153    /// Transform by matrix. (raymath `Vector2Transform`)
154    #[inline]
155    #[must_use]
156    pub fn transform(self, mat: Matrix) -> Self {
157        // SAFETY: pure value-in/out, no preconditions.
158        unsafe { crate::Vector2Transform(self, mat) }
159    }
160
161    /// Linear interpolation. (raymath `Vector2Lerp`)
162    #[inline]
163    #[must_use]
164    pub fn lerp(self, other: Vector2, amount: f32) -> Self {
165        // SAFETY: pure value-in/out, no preconditions.
166        unsafe { crate::Vector2Lerp(self, other, amount) }
167    }
168
169    /// Reflect vector about normal. (raymath `Vector2Reflect`)
170    #[inline]
171    #[must_use]
172    pub fn reflect(self, normal: Vector2) -> Self {
173        // SAFETY: pure value-in/out, no preconditions.
174        unsafe { crate::Vector2Reflect(self, normal) }
175    }
176
177    /// Component-wise minimum. (raymath `Vector2Min`)
178    #[inline]
179    #[must_use]
180    pub fn min(self, other: Vector2) -> Self {
181        // SAFETY: pure value-in/out, no preconditions.
182        unsafe { crate::Vector2Min(self, other) }
183    }
184
185    /// Component-wise maximum. (raymath `Vector2Max`)
186    #[inline]
187    #[must_use]
188    pub fn max(self, other: Vector2) -> Self {
189        // SAFETY: pure value-in/out, no preconditions.
190        unsafe { crate::Vector2Max(self, other) }
191    }
192
193    /// Rotate by angle (radians). (raymath `Vector2Rotate`)
194    #[inline]
195    #[must_use]
196    pub fn rotate(self, angle: f32) -> Self {
197        // SAFETY: pure value-in/out, no preconditions.
198        unsafe { crate::Vector2Rotate(self, angle) }
199    }
200
201    /// Move towards target by at most maxDistance. (raymath `Vector2MoveTowards`)
202    #[inline]
203    #[must_use]
204    pub fn move_towards(self, target: Vector2, max_distance: f32) -> Self {
205        // SAFETY: pure value-in/out, no preconditions.
206        unsafe { crate::Vector2MoveTowards(self, target, max_distance) }
207    }
208
209    /// Invert components (1/x, 1/y). (raymath `Vector2Invert`)
210    #[inline]
211    #[must_use]
212    pub fn invert(self) -> Self {
213        // SAFETY: pure value-in/out, no preconditions.
214        unsafe { crate::Vector2Invert(self) }
215    }
216
217    /// Clamp components between min and max vectors. (raymath `Vector2Clamp`)
218    #[inline]
219    #[must_use]
220    pub fn clamp(self, min: Vector2, max: Vector2) -> Self {
221        // SAFETY: pure value-in/out, no preconditions.
222        unsafe { crate::Vector2Clamp(self, min, max) }
223    }
224
225    /// Clamp length between min and max scalars. (raymath `Vector2ClampValue`)
226    #[inline]
227    #[must_use]
228    pub fn clamp_value(self, min: f32, max: f32) -> Self {
229        // SAFETY: pure value-in/out, no preconditions.
230        unsafe { crate::Vector2ClampValue(self, min, max) }
231    }
232
233    /// Approximate equality (uses raymath epsilon). (raymath `Vector2Equals`)
234    #[inline]
235    #[must_use]
236    pub fn equals(self, other: Vector2) -> bool {
237        // SAFETY: pure value-in/out, no preconditions.
238        unsafe { crate::Vector2Equals(self, other) != 0 }
239    }
240
241    /// Refract through surface with index ratio r. (raymath `Vector2Refract`)
242    #[inline]
243    #[must_use]
244    pub fn refract(self, n: Vector2, r: f32) -> Self {
245        // SAFETY: pure value-in/out, no preconditions.
246        unsafe { crate::Vector2Refract(self, n, r) }
247    }
248}
249
250impl From<(f32, f32)> for Vector2 {
251    /// Construct from an `(x, y)` tuple.
252    #[inline]
253    fn from((x, y): (f32, f32)) -> Self {
254        Vector2 { x, y }
255    }
256}
257impl From<[f32; 2]> for Vector2 {
258    /// Construct from an `[x, y]` array.
259    #[inline]
260    fn from([x, y]: [f32; 2]) -> Self {
261        Vector2 { x, y }
262    }
263}
264
265impl core::ops::Add for Vector2 {
266    type Output = Vector2;
267    #[inline]
268    fn add(self, rhs: Vector2) -> Vector2 {
269        // SAFETY: Vector2Add is a pure value-in/out raymath fn; no preconditions.
270        unsafe { crate::Vector2Add(self, rhs) }
271    }
272}
273impl core::ops::AddAssign for Vector2 {
274    #[inline]
275    fn add_assign(&mut self, rhs: Vector2) {
276        *self = *self + rhs;
277    }
278}
279
280impl core::ops::Sub for Vector2 {
281    type Output = Vector2;
282    #[inline]
283    fn sub(self, rhs: Vector2) -> Vector2 {
284        // SAFETY: Vector2Subtract is a pure value-in/out raymath fn; no preconditions.
285        unsafe { crate::Vector2Subtract(self, rhs) }
286    }
287}
288impl core::ops::SubAssign for Vector2 {
289    #[inline]
290    fn sub_assign(&mut self, rhs: Vector2) {
291        *self = *self - rhs;
292    }
293}
294
295impl core::ops::Mul<f32> for Vector2 {
296    type Output = Vector2;
297    #[inline]
298    fn mul(self, rhs: f32) -> Vector2 {
299        // SAFETY: Vector2Scale is a pure value-in/out raymath fn; no preconditions.
300        unsafe { crate::Vector2Scale(self, rhs) }
301    }
302}
303impl core::ops::MulAssign<f32> for Vector2 {
304    #[inline]
305    fn mul_assign(&mut self, rhs: f32) {
306        *self = *self * rhs;
307    }
308}
309
310impl core::ops::Mul<Vector2> for Vector2 {
311    type Output = Vector2;
312    #[inline]
313    fn mul(self, rhs: Vector2) -> Vector2 {
314        // SAFETY: Vector2Multiply is a pure value-in/out raymath fn (component-wise); no preconditions.
315        unsafe { crate::Vector2Multiply(self, rhs) }
316    }
317}
318
319impl core::ops::Div<Vector2> for Vector2 {
320    type Output = Vector2;
321    #[inline]
322    fn div(self, rhs: Vector2) -> Vector2 {
323        // SAFETY: Vector2Divide is a pure value-in/out raymath fn (component-wise); no preconditions.
324        unsafe { crate::Vector2Divide(self, rhs) }
325    }
326}
327
328impl core::ops::Neg for Vector2 {
329    type Output = Vector2;
330    #[inline]
331    fn neg(self) -> Vector2 {
332        // SAFETY: Vector2Negate is a pure value-in/out raymath fn; no preconditions.
333        unsafe { crate::Vector2Negate(self) }
334    }
335}
336
337// ─── Vector3 ─────────────────────────────────────────────────────────────────
338
339impl Vector3 {
340    /// The zero vector `(0, 0, 0)`.
341    pub const ZERO: Self = Self {
342        x: 0.0,
343        y: 0.0,
344        z: 0.0,
345    };
346    /// The vector `(1, 1, 1)`.
347    pub const ONE: Self = Self {
348        x: 1.0,
349        y: 1.0,
350        z: 1.0,
351    };
352    /// The unit vector along +X `(1, 0, 0)`.
353    pub const X: Self = Self {
354        x: 1.0,
355        y: 0.0,
356        z: 0.0,
357    };
358    /// The unit vector along +Y `(0, 1, 0)`.
359    pub const Y: Self = Self {
360        x: 0.0,
361        y: 1.0,
362        z: 0.0,
363    };
364    /// The unit vector along +Z `(0, 0, 1)`.
365    pub const Z: Self = Self {
366        x: 0.0,
367        y: 0.0,
368        z: 1.0,
369    };
370
371    /// Construct a new Vector3.
372    #[inline]
373    pub fn new(x: f32, y: f32, z: f32) -> Self {
374        Self { x, y, z }
375    }
376
377    /// Zero vector. (raymath `Vector3Zero`)
378    #[inline]
379    #[must_use]
380    pub fn zero() -> Self {
381        // SAFETY: Vector3Zero is a pure value-out raymath fn; no preconditions.
382        unsafe { crate::Vector3Zero() }
383    }
384
385    /// Vector with all components set to 1. (raymath `Vector3One`)
386    #[inline]
387    #[must_use]
388    pub fn one() -> Self {
389        // SAFETY: Vector3One is a pure value-out raymath fn; no preconditions.
390        unsafe { crate::Vector3One() }
391    }
392
393    /// Add scalar to all components. (raymath `Vector3AddValue`)
394    #[inline]
395    #[must_use]
396    pub fn add_value(self, add: f32) -> Self {
397        // SAFETY: pure value-in/out, no preconditions.
398        unsafe { crate::Vector3AddValue(self, add) }
399    }
400
401    /// Subtract scalar from all components. (raymath `Vector3SubtractValue`)
402    #[inline]
403    #[must_use]
404    pub fn sub_value(self, sub: f32) -> Self {
405        // SAFETY: pure value-in/out, no preconditions.
406        unsafe { crate::Vector3SubtractValue(self, sub) }
407    }
408
409    /// Scale vector by scalar. (raymath `Vector3Scale`)
410    #[inline]
411    #[must_use]
412    pub fn scale(self, scalar: f32) -> Self {
413        // SAFETY: pure value-in/out, no preconditions.
414        unsafe { crate::Vector3Scale(self, scalar) }
415    }
416
417    /// Component-wise multiply. (raymath `Vector3Multiply`)
418    #[inline]
419    #[must_use]
420    pub fn multiply(self, other: Vector3) -> Self {
421        // SAFETY: pure value-in/out, no preconditions.
422        unsafe { crate::Vector3Multiply(self, other) }
423    }
424
425    /// Cross product. (raymath `Vector3CrossProduct`)
426    #[inline]
427    #[must_use]
428    pub fn cross(self, other: Vector3) -> Self {
429        // SAFETY: pure value-in/out, no preconditions.
430        unsafe { crate::Vector3CrossProduct(self, other) }
431    }
432
433    /// Perpendicular vector. (raymath `Vector3Perpendicular`)
434    #[inline]
435    #[must_use]
436    pub fn perpendicular(self) -> Self {
437        // SAFETY: pure value-in/out, no preconditions.
438        unsafe { crate::Vector3Perpendicular(self) }
439    }
440
441    /// Vector length. (raymath `Vector3Length`)
442    #[inline]
443    #[must_use]
444    pub fn length(self) -> f32 {
445        // SAFETY: pure value-in/out, no preconditions.
446        unsafe { crate::Vector3Length(self) }
447    }
448
449    /// Squared vector length. (raymath `Vector3LengthSqr`)
450    #[inline]
451    #[must_use]
452    pub fn length_sqr(self) -> f32 {
453        // SAFETY: pure value-in/out, no preconditions.
454        unsafe { crate::Vector3LengthSqr(self) }
455    }
456
457    /// Dot product. (raymath `Vector3DotProduct`)
458    #[inline]
459    #[must_use]
460    pub fn dot(self, other: Vector3) -> f32 {
461        // SAFETY: pure value-in/out, no preconditions.
462        unsafe { crate::Vector3DotProduct(self, other) }
463    }
464
465    /// Distance between two vectors. (raymath `Vector3Distance`)
466    #[inline]
467    #[must_use]
468    pub fn distance(self, other: Vector3) -> f32 {
469        // SAFETY: pure value-in/out, no preconditions.
470        unsafe { crate::Vector3Distance(self, other) }
471    }
472
473    /// Squared distance between two vectors. (raymath `Vector3DistanceSqr`)
474    #[inline]
475    #[must_use]
476    pub fn distance_sqr(self, other: Vector3) -> f32 {
477        // SAFETY: pure value-in/out, no preconditions.
478        unsafe { crate::Vector3DistanceSqr(self, other) }
479    }
480
481    /// Angle between two vectors (radians). (raymath `Vector3Angle`)
482    #[inline]
483    #[must_use]
484    pub fn angle(self, other: Vector3) -> f32 {
485        // SAFETY: pure value-in/out, no preconditions.
486        unsafe { crate::Vector3Angle(self, other) }
487    }
488
489    /// Negate vector. (raymath `Vector3Negate`)
490    #[inline]
491    #[must_use]
492    pub fn negate(self) -> Self {
493        // SAFETY: pure value-in/out, no preconditions.
494        unsafe { crate::Vector3Negate(self) }
495    }
496
497    /// Component-wise divide. (raymath `Vector3Divide`)
498    #[inline]
499    #[must_use]
500    pub fn divide(self, other: Vector3) -> Self {
501        // SAFETY: pure value-in/out, no preconditions.
502        unsafe { crate::Vector3Divide(self, other) }
503    }
504
505    /// Normalize vector. (raymath `Vector3Normalize`)
506    #[inline]
507    #[must_use]
508    pub fn normalize(self) -> Self {
509        // SAFETY: pure value-in/out, no preconditions.
510        unsafe { crate::Vector3Normalize(self) }
511    }
512
513    /// Project `self` onto `other`. (raymath `Vector3Project`)
514    #[inline]
515    #[must_use]
516    pub fn project(self, other: Vector3) -> Self {
517        // SAFETY: pure value-in/out, no preconditions.
518        unsafe { crate::Vector3Project(self, other) }
519    }
520
521    /// Reject `self` from `other` (component perpendicular to `other`). (raymath `Vector3Reject`)
522    #[inline]
523    #[must_use]
524    pub fn reject(self, other: Vector3) -> Self {
525        // SAFETY: pure value-in/out, no preconditions.
526        unsafe { crate::Vector3Reject(self, other) }
527    }
528
529    /// Transform by matrix. (raymath `Vector3Transform`)
530    #[inline]
531    #[must_use]
532    pub fn transform(self, mat: Matrix) -> Self {
533        // SAFETY: pure value-in/out, no preconditions.
534        unsafe { crate::Vector3Transform(self, mat) }
535    }
536
537    /// Rotate by quaternion. (raymath `Vector3RotateByQuaternion`)
538    #[inline]
539    #[must_use]
540    pub fn rotate_by_quaternion(self, q: Quaternion) -> Self {
541        // SAFETY: pure value-in/out; Quaternion is #[repr(C)] matching the FFI type.
542        unsafe { crate::Vector3RotateByQuaternion(self, q) }
543    }
544
545    /// Rotate by axis and angle (radians). (raymath `Vector3RotateByAxisAngle`)
546    #[inline]
547    #[must_use]
548    pub fn rotate_by_axis_angle(self, axis: Vector3, angle: f32) -> Self {
549        // SAFETY: pure value-in/out, no preconditions.
550        unsafe { crate::Vector3RotateByAxisAngle(self, axis, angle) }
551    }
552
553    /// Move towards target by at most maxDistance. (raymath `Vector3MoveTowards`)
554    #[inline]
555    #[must_use]
556    pub fn move_towards(self, target: Vector3, max_distance: f32) -> Self {
557        // SAFETY: pure value-in/out, no preconditions.
558        unsafe { crate::Vector3MoveTowards(self, target, max_distance) }
559    }
560
561    /// Linear interpolation. (raymath `Vector3Lerp`)
562    #[inline]
563    #[must_use]
564    pub fn lerp(self, other: Vector3, amount: f32) -> Self {
565        // SAFETY: pure value-in/out, no preconditions.
566        unsafe { crate::Vector3Lerp(self, other, amount) }
567    }
568
569    /// Cubic Hermite interpolation. (raymath `Vector3CubicHermite`)
570    #[inline]
571    #[must_use]
572    pub fn cubic_hermite(
573        self,
574        tangent1: Vector3,
575        v2: Vector3,
576        tangent2: Vector3,
577        amount: f32,
578    ) -> Self {
579        // SAFETY: pure value-in/out, no preconditions.
580        unsafe { crate::Vector3CubicHermite(self, tangent1, v2, tangent2, amount) }
581    }
582
583    /// Reflect vector about normal. (raymath `Vector3Reflect`)
584    #[inline]
585    #[must_use]
586    pub fn reflect(self, normal: Vector3) -> Self {
587        // SAFETY: pure value-in/out, no preconditions.
588        unsafe { crate::Vector3Reflect(self, normal) }
589    }
590
591    /// Component-wise minimum. (raymath `Vector3Min`)
592    #[inline]
593    #[must_use]
594    pub fn min(self, other: Vector3) -> Self {
595        // SAFETY: pure value-in/out, no preconditions.
596        unsafe { crate::Vector3Min(self, other) }
597    }
598
599    /// Component-wise maximum. (raymath `Vector3Max`)
600    #[inline]
601    #[must_use]
602    pub fn max(self, other: Vector3) -> Self {
603        // SAFETY: pure value-in/out, no preconditions.
604        unsafe { crate::Vector3Max(self, other) }
605    }
606
607    /// Barycentric coordinates of point p in triangle (a, b, c). (raymath `Vector3Barycenter`)
608    #[inline]
609    #[must_use]
610    pub fn barycenter(p: Vector3, a: Vector3, b: Vector3, c: Vector3) -> Self {
611        // SAFETY: pure value-in/out, no preconditions.
612        unsafe { crate::Vector3Barycenter(p, a, b, c) }
613    }
614
615    /// Unproject vector from screen space using projection and view matrices. (raymath `Vector3Unproject`)
616    #[inline]
617    #[must_use]
618    pub fn unproject(self, projection: Matrix, view: Matrix) -> Self {
619        // SAFETY: pure value-in/out, no preconditions.
620        unsafe { crate::Vector3Unproject(self, projection, view) }
621    }
622
623    /// Convert to array of floats. (raymath `Vector3ToFloatV`)
624    #[inline]
625    #[must_use]
626    pub fn to_float_array(self) -> crate::float3 {
627        // SAFETY: pure value-in/out, no preconditions.
628        unsafe { crate::Vector3ToFloatV(self) }
629    }
630
631    /// Invert components (1/x, 1/y, 1/z). (raymath `Vector3Invert`)
632    #[inline]
633    #[must_use]
634    pub fn invert(self) -> Self {
635        // SAFETY: pure value-in/out, no preconditions.
636        unsafe { crate::Vector3Invert(self) }
637    }
638
639    /// Clamp components between min and max vectors. (raymath `Vector3Clamp`)
640    #[inline]
641    #[must_use]
642    pub fn clamp(self, min: Vector3, max: Vector3) -> Self {
643        // SAFETY: pure value-in/out, no preconditions.
644        unsafe { crate::Vector3Clamp(self, min, max) }
645    }
646
647    /// Clamp length between min and max scalars. (raymath `Vector3ClampValue`)
648    #[inline]
649    #[must_use]
650    pub fn clamp_value(self, min: f32, max: f32) -> Self {
651        // SAFETY: pure value-in/out, no preconditions.
652        unsafe { crate::Vector3ClampValue(self, min, max) }
653    }
654
655    /// Approximate equality (uses raymath epsilon). (raymath `Vector3Equals`)
656    #[inline]
657    #[must_use]
658    pub fn equals(self, other: Vector3) -> bool {
659        // SAFETY: pure value-in/out, no preconditions.
660        unsafe { crate::Vector3Equals(self, other) != 0 }
661    }
662
663    /// Refract through surface with index ratio r. (raymath `Vector3Refract`)
664    #[inline]
665    #[must_use]
666    pub fn refract(self, n: Vector3, r: f32) -> Self {
667        // SAFETY: pure value-in/out, no preconditions.
668        unsafe { crate::Vector3Refract(self, n, r) }
669    }
670}
671
672/// Orthonormalize two vectors in-place. (raymath `Vector3OrthoNormalize`)
673pub fn vector3_ortho_normalize(v1: &mut Vector3, v2: &mut Vector3) {
674    // SAFETY: Vector3OrthoNormalize writes through the given pointers, which are valid,
675    // non-null, properly aligned &mut references. v1 and v2 are distinct borrows so
676    // there is no aliasing.
677    unsafe { crate::Vector3OrthoNormalize(v1 as *mut _, v2 as *mut _) }
678}
679
680impl From<(f32, f32, f32)> for Vector3 {
681    /// Construct from an `(x, y, z)` tuple.
682    #[inline]
683    fn from((x, y, z): (f32, f32, f32)) -> Self {
684        Vector3 { x, y, z }
685    }
686}
687impl From<[f32; 3]> for Vector3 {
688    /// Construct from an `[x, y, z]` array.
689    #[inline]
690    fn from([x, y, z]: [f32; 3]) -> Self {
691        Vector3 { x, y, z }
692    }
693}
694
695impl core::ops::Add for Vector3 {
696    type Output = Vector3;
697    #[inline]
698    fn add(self, rhs: Vector3) -> Vector3 {
699        // SAFETY: Vector3Add is a pure value-in/out raymath fn; no preconditions.
700        unsafe { crate::Vector3Add(self, rhs) }
701    }
702}
703impl core::ops::AddAssign for Vector3 {
704    #[inline]
705    fn add_assign(&mut self, rhs: Vector3) {
706        *self = *self + rhs;
707    }
708}
709
710impl core::ops::Sub for Vector3 {
711    type Output = Vector3;
712    #[inline]
713    fn sub(self, rhs: Vector3) -> Vector3 {
714        // SAFETY: Vector3Subtract is a pure value-in/out raymath fn; no preconditions.
715        unsafe { crate::Vector3Subtract(self, rhs) }
716    }
717}
718impl core::ops::SubAssign for Vector3 {
719    #[inline]
720    fn sub_assign(&mut self, rhs: Vector3) {
721        *self = *self - rhs;
722    }
723}
724
725impl core::ops::Mul<f32> for Vector3 {
726    type Output = Vector3;
727    #[inline]
728    fn mul(self, rhs: f32) -> Vector3 {
729        // SAFETY: Vector3Scale is a pure value-in/out raymath fn; no preconditions.
730        unsafe { crate::Vector3Scale(self, rhs) }
731    }
732}
733impl core::ops::MulAssign<f32> for Vector3 {
734    #[inline]
735    fn mul_assign(&mut self, rhs: f32) {
736        *self = *self * rhs;
737    }
738}
739
740impl core::ops::Mul<Vector3> for Vector3 {
741    type Output = Vector3;
742    #[inline]
743    fn mul(self, rhs: Vector3) -> Vector3 {
744        // SAFETY: Vector3Multiply is a pure value-in/out raymath fn (component-wise); no preconditions.
745        unsafe { crate::Vector3Multiply(self, rhs) }
746    }
747}
748
749impl core::ops::Div<Vector3> for Vector3 {
750    type Output = Vector3;
751    #[inline]
752    fn div(self, rhs: Vector3) -> Vector3 {
753        // SAFETY: Vector3Divide is a pure value-in/out raymath fn (component-wise); no preconditions.
754        unsafe { crate::Vector3Divide(self, rhs) }
755    }
756}
757
758impl core::ops::Neg for Vector3 {
759    type Output = Vector3;
760    #[inline]
761    fn neg(self) -> Vector3 {
762        // SAFETY: Vector3Negate is a pure value-in/out raymath fn; no preconditions.
763        unsafe { crate::Vector3Negate(self) }
764    }
765}
766
767// ─── Vector4 ─────────────────────────────────────────────────────────────────
768
769impl Vector4 {
770    /// Construct a new Vector4.
771    #[inline]
772    pub fn new(x: f32, y: f32, z: f32, w: f32) -> Self {
773        Self { x, y, z, w }
774    }
775
776    /// Zero vector. (raymath `Vector4Zero`)
777    #[inline]
778    #[must_use]
779    pub fn zero() -> Self {
780        // SAFETY: Vector4Zero is a pure value-out raymath fn; no preconditions.
781        unsafe { crate::Vector4Zero() }
782    }
783
784    /// Vector with all components set to 1. (raymath `Vector4One`)
785    #[inline]
786    #[must_use]
787    pub fn one() -> Self {
788        // SAFETY: Vector4One is a pure value-out raymath fn; no preconditions.
789        unsafe { crate::Vector4One() }
790    }
791
792    /// Add scalar to all components. (raymath `Vector4AddValue`)
793    #[inline]
794    #[must_use]
795    pub fn add_value(self, add: f32) -> Self {
796        // SAFETY: pure value-in/out, no preconditions.
797        unsafe { crate::Vector4AddValue(self, add) }
798    }
799
800    /// Subtract scalar from all components. (raymath `Vector4SubtractValue`)
801    #[inline]
802    #[must_use]
803    pub fn sub_value(self, sub: f32) -> Self {
804        // SAFETY: pure value-in/out, no preconditions.
805        unsafe { crate::Vector4SubtractValue(self, sub) }
806    }
807
808    /// Vector length. (raymath `Vector4Length`)
809    #[inline]
810    #[must_use]
811    pub fn length(self) -> f32 {
812        // SAFETY: pure value-in/out, no preconditions.
813        unsafe { crate::Vector4Length(self) }
814    }
815
816    /// Squared vector length. (raymath `Vector4LengthSqr`)
817    #[inline]
818    #[must_use]
819    pub fn length_sqr(self) -> f32 {
820        // SAFETY: pure value-in/out, no preconditions.
821        unsafe { crate::Vector4LengthSqr(self) }
822    }
823
824    /// Dot product. (raymath `Vector4DotProduct`)
825    #[inline]
826    #[must_use]
827    pub fn dot(self, other: Vector4) -> f32 {
828        // SAFETY: pure value-in/out, no preconditions.
829        unsafe { crate::Vector4DotProduct(self, other) }
830    }
831
832    /// Distance between two vectors. (raymath `Vector4Distance`)
833    #[inline]
834    #[must_use]
835    pub fn distance(self, other: Vector4) -> f32 {
836        // SAFETY: pure value-in/out, no preconditions.
837        unsafe { crate::Vector4Distance(self, other) }
838    }
839
840    /// Squared distance between two vectors. (raymath `Vector4DistanceSqr`)
841    #[inline]
842    #[must_use]
843    pub fn distance_sqr(self, other: Vector4) -> f32 {
844        // SAFETY: pure value-in/out, no preconditions.
845        unsafe { crate::Vector4DistanceSqr(self, other) }
846    }
847
848    /// Scale vector by scalar. (raymath `Vector4Scale`)
849    #[inline]
850    #[must_use]
851    pub fn scale(self, scale: f32) -> Self {
852        // SAFETY: pure value-in/out, no preconditions.
853        unsafe { crate::Vector4Scale(self, scale) }
854    }
855
856    /// Component-wise multiply. (raymath `Vector4Multiply`)
857    #[inline]
858    #[must_use]
859    pub fn multiply(self, other: Vector4) -> Self {
860        // SAFETY: pure value-in/out, no preconditions.
861        unsafe { crate::Vector4Multiply(self, other) }
862    }
863
864    /// Negate vector. (raymath `Vector4Negate`)
865    #[inline]
866    #[must_use]
867    pub fn negate(self) -> Self {
868        // SAFETY: pure value-in/out, no preconditions.
869        unsafe { crate::Vector4Negate(self) }
870    }
871
872    /// Component-wise divide. (raymath `Vector4Divide`)
873    #[inline]
874    #[must_use]
875    pub fn divide(self, other: Vector4) -> Self {
876        // SAFETY: pure value-in/out, no preconditions.
877        unsafe { crate::Vector4Divide(self, other) }
878    }
879
880    /// Normalize vector. (raymath `Vector4Normalize`)
881    #[inline]
882    #[must_use]
883    pub fn normalize(self) -> Self {
884        // SAFETY: pure value-in/out, no preconditions.
885        unsafe { crate::Vector4Normalize(self) }
886    }
887
888    /// Component-wise minimum. (raymath `Vector4Min`)
889    #[inline]
890    #[must_use]
891    pub fn min(self, other: Vector4) -> Self {
892        // SAFETY: pure value-in/out, no preconditions.
893        unsafe { crate::Vector4Min(self, other) }
894    }
895
896    /// Component-wise maximum. (raymath `Vector4Max`)
897    #[inline]
898    #[must_use]
899    pub fn max(self, other: Vector4) -> Self {
900        // SAFETY: pure value-in/out, no preconditions.
901        unsafe { crate::Vector4Max(self, other) }
902    }
903
904    /// Linear interpolation. (raymath `Vector4Lerp`)
905    #[inline]
906    #[must_use]
907    pub fn lerp(self, other: Vector4, amount: f32) -> Self {
908        // SAFETY: pure value-in/out, no preconditions.
909        unsafe { crate::Vector4Lerp(self, other, amount) }
910    }
911
912    /// Move towards target by at most maxDistance. (raymath `Vector4MoveTowards`)
913    #[inline]
914    #[must_use]
915    pub fn move_towards(self, target: Vector4, max_distance: f32) -> Self {
916        // SAFETY: pure value-in/out, no preconditions.
917        unsafe { crate::Vector4MoveTowards(self, target, max_distance) }
918    }
919
920    /// Invert components (1/x, 1/y, 1/z, 1/w). (raymath `Vector4Invert`)
921    #[inline]
922    #[must_use]
923    pub fn invert(self) -> Self {
924        // SAFETY: pure value-in/out, no preconditions.
925        unsafe { crate::Vector4Invert(self) }
926    }
927
928    /// Approximate equality (uses raymath epsilon). (raymath `Vector4Equals`)
929    #[inline]
930    #[must_use]
931    pub fn equals(self, other: Vector4) -> bool {
932        // SAFETY: pure value-in/out, no preconditions.
933        unsafe { crate::Vector4Equals(self, other) != 0 }
934    }
935}
936
937impl From<(f32, f32, f32, f32)> for Vector4 {
938    /// Construct from an `(x, y, z, w)` tuple.
939    #[inline]
940    fn from((x, y, z, w): (f32, f32, f32, f32)) -> Self {
941        Vector4 { x, y, z, w }
942    }
943}
944impl From<[f32; 4]> for Vector4 {
945    /// Construct from an `[x, y, z, w]` array.
946    #[inline]
947    fn from([x, y, z, w]: [f32; 4]) -> Self {
948        Vector4 { x, y, z, w }
949    }
950}
951
952impl core::ops::Add for Vector4 {
953    type Output = Vector4;
954    #[inline]
955    fn add(self, rhs: Vector4) -> Vector4 {
956        // SAFETY: Vector4Add is a pure value-in/out raymath fn; no preconditions.
957        unsafe { crate::Vector4Add(self, rhs) }
958    }
959}
960impl core::ops::AddAssign for Vector4 {
961    #[inline]
962    fn add_assign(&mut self, rhs: Vector4) {
963        *self = *self + rhs;
964    }
965}
966
967impl core::ops::Sub for Vector4 {
968    type Output = Vector4;
969    #[inline]
970    fn sub(self, rhs: Vector4) -> Vector4 {
971        // SAFETY: Vector4Subtract is a pure value-in/out raymath fn; no preconditions.
972        unsafe { crate::Vector4Subtract(self, rhs) }
973    }
974}
975impl core::ops::SubAssign for Vector4 {
976    #[inline]
977    fn sub_assign(&mut self, rhs: Vector4) {
978        *self = *self - rhs;
979    }
980}
981
982impl core::ops::Mul<f32> for Vector4 {
983    type Output = Vector4;
984    #[inline]
985    fn mul(self, rhs: f32) -> Vector4 {
986        // SAFETY: Vector4Scale is a pure value-in/out raymath fn; no preconditions.
987        unsafe { crate::Vector4Scale(self, rhs) }
988    }
989}
990impl core::ops::MulAssign<f32> for Vector4 {
991    #[inline]
992    fn mul_assign(&mut self, rhs: f32) {
993        *self = *self * rhs;
994    }
995}
996
997impl core::ops::Mul<Vector4> for Vector4 {
998    type Output = Vector4;
999    #[inline]
1000    fn mul(self, rhs: Vector4) -> Vector4 {
1001        // SAFETY: Vector4Multiply is a pure value-in/out raymath fn (component-wise); no preconditions.
1002        unsafe { crate::Vector4Multiply(self, rhs) }
1003    }
1004}
1005
1006impl core::ops::Div<Vector4> for Vector4 {
1007    type Output = Vector4;
1008    #[inline]
1009    fn div(self, rhs: Vector4) -> Vector4 {
1010        // SAFETY: Vector4Divide is a pure value-in/out raymath fn (component-wise); no preconditions.
1011        unsafe { crate::Vector4Divide(self, rhs) }
1012    }
1013}
1014
1015impl core::ops::Neg for Vector4 {
1016    type Output = Vector4;
1017    #[inline]
1018    fn neg(self) -> Vector4 {
1019        // SAFETY: Vector4Negate is a pure value-in/out raymath fn; no preconditions.
1020        unsafe { crate::Vector4Negate(self) }
1021    }
1022}