Last active
April 24, 2018 22:30
-
-
Save tdhooper/e4e7d442a06ce3974c0b8b6ae5c0f8c6 to your computer and use it in GitHub Desktop.
Faster helix distance
This file contains 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
vec2 closestPointOnRepeatedLine(vec2 line, vec2 point){ | |
// Angle of the line | |
float a = atan(line.x, line.y); | |
// Rotate space so we can easily repeat along | |
// one dimension | |
pR(point, -a); | |
// Repeat to create parallel lines at the corners | |
// of the vec2(lead, radius) polar bounding area | |
float repeatSize = sin(a) * line.y; | |
float cell = pMod1(point.x, repeatSize); | |
// Rotate space back to where it was | |
pR(point, a); | |
// Closest point on a line | |
line = normalize(line); | |
float d = dot(point, line); | |
vec2 closest = line * d; | |
// Part 2 of the repeat, move the line along it's | |
// perpendicular by the repeat cell | |
vec2 perpendicular = vec2(line.y, -line.x); | |
closest += cell * repeatSize * perpendicular; | |
return closest; | |
} | |
// Closest point on a helix | |
vec3 closestHelix(vec3 p, float lead, float radius) { | |
p = cartToPolar(p); | |
p.y *= radius; | |
vec2 line = vec2(lead, radius * PI * 2.); | |
vec2 closest = closestPointOnRepeatedLine(line, p.xy); | |
closest.y /= radius; | |
vec3 closestCart = polarToCart(vec3(closest, radius)); | |
return closestCart; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment