-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtriangle_intersection.asv
43 lines (33 loc) · 1.26 KB
/
triangle_intersection.asv
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
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.
% *******************************************************************
flag = false;
% *******************************************************************
end
%function to determine if point is inside a triangle
function val = point_inside_triangle(point, triangle)
% Choose first vertex of triangle as origin
% Construct vectors for triangle edges that make this vertex
vec_AC = triangle(3,:); - triangle(1,:);
vec_AB = triangle(2,:); - triangle(1,:);
% Construct the vector to the point
vec_AP = point - triangle(1,:);
% Compute dot products
dotACAC = dot(vec_AC, vec_AC);
dotACAB = dot(vec_AC, vec_AB);
dotACAP = dot(vec_AC, vec_AP);
dotABAB = dot(vec_AB, vec_AB);
dotABAP = dot(vec_AB, vec_AP);
% Compute barycentric coordinates
denom = dotACAC * dotABAB - dotACAB * dotACAB;
u = (dotABAB * dotACAP - dotACAB * dotABAP) / denom;
v = (dotACAC * dotABAP - dotACAB * dotACAP) / denom;
% Check if point is in triangle
if (u >= 0 && v >= 0 && u + v < 1)
val = true;
else
val = false;
end
return val
end