diff --git a/lib/std/math/math_vector.c3 b/lib/std/math/math_vector.c3 index 86d9fd208..3e96be962 100644 --- a/lib/std/math/math_vector.c3 +++ b/lib/std/math/math_vector.c3 @@ -1,11 +1,11 @@ module std::math::vector; import std::math; -macro double[<*>].length_sq(self) => self.dot(self); -macro float[<*>].length_sq(self) => self.dot(self); +macro double[<*>].sq_magnitude(self) => self.dot(self); +macro float[<*>].sq_magnitude(self) => self.dot(self); -macro double[<*>].distance_sq(self, double[<*>] v2) => (self - v2).length_sq(); -macro float[<*>].distance_sq(self, float[<*>] v2) => (self - v2).length_sq(); +macro double[<*>].distance_sq(self, double[<*>] v2) => (self - v2).sq_magnitude(); +macro float[<*>].distance_sq(self, float[<*>] v2) => (self - v2).sq_magnitude(); macro float[<2>].transform(self, Matrix4f mat) => transform2(self, mat); macro float[<2>].rotate(self, float angle) => rotate(self, angle); @@ -54,7 +54,7 @@ fn void ortho_normalized(double[<3>]* v1, double[<3>]* v2) => ortho_normalize3(v macro towards(v, target, max_distance) @private { var delta = target - v; - var square = delta.length_sq(); + var square = delta.sq_magnitude(); if (square == 0 || (max_distance >= 0 && (square <= max_distance * max_distance))) return target; @@ -65,7 +65,7 @@ macro towards(v, target, max_distance) @private macro clamp_magnitude(v, min, max) @private { - var length = v.dot(v); + var length = v.sq_magnitude(); if (length > 0) { length = math::sqrt(length); diff --git a/releasenotes.md b/releasenotes.md index da16e5bef..35c53c523 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -31,7 +31,7 @@ - `mem::temp_alloc` and related changed to `mem::talloc`. - `mem::temp_new_array` changed to `mem::temp_array`. - Add `ONHEAP` variants for List/HashMap for initializing global maps on the heap. -- Remove Vec2 and other aliases from std::math. +- Remove Vec2 and other aliases from std::math. Replace `.length_sq()` with `sq_magnitude()` ## 0.6.8 Change list diff --git a/test/unit/stdlib/math/math_vector.c3 b/test/unit/stdlib/math/math_vector.c3 index 61ab8b469..cd4fe8af9 100644 --- a/test/unit/stdlib/math/math_vector.c3 +++ b/test/unit/stdlib/math/math_vector.c3 @@ -32,7 +32,7 @@ fn void test_vec2_arithmetic() @test fn void test_vec2_length() @test { float[<2>] v = { 3.0f, 4.0f }; - assert(math::is_approx_rel(v.length_sq(), 25.0f, EPSILON_F)); + assert(math::is_approx_rel(v.sq_magnitude(), 25.0f, EPSILON_F)); float[<2>] v2 = { 0.0f, 0.0f }; assert(math::is_approx_rel(v.distance_sq(v2), 25.0f, EPSILON_F)); } @@ -58,7 +58,7 @@ fn void test_vec3_transform() @test assert(math::is_approx_rel(rotated[2], 0.0f, EPSILON_F)); float[<3>] perp = v.perpendicular(); assert(math::is_approx_rel(v.dot(perp), 0.0f, EPSILON_F)); - assert(math::is_approx_rel(perp.length_sq(), v.length_sq(), EPSILON_F)); + assert(math::is_approx_rel(perp.sq_magnitude(), v.sq_magnitude(), EPSILON_F)); } fn void test_vec_magnitude() @test @@ -91,7 +91,7 @@ fn void test_vec3_advanced() @test float[<3>] n = { 0.0f, 1.0f, 0.0f }; float r = 1.5f; float[<3>] refracted = v.refract(n, r); - assert(refracted.length_sq() > 0.0f); + assert(refracted.sq_magnitude() > 0.0f); float[<3>] a = { 0.0f, 0.0f, 0.0f }; float[<3>] b = { 1.0f, 0.0f, 0.0f }; float[<3>] c = { 0.0f, 1.0f, 0.0f }; @@ -104,20 +104,20 @@ fn void test_edge_cases() @test { float[<2>] zero2 = { 0.0f, 0.0f }; float[<3>] zero3 = { 0.0f, 0.0f, 0.0f }; - assert(zero2.length_sq() == 0.0f); - assert(zero3.length_sq() == 0.0f); + assert(zero2.sq_magnitude() == 0.0f); + assert(zero3.sq_magnitude() == 0.0f); float[<3>] perp = zero3.perpendicular(); - assert(perp.length_sq() <= 1.0f + EPSILON_F); + assert(perp.sq_magnitude() <= 1.0f + EPSILON_F); float[<2>] clamped = zero2.clamp_mag(1.0f, 2.0f); - assert(clamped.length_sq() == 0.0f); + assert(clamped.sq_magnitude() == 0.0f); } fn void test_type_consistency() @test { float[<2>] vf = { 1.0f, 2.0f }; double[<2>] vd = { 1.0, 2.0 }; - $assert @typeis(vf.length_sq(), float); - $assert @typeis(vd.length_sq(), double); + $assert @typeis(vf.sq_magnitude(), float); + $assert @typeis(vd.sq_magnitude(), double); float[<3>] v3f = { 1.0f, 2.0f, 3.0f }; double[<3>] v3d = { 1.0, 2.0, 3.0 }; $assert @typeis(v3f.cross(v3f)[0], float);