vector: perpendicular

This commit is contained in:
BlackMATov
2021-01-26 23:23:39 +07:00
parent b5bf46606d
commit 5651208914
2 changed files with 16 additions and 4 deletions

View File

@@ -698,8 +698,15 @@ namespace vmath_hpp
// project // project
template < typename T, std::size_t Size > template < typename T, std::size_t Size >
[[nodiscard]] vec<T, Size> project(const vec<T, Size>& v, const vec<T, Size>& normal) { [[nodiscard]] constexpr vec<T, Size> project(const vec<T, Size>& v, const vec<T, Size>& normal) {
return dot(v, normal) / length2(normal) * normal; return dot(v, normal) / dot(normal, normal) * normal;
}
// perpendicular
template < typename T, std::size_t Size >
[[nodiscard]] constexpr vec<T, Size> perpendicular(const vec<T, Size>& v, const vec<T, Size>& normal) {
return v - project(v, normal);
} }
} }

View File

@@ -301,8 +301,13 @@ TEST_CASE("vmath/ext") {
} }
SECTION("vector project") { SECTION("vector project") {
REQUIRE(project(float2(2.f, 2.f), float2(0.f, 1.f)) == uapprox2(0.f, 2.f)); STATIC_REQUIRE(project(float2(2.f, 2.f), float2(0.f, 1.f)) == uapprox2(0.f, 2.f));
REQUIRE(project(float3(2.f, 2.f, 2.f), float3(0.f, 0.f, 1.f)) == uapprox3(0.f, 0.f, 2.f)); STATIC_REQUIRE(project(float3(2.f, 2.f, 2.f), float3(0.f, 0.f, 1.f)) == uapprox3(0.f, 0.f, 2.f));
}
SECTION("vector perpendicular") {
STATIC_REQUIRE(perpendicular(float2(2.f, 2.f), float2(0.f, 1.f)) == uapprox2(2.f, 0.f));
STATIC_REQUIRE(perpendicular(float3(2.f, 2.f, 2.f), float3(0.f, 0.f, 1.f)) == uapprox3(2.f, 2.f, 0.f));
} }
SECTION("quaternion qrotate") { SECTION("quaternion qrotate") {