-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSAT.m
53 lines (46 loc) · 1.28 KB
/
SAT.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
52
53
function [Intersect] = SAT(P1,P2)
P1 = [P1;P1(1,:)];
P2 = [P2,;P2(1,:)];
Intersect = true;
for j = 2:4
x = P1(j,1) - P1(j-1,1);
y = P1(j,2) - P1(j-1,2);
x_ref = P1(j-1,1);
y_ref = P1(j-1,2);
x_rot = -y;
y_rot = x;
Intersect = CheckSide(P1, P2, x_rot, y_rot,x_ref,y_ref);
if (Intersect == false)
break;
end
end
if (Intersect == true)
for j=2:4
x = P2(j,1) - P2(j-1,1);
y = P2(j,2) - P2(j-1,2);
x_ref = P2(j-1,1);
y_ref = P2(j-1,2);
% compute the perpendcular to the edge vector
x_rot = -y;
y_rot = x;
Intersect = CheckSide(P1,P2, x_rot,y_rot,x_ref,y_ref);
if (Intersect == false)
break;
end
end
end
end
function intersect = CheckSide(P1,P2,x_rot,y_rot,x_ref,y_ref)
intersect = true;
side1 = zeros(1,3);
side2 = zeros(1,3);
for k=1:3
side1(k) = sign(x_rot * (P1(k,1) - x_ref) + y_rot * (P1(k,2) - y_ref));
end
for k=1:3
side2(k) = sign(x_rot * (P2(k,1) - x_ref) + y_rot * (P2(k,2) - y_ref));
end
if ( min(side1) > -1 && max(side2) < 1) || ( max(side1) < 1 && min(side2) > -1)
intersect = 0;
end
end