dist (`lygia`/`math`/`dist`)

Commonly used distance functions.

Check it on Github

``````
#ifndef DIST_FNC
#define DIST_FNC distEuclidean
#endif

#ifndef DIST_MINKOWSKI_P
#define DIST_MINKOWSKI_P 2.0 // 1: Manhattan, 2: Euclidean, Infinity: Chebychev
#endif

#ifndef FNC_DIST
#define FNC_DIST

float distEuclidean(vec2 a, vec2 b) { return distance(a, b); }
float distEuclidean(vec3 a, vec3 b) { return distance(a, b); }
float distEuclidean(vec4 a, vec4 b) { return distance(a, b); }

// https://en.wikipedia.org/wiki/Taxicab_geometry
float distManhattan(vec2 a, vec2 b) { return abs(a.x - b.x) + abs(a.y - b.y); }
float distManhattan(vec3 a, vec3 b) { return abs(a.x - b.x) + abs(a.y - b.y) + abs(a.z - b.z); }
float distManhattan(vec4 a, vec4 b) { return abs(a.x - b.x) + abs(a.y - b.y) + abs(a.z - b.z) + abs(a.w - b.w); }

// https://en.wikipedia.org/wiki/Chebyshev_distance
float distChebychev(vec2 a, vec2 b) { return max(abs(a.x - b.x), abs(a.y - b.y)); }
float distChebychev(vec3 a, vec3 b) { return max(abs(a.x - b.x), max(abs(a.y - b.y), abs(a.z - b.z))); }
float distChebychev(vec4 a, vec4 b) { return max(abs(a.x - b.x), max(abs(a.y - b.y), max(abs(a.z - b.z), abs(a.w - b.w) ))); }

// https://en.wikipedia.org/wiki/Minkowski_distance
float distMinkowski(vec2 a, vec2 b) { return  pow(pow(abs(a.x - b.x), DIST_MINKOWSKI_P) + pow(abs(a.y - b.y), DIST_MINKOWSKI_P), 1.0 / DIST_MINKOWSKI_P); }
float distMinkowski(vec3 a, vec3 b) { return  pow(pow(abs(a.x - b.x), DIST_MINKOWSKI_P) + pow(abs(a.y - b.y), DIST_MINKOWSKI_P) + pow(abs(a.z - b.z), DIST_MINKOWSKI_P), 1.0 / DIST_MINKOWSKI_P); }
float distMinkowski(vec4 a, vec4 b) { return  pow(pow(abs(a.x - b.x), DIST_MINKOWSKI_P) + pow(abs(a.y - b.y), DIST_MINKOWSKI_P) + pow(abs(a.z - b.z), DIST_MINKOWSKI_P) + pow(abs(a.w - b.w), DIST_MINKOWSKI_P), 1.0 / DIST_MINKOWSKI_P); }

float dist(vec2 a, vec2 b) { return DIST_FNC(a, b); }
float dist(vec3 a, vec3 b) { return DIST_FNC(a, b); }
float dist(vec4 a, vec4 b) { return DIST_FNC(a, b); }

#endif
``````

Check it on Github

``````
#ifndef DIST_FNC
#define DIST_FNC distEuclidean
#endif

#ifndef DIST_MINKOWSKI_P
#define DIST_MINKOWSKI_P 2 // 1: Manhattan, 2: Euclidean, Infinity: Chebychev
#endif

#ifndef FNC_DIST
#define FNC_DIST

float distEuclidean(float2 a, float2 b) {
return distance(a, b);
}

float distEuclidean(float3 a, float3 b) {
return distance(a, b);
}

// https://en.wikipedia.org/wiki/Taxicab_geometry
float distManhattan(float2 a, float2 b) {
return abs(a.x - b.x) + abs(a.y - b.y);
}

float distManhattan(float3 a, float3 b) {
return abs(a.x - b.x) + abs(a.y - b.y) + abs(a.z - b.z);
}

// https://en.wikipedia.org/wiki/Chebyshev_distance
float distChebychev(float2 a, float2 b) {
return max(abs(a.x - b.x), abs(a.y - b.y));
}

