mach: math lib vector dot product
This commit is contained in:
parent
9ec766c165
commit
f9e727fa2d
1 changed files with 36 additions and 0 deletions
36
src/math.zig
36
src/math.zig
|
|
@ -150,6 +150,14 @@ pub const vec = struct {
|
|||
|
||||
return sub1 - sub2;
|
||||
}
|
||||
|
||||
/// Calculates the dot product between vector a and b and returns scalar.
|
||||
pub inline fn dot(a: anytype, b: @TypeOf(a)) f32 {
|
||||
switch (@TypeOf(a)) {
|
||||
Vec2, Vec3, Vec4 => return @reduce(.Add, a * b),
|
||||
else => @compileError("Expected vector, found '" ++ @typeName(@TypeOf(a)) ++ "'"),
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
test "vec.size" {
|
||||
|
|
@ -439,6 +447,34 @@ test "vec.cross" {
|
|||
}
|
||||
}
|
||||
|
||||
test "vec.dot" {
|
||||
{
|
||||
const a = Vec2{ -1, 2 };
|
||||
const b = Vec2{ 4, 5 };
|
||||
const dot = vec.dot(a, b);
|
||||
try expectEqual(dot, 6);
|
||||
}
|
||||
{
|
||||
const a = Vec3{ -1.0, 2.0, 3.0 };
|
||||
const b = Vec3{ 4.0, 5.0, 6.0 };
|
||||
const dot = vec.dot(a, b);
|
||||
try expectEqual(dot, 24.0);
|
||||
}
|
||||
{
|
||||
const a = Vec4{ -1.0, 2.0, 3.0, -2.0 };
|
||||
const b = Vec4{ 4.0, 5.0, 6.0, 2.0 };
|
||||
const dot = vec.dot(a, b);
|
||||
try expectEqual(dot, 20.0);
|
||||
}
|
||||
|
||||
{
|
||||
const a = Vec4{ 0, 0, 0, 0 };
|
||||
const b = Vec4{ 0, 0, 0, 0 };
|
||||
const dot = vec.dot(a, b);
|
||||
try expectEqual(dot, 0.0);
|
||||
}
|
||||
}
|
||||
|
||||
/// Matrix operations
|
||||
pub const mat = struct {
|
||||
/// Constructs an identity matrix of type T.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue