-
Notifications
You must be signed in to change notification settings - Fork 1
/
find_intersections.m
51 lines (39 loc) · 1.26 KB
/
find_intersections.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
function [intersections, intersectionscase] = find_intersections(P1, P2, r1, r2)
d = norm(P1-P2);
if(r1+r2 < d)
intersections = zeros(0);
intersectionscase = "separated circles";
return;
endif
if(r1+d < r2)
intersections = zeros(0);
intersectionscase = "circle engulfed";
return;
endif
if(d+r2 < r1)
intersections = zeros(0);
intersectionscase = "circle engulfed";
return;
endif
middle = (P1 + P2) / 2;
if(r1+r2 == d)
intersections = middle;
intersectionscase = "one intersection";
return;
endif
c = norm(P1-P2); %# distance between circles' centers.
cosAlpha = (r1^2+c^2-r2^2)/(2*r1*c);
u_AB = (P2 - P1)/c; %# unit vector from first to second center.
pu_AB = [u_AB(2), -u_AB(1)]; %# perpendicular vector to unit vector.
sinAlpha = sqrt(1-cosAlpha^2);
base = P1 + u_AB * (r1*cosAlpha);
if(sinAlpha == 0)
intersections = base;
intersectionscase = "one intersection";
return;
endif
intersect_1 = base + (pu_AB * r1 * sinAlpha);
intersect_2 = base - (pu_AB * r1 * sinAlpha);
intersections = [intersect_1; intersect_2];
intersectionscase = "two intersections";
endfunction;