Ah... My fault, I read http://mathworld.wolfram.com/Circle-CircleIntersection.html #11 wrong. I only had the last half. The first half is killer And actually, I don't need anything more than an approximation. I could use tables, but this is actually not for a PIC, it is for a JavaScript demonstration of a system that would be developed with PICs: A vernier position encoder with a (round) light shining through a round hole in a gear. I'm trying to show how the vernier effect can be used to make the less precise and much larger shapes still give an accurate readout in much the same way that a verier caliper allows gross human sized rulers to measure fine dimensions. The visual simulation is valuable in that it can give some idea of how the signal changes with the size of the holes and the relative spacing. In any case, your code seems to be working. It slows the simulation down and I'm only doing one set of holes, but the simulation doesn't have to be fast, and I can see some ways to optimize it. Finally, THANK YOU! How the heck did you learn to write things like that? --- James Newton: PICList webmaster/Admin mailto:jamesnewton@piclist.com 1-619-652-0593 phone http://www.piclist.com/member/JMN-EFP-786 PIC/PICList FAQ: http://www.piclist.com > -----Original Message----- > From: pic microcontroller discussion list > [mailto:PICLIST@MITVMA.MIT.EDU] On Behalf Of Peter L. Peres > Sent: 2004 Jul 31, Sat 12:09 > To: PICLIST@MITVMA.MIT.EDU > Subject: [ee]: Compute area of overlap between two circles. > > > A = -0.5 * Math.Sqrt( (R1+R2-D) * (R1-R2+D) * (-R1+R2+D) * > (R1+R2+D) ) > > That forumla is certainly wrong. The area results negative. > > numerically you can solve it by numerical integration. use > the line that passes through both circle centers as ordinate > axis and integrate each circle's 'banana' area, on the domain > R*cos(A/2) to R, then add the two results. A is the center > angle that subtends the intersection points on the circle, > for each circle (you did not say identical radii). > > if D is the distance between the center points and R1, R2 are > the radii, and D <= R1+R2, then you can determine A/2, for > each circle, using the cosine theorem applied in the triange > D,R1,R2, i.e.: > > R2^2 = R1^2 + D^2 - 2*R1*D*cos(A1/2) -> > cos(A1/2) = 1/2 * (D^2 + R1^2 - R2^2) / (R1 * D) > ... > R1 * cos(A1/2) = 1/2 * (D + (R1^2 - R2^2) / D) > ... > R2 * cos(A2/2) = 1/2 * (D + (R2^2 - R1^2) / D) > > so for circle with R1 you integrate on the domain > R1*cos(A1/2) to R1, and for circle with R2 on R1*cos(A2/2) to > R2 and add the sums. Note that we do not need any trig > operations (cos(Ax/2) need never be solved for Ax). > > The circle equation is (in a form suitable for integer-only > calculations): > > x^2 + y^2 = R^2 > y = +/- sqrt(R^2 - x^2) > > which for integration purposes becomes (remember sqrt output > is +/- and we want both halves): > > y = 2 * sqrt(R^2 - x^2) > > so your integral sum should be (in a form computable by a pic): > > // D, R1 and R2 must be positive integers, D <= R1+R2 > // you supply sqrt() such that it works on a domain from 1 to > // max(R1^2,R2^2), with integer input and output > int i, Area; > > Area = 0; > for(i = 1/2 * (D + (R1^2 - R2^2) / D); i < R1; ++i) > Area += 2 * sqrt(R1^2 - i^2); > for(i = 1/2 * (D + (R2^2 - R1^2) / D); i < R2; ++i) > Area += 2 * sqrt(R2^2 - i^2); > return(Area); > // done > > barring any errrors (code not tested). The accuracy of the > integration will increase significantly by using a different > method than the summed squares method. > > But do you need accurate ? i.e. compute the area of the floor > of your funny-shaped swimming pool to the nearest square > micron and round up by 5% to account for losses in brush soak > and empty paint cans ? > > Note that the input to sqrt() above is always the square of > an integer or a difference of such squares. This leads to > implementation shortcuts. > > Peter > > -- > http://www.piclist.com hint: The list server can filter out > subtopics (like ads or off topics) for you. See > http://www.piclist.com/#topics > -- http://www.piclist.com hint: The list server can filter out subtopics (like ads or off topics) for you. See http://www.piclist.com/#topics