-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzones.m
185 lines (164 loc) · 7.76 KB
/
zones.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
classdef zones
%ZONES A zone that nodes must or can't enter
% Detailed explanation goes here
properties
shape
zoneType
location
end
methods
function obj = zones(shapeType, zoneType, coords)
%ZONES Construct an instance of this class
% Detailed explanation goes here
% Rectanges are defined by a 2*2 matrix, the top left and
% bottom right corner
% Triangles are defined by a 3*2 matrix, the 3 coords of the
% 3 points
% Circles are defined by a 1*3 vector, the radius, the x
% value of the center, then the y value
obj.location = coords;
obj.shape = shapeType;
obj.zoneType = zoneType;
switch(obj.shape)
case shapes.rectangle
[A,B] = size(obj.location);
if A ~= 2 || B ~= 2
error("Invalid coord dimentions\n" + ...
"Expected a [3,2] double and got a [%d,%d]", A,B)
end
case shapes.circle
[A,B] = size(obj.location);
if A ~= 1 || B ~= 3
error("Invalid coord dimentions\n" + ...
"Expected a [3,2] double and got a [%d,%d]", A,B)
end
case shapes.triangle
[A,B] = size(obj.location);
if A ~= 3 || B ~= 2
error("Invalid coord dimentions\n" + ...
"Expected a [3,2] double and got a [%d,%d]", A,B)
end
end
end
function isPossible = isRestricted(obj,trussStruct)
%METHOD1 Summary of this method goes here
% Detailed explanation goes here
% trussStruct.edgesArray(edgeIndex).endNodes(1) -> first node
% ID
% trussStruct.edgesArray(edgeIndex).endNodes(2) -> second node
% ID
% trussStruct.nodesArray(ID).x or .y to get coordinates
switch(obj.shape)
case {shapes.rectangle, shapes.triangle}
if obj.shape == shapes.rectangle
% [x1,y1;x2,y2]
% Top left ; bottom right
points = {point(obj.location(1,1), obj.location(1,2));
point(obj.location(1,1), obj.location(2,2));
point(obj.location(2,1), obj.location(1,2));
point(obj.location(2,1), obj.location(2,2));};
lineObjects = {lineObject(points{1},points{2});
lineObject(points{1},points{3});
lineObject(points{2},points{4});
lineObject(points{3},points{4});};
else
points = {point(obj.location(1,1), obj.location(1,2));
point(obj.location(2,1), obj.location(2,2));
point(obj.location(3,1), obj.location(3,2));};
lineObjects = {lineObject(points{1},points{2});
lineObject(points{1},points{3});
lineObject(points{2},points{3});};
end
for i = 1:trussStruct.numEdges
%convert edge to line
node_1 = trussStruct.nodesArray(trussStruct.edgesArray(i).endNodes(1));
node_2 = trussStruct.nodesArray(trussStruct.edgesArray(i).endNodes(2));
point_1 = point(node_1.x, node_1.y);
point_2 = point(node_2.x, node_2.y);
edgeLine = lineObject(point_1, point_2);
%any on cellfun lineobjects to check if any
%intercept
doesColide = any(cellfun(@(b) doIntercept(edgeLine, b),lineObjects));
%early return
if doesColide
isPossible = false;
return
end
end
isPossible = true;
case shapes.circle
isPossible = true;
for i = 1:length(trussStruct.edgesArray)
if isPossible
%Nodes
node1=trussStruct.edgesArray(i).endNodes(1);
node2=trussStruct.edgesArray(i).endNodes(2);
%Circle Location
cx = obj.location(2);
cy = obj.location(3);
%Relative X
x1 = trussStruct.nodesArray(node1).x - cx;
x2 = trussStruct.nodesArray(node2).x - cx;
%Relative Y
y1 = trussStruct.nodesArray(node1).y - cy;
y2 = trussStruct.nodesArray(node2).y - cy;
%...Magic...
a = (x2 - x1)^2 + (y2 - y1)^2;
b = 2 * (x1 * (x2 - x1) + y1 * (y2 - y1));
c = x1^2 + y1^2 - (obj.location(1))^2;
disc = b^2 - 4*a*c;
if disc > 0
sqrtdisc = sqrt(disc);
t1 = (-b + sqrtdisc)/(2*a);
t2 = (-b - sqrtdisc)/(2*a);
if ((0 < t1 && t1 < 1) || (0 < t2 && t2 < 1))
isPossible = false;
end
end
end
end
case shapes.triangle
3;
end
end
function isPossible = isWeightNodeInZone(obj, trussStruct)
isPossible = false;
weightNode = trussStruct.weightNode;
weightNodeLocation = [...
trussStruct.nodesArray(weightNode).x,...
trussStruct.nodesArray(weightNode).y];
switch(obj.shape)
case shapes.rectangle
% [x1,y1;x2,y2]
% Top left ; bottom right
if (...
(weightNodeLocation(1) > obj.location(1,1)) && (weightNodeLocation(1) < obj.location(2,1)) &&...
(weightNodeLocation(2) < obj.location(1,2)) && (weightNodeLocation(2) > obj.location(2,2)))
isPossible = true;
end
case shapes.circle
if (norm(obj.location(2:3) - weightNodeLocation) < obj.location(1))
isPossible = true;
end
case shapes.triange
end
end
function [point] = pointInZone(obj)
rand = [-1,-1];
while any(rand < 0) || any(rand > 1)
rand = normrnd(0.5,0.1941,1,2);
end
switch(obj.shape)
case shapes.rectangle
% [x1,y1;x2,y2]
% Top left ; bottom right
point = [obj.location(2,1)-obj.location(1,1), obj.location(1,2)-obj.location(2,2)] ...
.* rand + [obj.location(1,1),obj.location(2,2)];
case shapes.circle
point = obj.location(1:2) + ...
(rand - 0.5) .* obj.location(3);
case shapes.triangle
end
end
end
end