Created
July 27, 2015 23:22
-
-
Save mlabbe/3dd82b6d7e8f57cc6dfa to your computer and use it in GitHub Desktop.
Two implementations to extend a line.
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
| // worse for precision | |
| void extend_dividing_line(vec2 pt1, vec2 pt2, float len) | |
| { | |
| vec2 unit_v; | |
| vec2_sub(pt1,pt2,unit_v); | |
| vec2_normalize(unit_v, unit_v); | |
| vec2_mul(unit_v, len, unit_v); | |
| vec2_add(pt1, unit_v, pt1); | |
| vec2_sub(pt2, unit_v, pt2); | |
| } | |
| // better for precision | |
| void extend_dividing_line_by_units(vec2 pt1, vec2 pt2, float units) | |
| { | |
| float centroid[2] = {(pt2[0]-pt1[0]) * 0.5f + pt1[0], | |
| (pt2[1]-pt1[1]) * 0.5f + pt1[1]}; | |
| float lx = pt1[0] - pt2[0]; | |
| float ly = pt1[1] - pt2[1]; | |
| float p_length = sqrtf(lx*lx + ly*ly); | |
| float s1[2] = {pt1[0] + (pt1[0] - centroid[0]) / p_length * units, | |
| pt1[1] + (pt1[1] - centroid[1]) / p_length * units}; | |
| float s2[2] = {pt2[0] + (pt2[0] - centroid[0]) / p_length * units, | |
| pt2[1] + (pt2[1] - centroid[1]) / p_length * units}; | |
| memcpy(pt1, s1, sizeof(float)*2); | |
| memcpy(pt2, s2, sizeof(float)*2); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment