From 31c92cd94c2555207f25e400b15dc7032e8341ea Mon Sep 17 00:00:00 2001 From: intolerantape Date: Thu, 30 Sep 2021 12:22:12 -0700 Subject: [PATCH] Added Double2::Angle() --- Source/Engine/Core/Math/Double2.h | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/Source/Engine/Core/Math/Double2.h b/Source/Engine/Core/Math/Double2.h index e1ee3ef46..905a87a59 100644 --- a/Source/Engine/Core/Math/Double2.h +++ b/Source/Engine/Core/Math/Double2.h @@ -1,4 +1,4 @@ -// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved. +// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved. #pragma once @@ -625,6 +625,20 @@ public: /// The third triangle vertex. /// The triangle area. static double TriangleArea(const Double2& v0, const Double2& v1, const Double2& v2); + + /// + /// Calculates the angle (in radians) between from and to. This is always the smallest value. + /// + /// The first vector. + /// The second vector. + /// The angle (in radians). + static double Angle(const Double2& from, const Double2& to) + { + const double dot = Math::Clamp(Dot(Normalize(from), Normalize(to)), -1.0, 1.0); + if (Math::Abs(dot) > (1.0 - ZeroTolerance)) + return dot > 0.0 ? 0.0 : PI; + return Math::Acos(dot); + } }; inline Double2 operator+(double a, const Double2& b)