From 62d3f821076084ad2aa424e80107707e1d817305 Mon Sep 17 00:00:00 2001 From: Anilforextra Date: Thu, 10 Mar 2022 00:29:43 +0545 Subject: [PATCH] Vector2/Vector3: Add squared distance. --- src/raymath.h | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/raymath.h b/src/raymath.h index 00c134d8994c..d617fdcee821 100644 --- a/src/raymath.h +++ b/src/raymath.h @@ -278,6 +278,14 @@ RMAPI float Vector2Distance(Vector2 v1, Vector2 v2) return result; } +// Calculate square distance between two vectors +RMAPI float Vector2DistanceSqr(Vector2 v1, Vector2 v2) +{ + float result = ((v1.x - v2.x)*(v1.x - v2.x) + (v1.y - v2.y)*(v1.y - v2.y)); + + return result; +} + // Calculate angle from two vectors RMAPI float Vector2Angle(Vector2 v1, Vector2 v2) { @@ -548,6 +556,19 @@ RMAPI float Vector3Distance(Vector3 v1, Vector3 v2) return result; } +// Calculate square distance between two vectors +RMAPI float Vector3DistanceSqr(Vector3 v1, Vector3 v2) +{ + float result = 0.0f; + + float dx = v2.x - v1.x; + float dy = v2.y - v1.y; + float dz = v2.z - v1.z; + result = dx*dx + dy*dy + dz*dz; + + return result; +} + // Calculate angle between two vectors RMAPI float Vector3Angle(Vector3 v1, Vector3 v2) {