float distChebychev(float3 a, float3 b) {
return max(abs(a.x - b.x), max(abs(a.y - b.y), abs(a.z - b.z)));
}

// https://en.wikipedia.org/wiki/Minkowski_distance
float distMinkowski(float2 a, float2 b) {
return  pow(pow(abs(a.x - b.x), DIST_MINKOWSKI_P)
+ pow(abs(a.y - b.y), DIST_MINKOWSKI_P),
1.0 / DIST_MINKOWSKI_P);
}

float distMinkowski(float3 a, float3 b) {
return  pow(pow(abs(a.x - b.x), DIST_MINKOWSKI_P)
+ pow(abs(a.y - b.y), DIST_MINKOWSKI_P)
+ pow(abs(a.z - b.z), DIST_MINKOWSKI_P),
1.0 / DIST_MINKOWSKI_P);
}

float dist(float2 a, float2 b) { return DIST_FNC(a, b); }
float dist(float3 a, float3 b) { return DIST_FNC(a, b); }

#endif
``````

Check it on Github

``````
const DIST_MINKOWSKI_P: f32 = 2.0; // 1: Manhattan, 2: Euclidean, Infinity: Chebychev

fn distEuclidean2(a: vec2f, b: vec2f) -> f32 { return distance(a, b); }
fn distEuclidean3(a: vec3f, b: vec3f) -> f32 { return distance(a, b); }
fn distEuclidean4(a: vec4f, b: vec4f) -> f32 { return distance(a, b); }

// https://en.wikipedia.org/wiki/Taxicab_geometry
fn distManhattan2(a: vec2f, b: vec2f) -> f32 { return abs(a.x - b.x) + abs(a.y - b.y); }
fn distManhattan3(a: vec3f, b: vec3f) -> f32 { return abs(a.x - b.x) + abs(a.y - b.y) + abs(a.z - b.z); }
fn distManhattan4(a: vec4f, b: vec4f) -> f32 { return abs(a.x - b.x) + abs(a.y - b.y) + abs(a.z - b.z) + abs(a.w - b.w); }

// https://en.wikipedia.org/wiki/Chebyshev_distance
fn distChebychev2(a: vec2f, b: vec2f) -> f32 { return max(abs(a.x - b.x), abs(a.y - b.y)); }
fn distChebychev3(a: vec3f, b: vec3f) -> f32 { return max(abs(a.x - b.x), max(abs(a.y - b.y), abs(a.z - b.z))); }
fn distChebychev4(a: vec4f, b: vec4f) -> f32 { return max(abs(a.x - b.x), max(abs(a.y - b.y), max(abs(a.z - b.z), abs(a.w - b.w) ))); }

// https://en.wikipedia.org/wiki/Minkowski_distance
fn distMinkowski2(a: vec2f, b: vec2f) -> f32 { return  pow(pow(abs(a.x - b.x), DIST_MINKOWSKI_P) + pow(abs(a.y - b.y), DIST_MINKOWSKI_P), 1.0 / DIST_MINKOWSKI_P); }
fn distMinkowski3(a: vec3f, b: vec3f) -> f32 { return  pow(pow(abs(a.x - b.x), DIST_MINKOWSKI_P) + pow(abs(a.y - b.y), DIST_MINKOWSKI_P) + pow(abs(a.z - b.z), DIST_MINKOWSKI_P), 1.0 / DIST_MINKOWSKI_P); }
fn distMinkowski4(a: vec4f, b: vec4f) -> f32 { return  pow(pow(abs(a.x - b.x), DIST_MINKOWSKI_P) + pow(abs(a.y - b.y), DIST_MINKOWSKI_P) + pow(abs(a.z - b.z), DIST_MINKOWSKI_P) + pow(abs(a.w - b.w), DIST_MINKOWSKI_P), 1.0 / DIST_MINKOWSKI_P); }

fn dist2(a: vec2f, b: vec2f) -> f32 { return distEuclidean2(a, b); }
fn dist3(a: vec3f, b: vec3f) -> f32 { return distEuclidean3(a, b); }
fn dist4(a: vec4f, b: vec4f) -> f32 { return distEuclidean4(a, b); }

``````