Last active
January 15, 2018 00:11
-
-
Save bit-hack/c38b080355f545a3f0a7a49e8f22c6de to your computer and use it in GitHub Desktop.
2d interval insersection for isometric water-land computation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- | |
float constexpr lerp(float a0, float a1, float i) { | |
return a0 + (a1 - a0) * i; | |
} | |
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- | |
void intersect(float a0, float a1, float b0, float b1, vec2f_t &out) { | |
#if 1 | |
const float i = (a0 - b0) / (a0 - a1 - b0 + b1); | |
if (i < 0.f) { | |
(out.x = 0.f), (out.y = a0); | |
return; | |
} | |
if (i > 1.f) { | |
(out.x = 1.f), (out.y = a1); | |
return; | |
} | |
(out.x = i), (out.y = lerp(a0, a1, i)); | |
#else | |
const float denom = (a - b - c + d); | |
if (fabsf(denom) < 0.5f) { | |
out = vec2f_t{1.f, b}; | |
} else { | |
out = vec2f_t{(a - c) / denom, (a * d - b * c) / denom}; | |
} | |
#endif | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment