summaryrefslogtreecommitdiff
path: root/v2f.h
diff options
context:
space:
mode:
authorVito Caputo <vcaputo@pengaru.com>2019-05-06 22:19:05 -0700
committerVito Caputo <vcaputo@pengaru.com>2019-05-06 22:19:05 -0700
commitd5cc97b1d38ef7a54a7c1b58efd93d3d9eb371c5 (patch)
tree44dc94bd2f3387a5a8c0d320ab75da9d82084320 /v2f.h
parent7de752ffcc3f357001207c80dcef2fa12608a806 (diff)
v2f: add distance: v2f_distance()
v2f_distance_sq() variant omits the sqrtf(), which is often a possible optimization when e.g. comparing distances; just operate uniformly with squares of the distances and you can skip the expensive square roots.
Diffstat (limited to 'v2f.h')
-rw-r--r--v2f.h14
1 files changed, 14 insertions, 0 deletions
diff --git a/v2f.h b/v2f.h
index 9fd3b3f..4cbabd4 100644
--- a/v2f.h
+++ b/v2f.h
@@ -157,6 +157,20 @@ static inline float v2f_length(const v2f_t *v)
}
+static inline float v2f_distance(const v2f_t *a, const v2f_t *b)
+{
+ return v2f_length(v2f_sub(&(v2f_t){}, a, b));
+}
+
+
+static inline float v2f_distance_sq(const v2f_t *a, const v2f_t *b)
+{
+ v2f_t d = _v2f_sub(a, b);
+
+ return v2f_dot(&d, &d);
+}
+
+
static inline v2f_t _v2f_normalize(const v2f_t *v)
{
return _v2f_mult_scalar(v, 1.0f / v2f_length(v));
© All Rights Reserved