diff options
author | Campbell Barton <ideasman42@gmail.com> | 2011-06-26 15:08:12 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2011-06-26 15:08:12 +0400 |
commit | 933a65a76fff8ba7e15de9d8a552f10b9d02e7ec (patch) | |
tree | 00e28bb3097dc480ed41864058abe92e4f3fcb80 /source/blender/blenlib/intern/math_geom.c | |
parent | 913738a0429b2a11329260ee1f4e479b4a5af2cb (diff) |
2d version of line/circle intersec function.
Diffstat (limited to 'source/blender/blenlib/intern/math_geom.c')
-rw-r--r-- | source/blender/blenlib/intern/math_geom.c | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/source/blender/blenlib/intern/math_geom.c b/source/blender/blenlib/intern/math_geom.c index a71b60896fc..9d945e9baa3 100644 --- a/source/blender/blenlib/intern/math_geom.c +++ b/source/blender/blenlib/intern/math_geom.c @@ -420,6 +420,60 @@ int isect_line_sphere_v3(const float l1[3], const float l2[3], } } +/* keep in sync with isect_line_sphere_v3 */ +int isect_line_sphere_v2(const float l1[2], const float l2[2], + const float sp[2], const float r, + float r_p1[2], float r_p2[2]) +{ + const float ldir[2]= { + l2[0] - l1[0], + l2[1] - l1[1] + }; + + const float a= dot_v3v3(ldir, ldir); + + const float b= 2.0f * + (ldir[0] * (l1[0] - sp[0]) + + ldir[1] * (l1[1] - sp[1])); + + const float c= + dot_v2v2(sp, sp) + + dot_v2v2(l1, l1) - + (2.0f * dot_v2v2(sp, l1)) - + (r * r); + + const float i = b * b - 4.0f * a * c; + + float mu; + + if (i < 0.0f) { + /* no intersections */ + return 0; + } + else if (i == 0.0f) { + /* one intersection */ + mu = -b / (2.0f * a); + madd_v2_v2v2fl(r_p1, l1, ldir, mu); + return 1; + } + else if (i > 0.0) { + const float i_sqrt= sqrt(i); /* avoid calc twice */ + + /* first intersection */ + mu = (-b + i_sqrt) / (2.0f * a); + madd_v2_v2v2fl(r_p1, l1, ldir, mu); + + /* second intersection */ + mu = (-b - i_sqrt) / (2.0f * a); + madd_v2_v2v2fl(r_p2, l1, ldir, mu); + return 2; + } + else { + /* math domain error - nan */ + return -1; + } +} + /* -1: colliniar 1: intersection |