51 lines
1.2 KiB
Zig
51 lines
1.2 KiB
Zig
|
const std = @import("std");
|
||
|
|
||
|
pub fn cross(v1: anytype, v2: anytype) @typeInfo(@TypeOf(v1, v2)).Vector.child {
|
||
|
const multipled = v1 * v2;
|
||
|
const vector_info = @typeInfo(@TypeOf(v1)).Vector;
|
||
|
var result = multipled[0];
|
||
|
comptime var index = @as(usize, 1);
|
||
|
|
||
|
inline while (index < vector_info.len) : (index += 1) {
|
||
|
result -= multipled[index];
|
||
|
}
|
||
|
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
pub fn distance(v1: anytype, v2: anytype) @typeInfo(@TypeOf(v1, v2)).Vector.child {
|
||
|
return length(v1 - v2);
|
||
|
}
|
||
|
|
||
|
pub fn dot(v1: anytype, v2: anytype) @typeInfo(@TypeOf(v1, v2)).Vector.child {
|
||
|
const multipled = v1 * v2;
|
||
|
const vector_info = @typeInfo(@TypeOf(v1)).Vector;
|
||
|
var result = multipled[0];
|
||
|
comptime var index = @as(usize, 1);
|
||
|
|
||
|
inline while (index < vector_info.len) : (index += 1) {
|
||
|
result += multipled[index];
|
||
|
}
|
||
|
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
pub fn length(v: anytype) @typeInfo(@TypeOf(v)).Vector.child {
|
||
|
return @sqrt(length_squared(v));
|
||
|
}
|
||
|
|
||
|
pub fn length_squared(v: anytype) @typeInfo(@TypeOf(v)).Vector.child {
|
||
|
return dot(v, v);
|
||
|
}
|
||
|
|
||
|
pub fn normal(v: anytype) @TypeOf(v) {
|
||
|
const ls = length_squared(v);
|
||
|
const Vector = @TypeOf(v);
|
||
|
|
||
|
if (ls > std.math.floatEps(@typeInfo(Vector).Vector.child)) {
|
||
|
return v / @as(Vector, @splat(@sqrt(ls)));
|
||
|
}
|
||
|
|
||
|
return v;
|
||
|
}
|