Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 39 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 13 additions & 1 deletion src/generic_vector.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -620,15 +626,17 @@ test "zalgebra.Vectors.length" {
}
}

test "zalgebra.Vectors.distance" {
test "zalgebra.Vectors.distance/Sq" {
// Vec2
{
const a = Vec2.zero();
const b = Vec2.left();
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
Expand All @@ -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
Expand All @@ -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);
}
}

Expand Down
Loading