From da4af836749cd592864b2f5dcbd02a36827b088f Mon Sep 17 00:00:00 2001 From: Simon Galli Date: Tue, 2 Sep 2025 11:28:42 +0200 Subject: [PATCH] Add distance squared function to vectors --- README.md | 77 +++++++++++++++++++++--------------------- src/generic_vector.zig | 14 +++++++- 2 files changed, 52 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index 1843a34..1c7cf0e 100644 --- a/README.md +++ b/README.md @@ -60,44 +60,45 @@ pub fn main () void { ### Vectors -| Methods | Description | -| --------- | -------------------------------------------------------------------------- | -| new | Construct a vector from 2 to 4 components | -| x | Return first component | -| y | Return second component | -| z | Return third component (only for vec3, vec4) | -| w | Return fourth component (only for vec4) | -| at | Return component from given index | -| set | Set all components to the same given value | -| negate | Scale all components by -1 | -| cast | Cast a type to another type | -| fromSlice | Construct new vectors from slice | -| zero | Shorthand for `(0, 0, 0)` | -| one | Shorthand for `(1, 1, 1)` | -| up | Shorthand for `(0, 1, 0)` | -| down | Shorthand for `(0, -1, 0)` | -| right | Shorthand for `(1, 0, 0)` | -| left | Shorthand for `(-1, 0, 0)` | -| forward | Shorthand for `(0, 0, 1)` (only for vec3 and vec4) | -| back | Shorthand for `(0, 0, -1)` (only for vec3 and vec4) | -| toArray | Return an array of same size. | -| getAngle | Return angle in degrees between two vectors (only for vec2 and vec3) | -| rotate | Rotate vector by angle (in degrees) | -| length | Return the magnitude of the current vector | -| lengthSq | Return the magnitude squared of the current vector | -| distance | Return the distance between two points | -| norm | Construct a new normalized vector based on the given one | -| eql | Return `true` if two vectors are equals | -| sub | Construct new vector resulting from the substraction between two vectors | -| add | Construct new vector resulting from the addition between two vectors | -| div | Construct new vector resulting from the division between two vectors | -| mul | Construct new vector resulting from the multiplication between two vectors | -| scale | Construct new vector after multiplying each components by a given scalar | -| cross | Construct the cross product (as vector) from two vectors (only for vec3) | -| dot | Return the dot product between two vectors | -| lerp | Linear interpolation between two vectors | -| min | Construct vector from the min components between two vectors | -| max | Construct vector from the max components between two vectors | +| Methods | Description | +| ---------- | -------------------------------------------------------------------------- | +| new | Construct a vector from 2 to 4 components | +| x | Return first component | +| y | Return second component | +| z | Return third component (only for vec3, vec4) | +| w | Return fourth component (only for vec4) | +| at | Return component from given index | +| set | Set all components to the same given value | +| negate | Scale all components by -1 | +| cast | Cast a type to another type | +| fromSlice | Construct new vectors from slice | +| zero | Shorthand for `(0, 0, 0)` | +| one | Shorthand for `(1, 1, 1 ) | +| up | Shorthand for `(0, 1, 0)` | +| down | Shorthand for `(0, -1, 0)` | +| right | Shorthand for `(1, 0, 0)` | +| left | Shorthand for `(-1, 0, 0)` | +| forward | Shorthand for `(0, 0, 1)` (only for vec3 and vec4) | +| back | Shorthand for `(0, 0, -1)` (only for vec3 and vec4) | +| toArray | Return an array of same size. | +| getAngle | Return angle in degrees between two vectors (only for vec2 and vec3) | +| rotate | Rotate vector by angle (in degrees) | +| length | Return the magnitude of the current vector | +| lengthSq | Return the magnitude squared of the current vector | +| distance | Return the distance between two points | +| distanceSq | Return the squared distance between two points | +| norm | Construct a new normalized vector based on the given one | +| eql | Return `true` if two vectors are equals | +| sub | Construct new vector resulting from the substraction between two vectors | +| add | Construct new vector resulting from the addition between two vectors | +| div | Construct new vector resulting from the division between two vectors | +| mul | Construct new vector resulting from the multiplication between two vectors | +| scale | Construct new vector after multiplying each components by a given scalar | +| cross | Construct the cross product (as vector) from two vectors (only for vec3) | +| dot | Return the dot product between two vectors | +| lerp | Linear interpolation between two vectors | +| min | Construct vector from the min components between two vectors | +| max | Construct vector from the max components between two vectors | ### Matrices diff --git a/src/generic_vector.zig b/src/generic_vector.zig index 1a41812..baabc70 100644 --- a/src/generic_vector.zig +++ b/src/generic_vector.zig @@ -321,6 +321,12 @@ pub fn GenericVector(comptime dimensions: comptime_int, comptime T: type) type { return length(first_vector.sub(second_vector)); } + /// Return the distance squared between two points. + /// (x1 - x2)^2 + (y1 - y2)^2 + (z1 - z2)^2 ... + pub fn distanceSq(first_vector: Self, second_vector: Self) T { + return lengthSq(first_vector.sub(second_vector)); + } + /// Construct new normalized vector from a given one. pub fn norm(self: Self) Self { const l = self.length(); @@ -620,7 +626,7 @@ test "zalgebra.Vectors.length" { } } -test "zalgebra.Vectors.distance" { +test "zalgebra.Vectors.distance/Sq" { // Vec2 { const a = Vec2.zero(); @@ -628,7 +634,9 @@ test "zalgebra.Vectors.distance" { const c = Vec2.new(0, 5); try expectEqual(a.distance(b), 1); + try expectEqual(a.distanceSq(b), 1); try expectEqual(a.distance(c), 5); + try expectEqual(a.distanceSq(c), 25); } // Vec3 @@ -638,7 +646,9 @@ test "zalgebra.Vectors.distance" { const c = Vec3.new(0, 5, 0); try expectEqual(a.distance(b), 1); + try expectEqual(a.distanceSq(b), 1); try expectEqual(a.distance(c), 5); + try expectEqual(a.distanceSq(c), 25); } // Vec4 @@ -648,7 +658,9 @@ test "zalgebra.Vectors.distance" { const c = Vec4.new(0, 5, 0, 0); try expectEqual(a.distance(b), 1); + try expectEqual(a.distanceSq(b), 1); try expectEqual(a.distance(c), 5); + try expectEqual(a.distanceSq(c), 25); } }