Post Reply 
(41) Intersection points between circles
12-01-2020, 12:17 AM (This post was last modified: 12-01-2020 12:23 AM by Albert Chan.)
Post: #9
RE: Intersection points between circles
(11-30-2020 05:19 PM)rawi Wrote:  alpha = arcsin((y2-y1)/z)
...
We set sg1 = 1 if x2>=x1 and sg1 = -1 if x2<x1
and likewise sg2 = 1 fi y2 >= y1 and sg2 = -1 if y2 < y1.

sx1 = x1 + r1*(cos(alpha+beta))*sg1
sy1 = y1 + r1*(sin(alpha+beta))*sg2
sx2 = x1 + r1*(cos(alpha-beta))*sg1
sy2 = y1 + r1*(sin(alpha-beta))*sg2

If x2 ≥ x1, we have the correct angle. No correction is needed.

If x2 < x1, calculated alpha should really be pi - alpha

cos((pi - alpha) ± beta) = cos(pi - (alpha ∓ beta)) = cos(alpha ∓ beta) * -1
sin((pi - alpha) ± beta) = sin(pi - (alpha ∓ beta)) = sin(alpha ∓ beta)

→ sy does not require correction.

Code:
function intersection(p1,r1, p2, r2)
    p2 = p2 - p1        -- p2 relative to p1
    local d = p2:abs()
    local a = asin(p2:imag()/d)
    local b = acos((d*d+(r1+r2)*(r1-r2)) / (2*d*r1))
    local sg1 = p2:real()<0 and -1 or 1
    return p1 + r1*(cos(a+b)*sg1 + sin(a+b)*I),
           p1 + r1*(cos(a-b)*sg1 + sin(a-b)*I)
end

lua> I = require'complex'.I
lua> p1, r1 = 13+4*I, 6.8
lua> p2, r2 = 6+10*I, 4.0
lua>
lua> intersection(p1,r1,p2,r2)
9.99870384656476+10.101821154325554*I      6.510943212258768+6.032767080968563*I
lua> intersection(p2,r2,p1,r1)
9.99870384656476+10.101821154325554*I      6.51094321225877+6.032767080968563*I
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Intersection points between circles - Albert Chan - 12-01-2020 12:17 AM



User(s) browsing this thread: 1 Guest(s)