// This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0.If a copy of the MPL was not distributed with this // file, You can obtain one at https ://mozilla.org/MPL/2.0/. #include "Vector2.h" #include "Angle.h" #include "FloatSingle.h" #include "Vector3.h" #include #pragma region Vector2Of template Vector2Of::Vector2Of() {} template Vector2Of::Vector2Of(T horizontal, T vertical) : horizontal(horizontal), vertical(vertical) {} template Vector2Of Vector2Of::FromPolar(PolarOf p) { float cosHorizontal = AngleOf::Cos(p.angle); float sinHorizontal = AngleOf::Sin(p.angle); Vector2Of v; v.horizontal = p.distance * sinHorizontal; v.vertical = p.distance * cosHorizontal; return v; } template const Vector2Of Vector2Of::zero = Vector2Of(T{}, T{}); template const Vector2Of Vector2Of::forward = Vector2Of(T{}, 1); template bool LinearAlgebra::Vector2Of::operator==(const Vector2Of& v) { return (this->horizontal == v.horizontal && this->vertical == v.vertical); } template float Vector2Of::MagnitudeOf(const Vector2Of& v) { T sqr = v.horizontal * v.horizontal + v.vertical * v.vertical; float sqrFloat = static_cast(sqr); return sqrtf(sqrFloat); } template float Vector2Of::Magnitude() const { T sqr = this->horizontal * this->horizontal + this->vertical * this->vertical; float sqrFloat = static_cast(sqr); float r = sqrtf(sqrFloat); return r; } template float Vector2Of::SqrMagnitudeOf(const Vector2Of& v) { T sqr = v.horizontal * v.horizontal + v.vertical * v.vertical; return static_cast(sqr); } template float Vector2Of::SqrMagnitude() const { T sqr = this->horizontal * this->horizontal + this->vertical * this->vertical; return static_cast(sqr); } template float Vector2Of::Distance(const Vector2Of& v1, const Vector2Of& v2) { Vector2Of delta = v1 - v2; float r = MagnitudeOf(delta); return r; } template Vector2Of Vector2Of::operator-() { return Vector2Of(-this->horizontal, -this->vertical); } template Vector2Of Vector2Of::operator-(const Vector2Of& v) const { return Vector2Of(this->horizontal - v.horizontal, this->vertical - v.vertical); } template Vector2Of Vector2Of::operator-=(const Vector2Of& v) { this->horizontal -= v.horizontal; this->vertical -= v.vertical; return *this; } template Vector2Of Vector2Of::operator+(const Vector2Of& v) const { return Vector2Of(this->horizontal + v.horizontal, this->vertical + v.vertical); } template Vector2Of Vector2Of::operator+=(const Vector2Of& v) { this->horizontal += v.horizontal; this->vertical += v.vertical; return *this; } template Vector2Of Vector2Of::Scale(const Vector2Of& v1, const Vector2Of& v2) { return Vector2Of(v1.horizontal * v2.horizontal, v1.vertical * v2.vertical); } // template // Vector2Of Vector2Of::operator/=(float f) { // this->x /= f; // this->y /= f; // return *this; // } template T Vector2Of::Dot(const Vector2Of& v1, const Vector2Of& v2) { return v1.horizontal * v2.horizontal + v1.vertical * v2.vertical; } template Vector2Of Vector2Of::Normalize(const Vector2Of& v) { float num = Vector2Of::MagnitudeOf(v); Vector2Of result = Vector2Of::zero; if (num > Float::epsilon) { result = static_cast>(v) / num; } return result; } template AngleOf Vector2Of::UnsignedAngle(const Vector2Of& v1, const Vector2Of& v2) { return AngleOf::Abs(SignedAngle(v1, v2)); } template AngleOf Vector2Of::SignedAngle(const Vector2Of& v1, const Vector2Of& v2) { float sqrMagFrom = v1.SqrMagnitude(); float sqrMagTo = v2.SqrMagnitude(); if (sqrMagFrom == 0 || sqrMagTo == 0) return AngleOf::zero; if (!isfinite(sqrMagFrom) || !isfinite(sqrMagTo)) return AngleOf::zero; // Angle does not support NaN... AngleOf angleFrom = AngleOf::Atan2( static_cast(v1.vertical), static_cast(v1.horizontal)); AngleOf angleTo = AngleOf::Atan2( static_cast(v2.vertical), static_cast(v2.horizontal)); return -(angleTo - angleFrom); } template Vector2Of Vector2Of::Rotate(const Vector2Of& v, AngleOf a) { float sinValue = AngleOf::Sin(a); float cosValue = AngleOf::Cos(a); float tx = static_cast(v.horizontal); float ty = static_cast(v.vertical); Vector2Of r = Vector2Of((cosValue * tx) - (sinValue * ty), (sinValue * tx) + (cosValue * ty)); return r; } template Vector2Of Vector2Of::Lerp(const Vector2Of& v1, const Vector2Of& v2, float f) { Vector2Of v1f = (Vector2Of)v1; Vector2Of delta = v2 - v1; Vector2Of r = v1f + delta * f; return r; } template Vector2Of Vector2Of::Normalized() const { float num = Vector2Of::MagnitudeOf(*this); Vector2Of result = Vector2Of::zero; if (num > Float::epsilon) { result = static_cast>(*this) / num; } return result; } // Explicit instantiation for int template class Vector2Of; template class Vector2Of; template class Vector2Of; #pragma endregion Vector2Of