Hi,
I want to ask a question about overlap area between two circles in R.
Two circles intersect, the following link gives only one type, if the position of the centre of two intersecting circles changes (all in one of the two intersecting circles), the formula to calculate overlap area is still the same?
Is it possible to calculate them in one way, maybe Calculus？
Calculate the intersection area of two circles • Computer Science and Machine Learning (xarg.org)

Two circles may intersect at exactly 0, 1, 2, or infinitely many points, but for overlap area the only interesting case is at exactly 2 points which your link addresses. Which of the other cases are you interested in?

Like the picture(Zhoufei Lu,2016) shows, there are three different case when it is exactly 2 points. library(PlaneGeometry)
# this block solely to illustrate the problem
O1 <- c(3,4); circ1 <- Circle$new(O1, 2) O2 <- c(6,5); circ2 <- Circle$new(O2, 1.75)
intersections <- intersectionCircleCircle(circ1, circ2)
A <- intersections[]; B <- intersections[]
theta1 <- Arg((A-O1) + 1i*(A-O1))
theta2 <- Arg((B-O1) + 1i*(B-O1))
path1 <- Arc$new(O1, circ1$radius, theta1, theta2, FALSE)$path() theta1 <- Arg((A-O2) + 1i*(A-O2)) theta2 <- Arg((B-O2) + 1i*(B-O2)) path2 <- Arc$new(O2, circ2$radius, theta2, theta1, FALSE)$path()
plot(0, 0, type="n", xlim = c(0,10), ylim = c(0,10),xlab = NA, ylab = NA)
grid()
draw(circ1, border = "blue", lwd = 2)
draw(circ2, border = "forestgreen", lwd = 2)
polypath(rbind(path1,path2), col = "red")
points(3,4,pch=19, col = "blue")
text(3,5,"A")
text(3,3.55,"r=2")
points(6,5,pch=19, col = "forestgreen")
text(6,6,"B")
text(6,4.5,"r=1.75") # this block calculates the area of the intersection

area <- function(A, B) {
d <- gsl::hypot(B$x - A$x, B$y - A$y)
if (d <= abs(B$r - A$r)) return (pi * min(a, b))
if (d < A$r + B$r) {
a <- A$r^2 b <- B$r^2
x <- (a - b + d^2) / (2 * d)
z <- x^2
y <- sqrt(a - z)
return (a * asin(y / A$r) + b * asin(y / B$r) - y * (x + sqrt(z + b - a)))
}
return (0)
}

A <- data.frame(x = 3, y = 4, r = 2)
B <- data.frame(x = 6, y = 5, r = 1.75)

area(A,B)
#>  0.8008107


Created on 2023-05-09 with reprex v2.0.2

2 Likes

Wow, great job, helped me a lot.
The example you show seems to be familiar with the case I in the picture, the circlecenter is in its own circle respectively. Well, in caseII and caseIII the relative location of two centres changed(distance change). The formula is still the same?
Thank you for your any help. The solution is affected by the x,y coordinates a d r only to the extent of whether they produce Case IV or Case V (no intersection), in which case the result is a union or disjunction. That will produce an area of \pi ^2 in the first case, where r is the radius of the smaller circle, and 0 of the second. Otherwise, for all other \inf cases in which there are two points of intersection the solution will be some positive real number.

Yeah, I get it. You mean that the process(even the formula) to get the overlap area is the same with the follwing post in all two point case? Like the following post?

If I understand your question, you can place Case I, Case II or Case III anywhere around the circumference of the circle with the same result so long as there are two points of intersection.

OK，would you mind gave me some opinion about the comment in this link bottom?
Calculate the intersection area of two circles • Computer Science and Machine Learning (xarg.org)

What do you think should be difference in the case where circles overlap but both radius points are within the same circle?

I apologize for my lack of math knowledge . After I look for some articles about it, you are right. You have solved my problem. I am very grateful for your effort and help on this matter .

1 Like

No worries and, especially, no apologies. Almost all of us suffers in math because it's hard. The reason it is hard is that either the way it is taught, or by the way we are taught from other study to approach it, is that math is a pursuit of the right answer. That's completely backwards.

An answer to a math problem might be tedious and there are math problems that only those who devote their lives to mathematics encounter that are truly complex. But what most math problems do to trip us up is to hide in plain sight the secret that the hard part is thinking of the problem not as an answer, but as a question.

