Original Post
Here is my code atm:
the computed value 'x' is such that a.pos + x*unit(delta) would be the point between the circles at which the bisector would intersect the two intersection points of the circles.
the last two lines in the final return computer the area of the two circular segments either side of the bisector which at least thankfully, works even when the bisector does not lie between the two centres.
It is not so much the square roots which bother me, as i have a fast sqrt in place which can be used. What bothers me most is the inverse cosines, which i guess could be optimised with a table lookup as accuracy is not essential here, but it would interest me if there were a way to write this function to be simpler.
typedef Circle = { pos : Vector2; rad : Float;}function intersection(a:Circle, b:Circle) { var delta = b.pos - a.pos; var dl = dot(delta,delta); var dr = b.rad + a.rad; //not intersecting if(dl > dr*dr) return 0; var dls = sqrt(dl); var ar2 = a.rad*a.rad; var br2 = b.rad*b.rad; var x = (dl - br2 + ar2) / (2*dls); //circle a totally contained in circle b if(x < -a.rad) return pi*ar2; //circle b totally contained in circle a var x2 = dls - x; if(x2 < -b.rad) return pi*br2; var ta = acos(x / a.rad); var tb = acos(x2 / b.rad); return ta*ar2 - x*sqrt(ar2 - x*x) + tb*br2 - x2*sqrt(br2 - x2*x2);}the computed value 'x' is such that a.pos + x*unit(delta) would be the point between the circles at which the bisector would intersect the two intersection points of the circles.
the last two lines in the final return computer the area of the two circular segments either side of the bisector which at least thankfully, works even when the bisector does not lie between the two centres.
It is not so much the square roots which bother me, as i have a fast sqrt in place which can be used. What bothers me most is the inverse cosines, which i guess could be optimised with a table lookup as accuracy is not essential here, but it would interest me if there were a way to write this function to be simpler.