-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtriangle_intersection.m
155 lines (154 loc) · 3.15 KB
/
triangle_intersection.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
function flag = triangle_intersection(P1, P2)
% triangle_test : returns true if the triangles overlap and false otherwise
%%% All of your code should be between the two lines of stars.
% *******************************************************************
%calculating the equations for each side of the two triangles
% m = [];
% c =[];
% m(1) = (P1(2,2)-P1(1,2)) / (P1(2,1)-P1(1,1));
% if (m(1)==Inf)
% c(1) = P1(1,1);
% else
% c(1) = P1(1,2)- m(1)*P1(1,1);
% end
%
% m(2) = (P1(3,2)-P1(2,2)) / (P1(3,1)-P1(2,1));
% if (m(2)==Inf)
% c(2) = P1(2,1);
% else
% c(2) = P1(2,2)- m(2)*P1(2,1);
% end
%
% m(3) = (P1(3,2)-P1(1,2) )/ (P1(3,1)-P1(1,1));
% if (m(3)==Inf)
% c(3) = P1(1,1);
% else
% c(3) = P1(1,2)- m(3)*P1(1,1);
% end
% %second triangle begins here
% m(4) = (P2(2,2)-P2(1,2) )/ (P2(2,1)-P2(1,1));
% if (m(4)==Inf)
% c(4) = P2(1,1);
% else
% c(4) = P2(1,2)- m(4)*P2(1,1);
% end
%
% m(5) = (P2(3,2)-P2(2,2)) / (P2(3,1)-P2(2,1));
% if (m(5)==Inf)
% c(5) = P2(2,1);
% else
% c(5) = P2(2,2)- m(5)*P2(2,1);
% end
%
% m(6) = (P2(3,2)-P2(1,2))/ (P2(3,1)-P2(1,1));
% if(m(6)==Inf)
% c(6) = P2(1,1);
% else
% c(6) = P2(1,2)- m(6)*P2(1,1);
% end
% % display('correct till here')
% %checking for vertices of tri 2 with eqns of tri 1
% s = [];
% i1=1;
% j1=1;
% for j1 = 1:3 %looping for eqns
% for i1=1:3 %looping for vertices
% if (m(j1)==Inf)
% s(i1,j1) = P2(i1,1) - c(j1);
% else
% s(i1,j1) = (P2(i1,2) - m(j1)*P2(i1,1) - c(j1));
% end
% end
% end
% %third vertex for P1
% o1 = [];
% if (m(1)==Inf)
% o1(1) = (P1(3,1) - c(1));
% else
% o1(1) = (P1(3,2) - m(1)*P1(3,1) - c(1));
% end
%
% if (m(2)==Inf)
% o1(2) = (P1(1,1) - c(2));
% else
% o1(2) = (P1(1,2) - m(2)*P1(1,1) - c(2));
% end
%
% if (m(3)==Inf)
% o1(3) = (P1(2,1) - c(3));
% else
% o1(3) = (P1(2,2) - m(3)*P1(2,1) - c(3));
% end
%
% o1 = o1';
% i=1;
% new = NaN(3,3);
% for i=1:3
% new(:,i) = s(:,i).*o1(i);
% end
% new2 = (new<0);
% for k =1:3
% if all(new2(:,k)==1)
% flag = false;
% break;
% else
% flag = true;
% end
% end
% s1 = [];
% i2 = 1;
% j2 = 4;
% for j2 = 4:6 %looping for eqns
% for i2=1:3 %looping for vertices
% if (m(j2)==Inf)
% s1(i2,j2) = P1(i2,1) - c(j2);
% else
% s1(i2,j2) = (P1(i2,2) - m(j2)*P1(i2,1) - c(j2));
% end
% end
% end
%
% o2 = [];
% if (m(4)==Inf)
% o2(1) = (P2(3,1) - c(1));
% else
% o2(1) = (P2(3,2) - m(4)*P2(3,1) - c(1));
% end
%
% if (m(5)==Inf)
% o2(2) = (P2(1,1) - c(2));
% else
% o2(2) = (P2(1,2) - m(5)*P2(1,1) - c(2));
% end
%
% if (m(6)==Inf)
% o2(3) = (P2(2,1) - c(3));
% else
% o2(3) = (P2(2,2) - m(6)*P2(2,1) - c(3));
% end
%
% o2=o2';
% i3=1;
% new3 = NaN(3,3);
% for i3=1:3
% new3(:,i3) = s1(:,i3).*o2(i3);
% end
% new4 = (new3<0);
% k = 1;
% for k =1:3
% if all(new4(:,k)==1)
% flag1 = false;
% break;
% else
% flag1 = true;
% end
% end
%
% if flag && flag1 == true
% flag = true;
% else
% flag = false;
% end
flag = SAT(P1,P2);
% *******************************************************
end