-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp1.js
342 lines (265 loc) · 11.5 KB
/
p1.js
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
class p1
{
constructor()
{
this.grid = new Grid(400,400);
this.green_indexes = [];
this.permanent_green_indexes =[];
//setup empty circle
this.initialCircle = new Circle(0,0, 0, 'Blue');
this.grid.svg.appendChild(this.initialCircle.getCircle());
//Setup listeners
this.grid.svg.addEventListener("mousedown", this.start.bind(this));
this.grid.svg.addEventListener("mousemove", this.move.bind(this));
this.grid.svg.addEventListener("mouseup", this.end.bind(this));
}
returnSvg()
{
return this.grid.svg;
}
/* function for handling the event when the mouse is pressed down.*/
start(e)
{
this.radiusXOne = 0, this.radiusYOne = 0, this.radiusXTwo = 0, this.radiusYTwo = 0;
//gathers the click information
var pt = this.grid.svg.createSVGPoint(), svgP;
pt.x = e.clientX;
pt.y = e.clientY;
svgP = pt.matrixTransform(this.grid.svg.getScreenCTM().inverse());
//radiusXOne and radiusYOne is the center of the circle
this.radiusXOne = svgP.x;
this.radiusYOne = svgP.y;
//sets the move flag to true, so we can start building the circle
this.moved = true;
}
/* function for handling the event when the mouse is moved.*/
move(e)
{
if (this.moved == true)
{
//remove the earlier circle as we are moving the mouse and creating a new circle
this.grid.svg.removeChild(this.initialCircle.getCircle());
//gathers the click information
var pt = this.grid.svg.createSVGPoint(), svgP;
pt.x = e.clientX;
pt.y = e.clientY;
svgP = pt.matrixTransform(this.grid.svg.getScreenCTM().inverse());
//Compute the radius and update the circle as the mouse is being moved
this.radius = this.grid.computeDistance(this.radiusXOne, this.radiusYOne, svgP.x, svgP.y);
this.initialCircle = new Circle(this.radiusXOne,this.radiusYOne, this.radius, 'Blue');
this.grid.svg.appendChild(this.initialCircle.getCircle());
}
}
/* function for handling the event when the mouse is released.*/
end(e)
{
var pt = this.grid.svg.createSVGPoint(), svgP;
pt.x = e.clientX;
pt.y = e.clientY;
svgP = pt.matrixTransform(this.grid.svg.getScreenCTM().inverse());
this.radiusXTwo = svgP.x;
this.radiusYTwo = svgP.y;
this.createCircle();
//setup for new circles
this.moved = false;
this.initialCircle = new Circle(0,0, 0, 'Blue');
this.grid.svg.appendChild(this.initialCircle.getCircle());
}
/* this function is the entry points for functions that creates the boundary circles.*/
createCircle()
{
this.updateDots();
this.updateSvg();
}
/** This function uses the Mid-Point circle algorithm to find the dots for making a circle. The algorithm first
finds the first point on the circumference using the radius. Then determines the next possible move
by evaluating the midpoints of the next possible moves.
*/
updateDots()
{
var centerCoordinates = this.grid.findClosest(this.radiusXOne, this.radiusYOne);
var firstCoordinates = this.grid.findClosest(this.radiusXOne, this.radiusYOne + this.radius);
var arrRadius = Math.abs(firstCoordinates[1] - centerCoordinates[1]);
var d = (5 - (arrRadius * 4))/4;
var x = 0;
var y = arrRadius;
//Builds the circle
do {
if(this.iteratorCheck(centerCoordinates[0]+x,centerCoordinates[1]+y))
{
this.green_indexes.push([centerCoordinates[0]+x,centerCoordinates[1]+y]);
if(!this.findGreenIndex(centerCoordinates[0]+x,centerCoordinates[1]+y, this.permanent_green_indexes))
{
this.permanent_green_indexes.push([centerCoordinates[0]+x,centerCoordinates[1]+y]);
}
}
if(this.iteratorCheck(centerCoordinates[0]+x,centerCoordinates[1]-y))
{
this.green_indexes.push([centerCoordinates[0]+x,centerCoordinates[1]-y])
if(!this.findGreenIndex(centerCoordinates[0]+x,centerCoordinates[1]+y, this.permanent_green_indexes))
{
this.permanent_green_indexes.push([centerCoordinates[0]+x,centerCoordinates[1]-y]);
}
}
if(this.iteratorCheck(centerCoordinates[0]-x,centerCoordinates[1]+y))
{
this.green_indexes.push([centerCoordinates[0]-x,centerCoordinates[1]+y])
if(!this.findGreenIndex(centerCoordinates[0]-x,centerCoordinates[1]+y, this.permanent_green_indexes))
{
this.permanent_green_indexes.push([centerCoordinates[0]-x,centerCoordinates[1]+y]);
}
}
if(this.iteratorCheck(centerCoordinates[0]-x,centerCoordinates[1]-y))
{
this.green_indexes.push([centerCoordinates[0]-x,centerCoordinates[1]-y])
if(!this.findGreenIndex(centerCoordinates[0]-x,centerCoordinates[1]-y, this.permanent_green_indexes))
{
this.permanent_green_indexes.push([centerCoordinates[0]-x,centerCoordinates[1]-y]);
}
}
if(this.iteratorCheck(centerCoordinates[0]+y,centerCoordinates[1]+x))
{
this.green_indexes.push([centerCoordinates[0]+y,centerCoordinates[1]+x])
if(!this.findGreenIndex(centerCoordinates[0]+y,centerCoordinates[1]+x, this.permanent_green_indexes))
{
this.permanent_green_indexes.push([centerCoordinates[0]+y,centerCoordinates[1]+x]);
}
}
if(this.iteratorCheck(centerCoordinates[0]+y,centerCoordinates[1]-x))
{
this.green_indexes.push([centerCoordinates[0]+y,centerCoordinates[1]-x])
if(!this.findGreenIndex(centerCoordinates[0]+y,centerCoordinates[1]-x, this.permanent_green_indexes))
{
this.permanent_green_indexes.push([centerCoordinates[0]+y,centerCoordinates[1]-x]);
}
}
if(this.iteratorCheck(centerCoordinates[0]-y,centerCoordinates[1]+x))
{
this.green_indexes.push([centerCoordinates[0]-y,centerCoordinates[1]+x])
if(!this.findGreenIndex(centerCoordinates[0]-y,centerCoordinates[1]+x, this.permanent_green_indexes))
{
this.permanent_green_indexes.push([centerCoordinates[0]-y,centerCoordinates[1]+x]);
}
}
if(this.iteratorCheck(centerCoordinates[0]-y,centerCoordinates[1]-x))
{
this.green_indexes.push([centerCoordinates[0]-y,centerCoordinates[1]-x])
if(!this.findGreenIndex(centerCoordinates[0]-y,centerCoordinates[1]-x, this.permanent_green_indexes))
{
this.permanent_green_indexes.push([centerCoordinates[0]-y,centerCoordinates[1]-x]);
}
}
if (d < 0) {
d += 2*x + 1;
} else {
d += 2*(x - y) + 1;
y--;
}
x++;
} while (x <= y);
//Find the shortest and farthest distance
var shortest = 500;
var farthest = -1;
for(var i = 0 ; i<this.green_indexes.length; i++)
{
var distance = this.grid.computeDistance(this.grid.squares[centerCoordinates[0]][centerCoordinates[1]].xcoordinate, this.grid.squares[centerCoordinates[0]][centerCoordinates[1]].ycoordinate, this.grid.squares[this.green_indexes[i][0]][this.green_indexes[i][1]].xcoordinate, this.grid.squares[this.green_indexes[i][0]][this.green_indexes[i][1]].ycoordinate);
if(distance > farthest)
{
farthest = distance;
}
if (distance < shortest)
{
shortest = distance;
}
}
//calibrating to avoid the circles to overlap
if (farthest - this.radius < 5 )
{
farthest = this.radius + 3;
}
this.farthestCircle = new Circle(this.radiusXOne, this.radiusYOne, farthest , 'red');
this.grid.svg.appendChild(this.farthestCircle.getCircle());
if (this.radius - shortest < 5 )
{
shortest = this.radius - 5;
}
this.shortestCircle = new Circle(this.radiusXOne, this.radiusYOne, shortest, 'red');
this.grid.svg.appendChild(this.shortestCircle.getCircle());
}
/** This function checks whether a iterator is valid or not
@param {Number} itOne: x coordinate from the mouse click event
@param {Number} itTwo: y coordinate from the mouse click event
@return {Boolean} true if the iterator is in a desired range false otherwise
*/
iteratorCheck (itOne, itTwo)
{
if(itOne >= 0 && itOne < 20 && itTwo >= 0 && itTwo <20)
{
return true;
}else
{
return false;
}
}
/** This function removes all the squares from the grid and updates them with appropriate coloring after constructing the circle */
updateSvg()
{
this.updatedSquares = new Array(20);
for (var i=0; i< 20; i++)
{
this.updatedSquares[i] = new Array(20);
}
for (var i=0; i< 20; i++)
{
for (var j=0; j< 20; j++)
{
this.grid.svg.removeChild(this.grid.squares[i][j].getSquare());
}
}
var indexX = 0;
var indexY = 0;
for (var i = 0; i < this.grid.height;)
{
for(var j=0; j < this.grid.width; )
{
if(this.findGreenIndex(indexX,indexY, this.permanent_green_indexes))
{
this.updatedSquares[indexX][indexY] = new Square(i, j, 'Blue');
}else
{
this.updatedSquares[indexX][indexY] = new Square(i, j, 'Grey');
}
indexY= indexY+1;
j=j+20;
}
i = i+20;
indexX = indexX+1;
indexY = 0;
}
for (var i=0; i< 20; i++)
{
for (var j=0; j< 20; j++)
{
this.grid.svg.appendChild(this.updatedSquares[i][j].getSquare());
}
}
this.grid.squares = this.updatedSquares;
this.green_indexes = []
}
/** This function is a lookup function to see whether the given indices are blue grids( grids that are part of the circle )
@param {Number} x1: x grid index
@param {Number} y1: y grid index
@return {Boolean} return true if the given indices are part of the blue grids( grids that are part of the circle) else returns false
*/
findGreenIndex(indexX, indexY, array)
{
for(var i = 0 ; i<array.length; i++)
{
if(indexX == array[i][0] && indexY == array[i][1])
{
return true;
}
}
return false;
}
}