It took me about 30 years to figure this out—between the time I took high school algebra and the time I had been a practicing lawyer in Wall Street financial engineering for a few years. In the interval I struggled through college pre-calculus (where to this day I haven't really grasped trigonometry), calculus (where I could prove the theorems but couldn't solve the exercises because it turned out that I'm mildly numerically dyslexic, always coming up with different simple add, subtract, multiply and divide results because I transposed numbers without noticing). I flunked out of a geophysics Ph.D. program (taking away the consolation master's degree) and didn't gain traction until my second master's degree program, in a non-STEM discipline where my feeble skills in math made me a giant able to deploy Student's t to be able to detect if the difference between metrics applied to air photo interpretations from 1951 to 1971 in three Massachusetts townships were, or were not, due to random variation.

Then, I didn't do much for the next 10 years until I got interested in suddenly accessible programming tools in the early 80s. I saw dimly that no matter how carefully I crafted C code the results were garbage unless I had first carefully thought about what the code was supposed actually to do, to accomplish.

By 1993, I was a hot-shot Wall Street enabler reverse engineering descriptions of complex cashflows in mortgage backed securitizations into word problems—dealing with tiny distinctions about the origin and destination of different types of receipts to be distributed to investors and the nuances of timing. Fortunately, the guy who had to stress test what I came up with was a tax lawyer who moved on from being a full professor of mathematics to the big bucks in Big Law (big only because there was a lot of overtime priced in for the all-nighters).

After that, it was another 15 years before I had to think hard about how to pose math questions, in 2007 in the run up to the mortgage lending and securitization meltdown when the issue was whether my firm's securitized loan portfolio was, or was not, poorer performing than its competitors given the disclosed mortgage loan characteristics. This turned out for me personally to be a \$30 billion dollar question, because that was how much disappointed investors were suing me for, personally. (Long story, but they never collected because other people who were being sued settled on my behalf.)

That was when I first picked up the beginnings of R. I quickly gravitated to the tidyverse and spend much time during my (continuing) unemployment after my employer went bust in 2008, trying better to understand it and to answer the questions left pending of WTF happened.

A few years ago I realized that despite my many data science certificates in courses based on R and Python that I was falling deeper and deeper into the pit of how do I do X?

The reason that is a pit is that with dedication it is possible to find an answer to the wrong question the real effort is in finding the right question. With the right question, the appropriate tools are almost trivial. Rather than worrying about how to dplyr::filter(object) |> map(something) |> [display]() with all the picky pieces, I began to think in terms of school algebra—f(x) = y.

x is my object
y is what I want to know
f transforms x into y

any of which may be, and usually are, composite.

After few years, it was almost as if once I recognized the problem the solution fell into my lap, so I turned my attention to recognizing the problem.

That was the motivation for my question about how the case in which the center of the smaller circle being within the larger circle. My hope was that would nudge thinking in the right direction, and I'm delighted if it did.

1 Like

Many thanks for your kind and warm help，sharing your special and unique experience about figuring about the sentence（" But what most math problems do to trip us up is to hide in plain sight the secret that the hard part is thinking of the problem not as an answer, but as a question ."), this was very inspiring to me. Actually, my major is about forest ecology, not math, but must use it. I would read this experience you shared with me again and again when familiar problems cames up.May be another 30 years to figure it out , it would be fortune，invaluable fortune.
I am sincerely thank you for your help about the question itself and others.

1 Like

I would like to suggest a different idea about math problems in R.

I try to do the math problem first outside of R. If I can't solve it outside of R, I am often unlikely to be able to understand and solve it with R, but this one is an interesting exception.

In math, one method of attack is, when you can't solve the problem in front of you (solve the general problem of circle overlap), then try to solve a smaller subset of that problem first. I tried that with a specific example of two circles with given centers and radii.

I got stuck at finding the two points of intersection. This is doable with algebra, but it is a complicated algebra problem and the chance for making an error is high, and truthfully, I gave up.

I was surprised and pleased that Technocrat shared an R package PlaneGeometry with a function intersectionCircleCircle that finds the two points of intersection, which speeds up the manual calculation. And by the way, the use of Arg is quite clever!

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.