-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTriangle.m
More file actions
114 lines (90 loc) · 2.43 KB
/
Triangle.m
File metadata and controls
114 lines (90 loc) · 2.43 KB
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
function v = Triangle(v0, x, y, vertices)
% Triangle with arbitrary verticies inspired by
% https://math.stackexchange.com/questions/274712/calculate-on-which-side-of-a-straight-line-is-a-given-point-located
% v0: Triangle has value of v0 inside, and 0 outside.
% x, y: x and y axis, use linspace to set their value.
% verticies: an array containing 2d data points.
% e.g: [[1,0]; [-1, 0]; [0, 2]]
Lx = length(x);
Ly = length(y);
v = zeros(Ly, Lx);
[~, I] = sort(vertices(:,2));
vertices = vertices(I,:);
a = vertices(1, :);
b = vertices(2, :);
c = vertices(3, :);
for t = 1:6
for p = 1:Lx
for q = 1:Ly
if sign( (x(p) - c(1))*(a(2)-c(2)) - (y(q) - c(2))*(a(1) - c(1)) ) <= 0
if sign( (x(p) - c(1))*(b(2)-c(2)) - (y(q) - c(2))*(b(1) - c(1)) ) >= 0
if sign( (x(p) - a(1))*(b(2)-a(2)) - (y(q) - a(2))*(b(1) - a(1)) ) <= 0
v(q, p) = v0;
end
end
end
end
end
%We have no idea where is right and where is left,
%so we keep exchanging indicies until we find a solution
if any(v,'all')
break
elseif t == 1
a = vertices(2, :);
b = vertices(1, :);
c = vertices(3, :);
elseif t == 2
a = vertices(3, :);
b = vertices(2, :);
c = vertices(1, :);
elseif t == 3
a = vertices(1, :);
b = vertices(3, :);
c = vertices(2, :);
elseif t == 4
a = vertices(2, :);
b = vertices(3, :);
c = vertices(1, :);
elseif t == 5
a = vertices(3, :);
b = vertices(1, :);
c = vertices(2, :);
end
end
% Plot 3 lines that makes a triangle
% m1 = ( a(2) - b(2) )/( a(1) - b(1) );
% m2 = ( a(2) - c(2) )/( a(1) - c(1) );
% m3 = ( c(2) - b(2) )/( c(1) - b(1) );
%
% if isinf(m1)
% Xab = a(1)*ones(1,Lx);
% Yab = y;
% else
% Xab = x;
% Yab = m1*(x - b(1)) + b(2);
% end
%
%
% if isinf(m2)
% Xac = a(1)*ones(1,Lx);
% Yac = y;
% else
% Xac = x;
% Yac = m2*(x - c(1)) + c(2);
% end
%
%
% if isinf(m3)
% Xcb = b(1)*ones(1,Lx);
% Ycb = y;
% else
% Xcb = x;
% Ycb = m3*(x - b(1)) + b(2);
% end
%
%
% hold on
% plot(Xab, Yab)
% plot(Xac, Yac)
% plot(Xcb, Ycb)
end