/* * Copyright 2019-2025 Andrey Pokidov * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System; /* * Author: Andrey Pokidov * Date: 1 Feb 2019 */ namespace BasicGeometry { public struct Vector2FP32 { public static readonly Vector2FP32 ZERO = new Vector2FP32(0.0f, 0.0f); public float x1 = 0.0f; public float x2 = 0.0f; public Vector2FP32(float x1, float x2) { this.x1 = x1; this.x2 = x2; } public Vector2FP32(in Vector2FP32 vector) { this.x1 = vector.x1; this.x2 = vector.x2; } public Vector2FP32(in Vector2FP64 vector) { this.x1 = (float)vector.x1; this.x2 = (float)vector.x2; } public readonly float GetSquareModule() { return this.x1 * this.x1 + this.x2 * this.x2; } public readonly float GetModule() { return MathF.Sqrt(this.GetSquareModule()); } public int Normalize() { float squareModule = this.GetSquareModule(); if (UtilityFP32.IsSqareValueUnit(squareModule)) { return 1; } if (squareModule <= UtilityFP32.SQUARE_EPSYLON) { this.Reset(); return 0; } float multiplier = MathF.Sqrt(1.0f / squareModule); this.x1 *= multiplier; this.x2 *= multiplier; return 1; } public void Reverse() { this.x1 = -this.x1; this.x2 = -this.x2; } public readonly bool IsZero() { return this.GetSquareModule() <= UtilityFP32.SQUARE_EPSYLON; } public readonly bool IsUnit() { return UtilityFP32.IsSqareValueUnit(this.GetSquareModule()); } public void Reset() { this.x1 = 0.0f; this.x2 = 0.0f; } public void SetValues(float x1, float x2) { this.x1 = x1; this.x2 = x2; } public void SetValues(in Vector2FP32 vector) { this.x1 = vector.x1; this.x2 = vector.x2; } public void SetValues(in Vector2FP64 vector) { this.x1 = (float)vector.x1; this.x2 = (float)vector.x2; } public void SetReverseOf(in Vector2FP32 vector) { this.x1 = -vector.x1; this.x2 = -vector.x2; } public void SetReverseOf(in Vector2FP64 vector) { this.x1 = -(float)vector.x1; this.x2 = -(float)vector.x2; } public void AppendScaled(Vector2FP32 summand, float scale) { this.x1 += summand.x1 * scale; this.x2 += summand.x2 * scale; } public readonly override string ToString() { return String.Format("SPVector2({0}, {1})", this.x1, this.x2); } public static void Add(in Vector2FP32 vector1, in Vector2FP32 vector2, out Vector2FP32 sum) { sum.x1 = vector1.x1 + vector2.x1; sum.x2 = vector1.x2 + vector2.x2; } public static void Subtract(in Vector2FP32 minuend, in Vector2FP32 subtrahend, out Vector2FP32 difference) { difference.x1 = minuend.x1 - subtrahend.x1; difference.x2 = minuend.x2 - subtrahend.x2; } public static void Multiply(in Vector2FP32 multiplicand, float multiplier, out Vector2FP32 product) { product.x1 = multiplicand.x1 * multiplier; product.x2 = multiplicand.x2 * multiplier; } public static void Divide(in Vector2FP32 dividend, float divisor, out Vector2FP32 quotient) { Multiply(dividend, 1.0f / divisor, out quotient); } public static void GetMean2(in Vector2FP32 vector1, in Vector2FP32 vector2, out Vector2FP32 result) { result.x1 = (vector1.x1 + vector2.x1) * 0.5f; result.x2 = (vector1.x2 + vector2.x2) * 0.5f; } public static void GetMean3(in Vector2FP32 vector1, in Vector2FP32 vector2, in Vector2FP32 vector3, out Vector2FP32 result) { result.x1 = (vector1.x1 + vector2.x1 + vector3.x1) * UtilityFP32.ONE_THIRD; result.x2 = (vector1.x2 + vector2.x2 + vector3.x2) * UtilityFP32.ONE_THIRD; } public static float GetScalarProduct(in Vector2FP32 vector1, in Vector2FP32 vector2) { return vector1.x1 * vector2.x1 + vector1.x2 * vector2.x2; } public static float GetCrossProduct(in Vector2FP32 vector1, in Vector2FP32 vector2) { return vector1.x1 * vector2.x2 - vector1.x2 * vector2.x1; } public static float GetAngle(in Vector2FP32 vector1, in Vector2FP32 vector2, AngleUnit unit) { float squareModule1 = vector1.GetSquareModule(); if (squareModule1 <= UtilityFP32.SQUARE_EPSYLON) { return 0.0f; } float squareModule2 = vector2.GetSquareModule(); if (squareModule2 <= UtilityFP32.SQUARE_EPSYLON) { return 0.0f; } float cosine = Vector2FP32.GetScalarProduct(vector1, vector2) / MathF.Sqrt(squareModule1 * squareModule2); if (1.0f - UtilityFP32.EPSYLON <= cosine) { return 0.0f; } if (cosine <= -(1.0f - UtilityFP32.EPSYLON)) { return AngleFP32.GetHalfCircle(unit); } return RadianFP32.ToUnits(MathF.Acos(cosine), unit); } public static float GetSquareDistance(in Vector2FP32 vector1, in Vector2FP32 vector2) { float dx1 = vector1.x1 - vector2.x1; float dx2 = vector1.x2 - vector2.x2; return dx1 * dx1 + dx2 * dx2; } public static float GetDistance(in Vector2FP32 vector1, in Vector2FP32 vector2) { return MathF.Sqrt(GetSquareDistance(vector1, vector2)); } public static bool AreEqual(in Vector2FP32 vector1, in Vector2FP32 vector2) { float squareModule1 = vector1.GetSquareModule(); float squareModule2 = vector2.GetSquareModule(); float squareModule3 = GetSquareDistance(vector1, vector2); // 2.0f means dimension amount if (squareModule1 < UtilityFP32.EPSYLON_EFFECTIVENESS_LIMIT || squareModule2 < UtilityFP32.EPSYLON_EFFECTIVENESS_LIMIT) { return squareModule3 < (2.0f * UtilityFP32.SQUARE_EPSYLON); } if (squareModule1 <= squareModule2) { return squareModule3 <= (2.0f * UtilityFP32.SQUARE_EPSYLON) * squareModule2; } return squareModule3 <= (2.0f * UtilityFP32.SQUARE_EPSYLON) * squareModule1; } } }