-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyMap2D.java
More file actions
421 lines (384 loc) · 19.2 KB
/
MyMap2D.java
File metadata and controls
421 lines (384 loc) · 19.2 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
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
package Exe.EX3;
import java.awt.Color;
import java.util.Iterator;
/**
* This class implements the Map2D interface.
* You should change (implement) this class as part of Ex3.
*
* @author
* ID1: 326648532
* ID2: 214266819
* */
public class MyMap2D implements Map2D{
private static final int WIDTH = 0;
static Point2D FirstPoint;
private int[][] _map;
private String[] args;
public MyMap2D(int w, int h) {init(w,h);}
public MyMap2D(int size) {this(size,size);}
public MyMap2D(int[][] data) {
this(data.length, data[0].length);
init(data);
}
@Override
public void init(int w, int h) {
_map = new int[w][h];
}
@Override
public void init(int[][] arr) {
init(arr.length,arr[0].length);
for(int x = 0;x<this.getWidth()&& x<arr.length;x++) {
for(int y=0;y<this.getHeight()&& y<arr[0].length;y++) {
this.setPixel(x, y, arr[x][y]);
}
}
}
@Override
public int getWidth() {return _map.length;}
@Override
public int getHeight() {return _map[0].length;}
@Override
public int getPixel(int x, int y) { return _map[x][y];}
@Override
public int getPixel(Point2D p) {
return this.getPixel(p.ix(),p.iy());
}
public void setPixel(int x, int y, int v) {_map[x][y] = v;}
public void setPixel(Point2D p, int v) {
setPixel(p.ix(), p.iy(), v);
}
public void drawSegmentLow(int x0,int y0,int x1, int y1, int v) {
Point2D p1 = new Point2D(x0,y0);//Setting a new point
Point2D p2 = new Point2D(x1,y1);//Setting a new point
double dx=p2.ix()-p1.ix();//The X value at one point is less than the X value at a second point
double dy=p2.iy()-p1.iy();//The Y value at one point is less than the Y value at a second point
double yi = 1; // The Y value at place [i] is being 1
if(dy<0) // if loop that means if the y value is under 0 will get into the loop
{
yi=-1; // placement
dy=-dy; // placement
}
double D = 2*dy - dx; // variable definition
int y=p1.iy(); // variable definition
for(int x=p1.ix();x<p2.ix();x++) // for loop Starting from the X value of one point up to the X value at point I
{
this.setPixel(x,y,v); // Paints the point with a certain color
if(D>0) { // if loop that check if the variable [D] bigger than 0
y=(int)(y+yi); // variable definition and turns it into INT
D = D+(2*(dy-dx)); // variable definition
} else {D=D+2*dy;} // Defining a variable and another 2 times the Y value
}
}
public void drawSegmentHigh(int x0,int y0,int x1, int y1, int v) {
Point2D p1 = new Point2D(x0,y0); // variable definition
Point2D p2 = new Point2D(x1,y1); // variable definition
double dx=p2.ix()-p1.ix(); // The X value at one point minus the X value at a second point
double dy=p2.iy()-p1.iy(); // The Y value at one point minus the Y value at a second point
double xi = 1; // variable definition
if(dx<0) // IF loop that check if the value at x lowest then 0
{
xi=-1;//placement
dx=-dx; // placement
}
double D = 2*dx - dy; // variable definition
int x=p1.ix(); // variable definition
for(int y=p1.iy();y<p2.iy();y++) // for loop that run from the Y value of one point to another point
{
this.setPixel(x,y,v); // Draws a point on the map
if(D>0) { // IF loop that check if D is bigger 0
x=(int)(x+xi); // variable definition
D = D + (2*(dx-dy)); // variable definition
}
else {D=D+2*dx;} //Defining a variable and another 2 times the Y value
}
}
@Override
public void drawSegment(Point2D p1, Point2D p2, int v) {
if(Math.abs(p2.iy()- p1.iy())<Math.abs(p2.ix()- p1.ix())) { // IF loop Checks if in absolute value the X value at one point minus the second point is greater in absolute value
//the second point is greater in absolute value then the Y value at one point minus the second
if(p1.ix()>p2.ix()) { // IF loop that check if the X value of one point biggest than other point
drawSegmentLow(p2.ix(),p2.iy(),p1.ix(),p1.iy(),v); // Using a helper function
} else{
drawSegmentLow(p1.ix(),p1.iy(),p2.ix(),p2.iy(),v); // else Using a helper function
}
} else if(p1.iy()>p2.iy()){ // else if the y value of one point is biggest than other point
drawSegmentHigh(p2.ix(),p2.iy(),p1.ix(),p1.iy(),v); // Using a helper function
}else {
drawSegmentHigh(p1.ix(),p1.iy(),p2.ix(),p2.iy(),v); // else Using a helper function
}
this.setPixel(p1,v); // placement
this.setPixel(p2,v); // placement
}
@Override
public void drawRect(Point2D p1, Point2D p2, int col) {
double xMax=Math.max(p1.ix(), p2.ix());//gets the maximum between the x-point of p1 and the x-point of p2
double xMin=Math.min(p1.ix(), p2.ix());//gets the minimum between the x-point of p1 and the x-point of p2
double yMax=Math.max(p1.iy(), p2.iy());//gets the maximum between the y-point of p1 and the y-point of p2
double yMin=Math.min(p1.iy(), p2.iy());//gets the minimum between the x-point of p1 and the x-point of p2
for(int x=0;x<this.getWidth();x++) // for loop that run from 0 to width of all map
{
for(int y=0;y<this.getHeight();y++) // for loop in for loop that run from 0 to high of all map
{
if(x>=xMin &&x<=xMax && y>=yMin && y<=yMax)// if loop that check
{
this.setPixel(x,y,col);
//The loop goes through all the elements in the array and checks whether the x-point and the y-point are within the range. If so, it paints the pixel with the new color
}
}
}
}
@Override
// A function creates a circle after 2 clicks on the map
public void drawCircle(Point2D p , double rad, int col) {
for (int x = 0; x < this.getWidth(); x++) { // for loop that runs from 0 to the width of all the map
for (int y = 0; y < this.getHeight(); y++) { // for loop in for loop that runs from 0 to the high of all the map
double t = Math.pow(p.ix()-x,2)+Math.pow(p.iy()-y,2);//
if (Math.sqrt(t) <= rad) {// if loop that check if root of T is lowest and worth to the radius
this.setPixel(x, y, col); // placement
}
}
}
}
static Point2D P;// Sets P to be the first point
@Override
public int fill(Point2D p, int new_v) {
int [][]map = new int[this.getWidth()][this.getHeight()];
int colorFill=this.getPixel(P);
for(int x=0;x<this.getWidth();x++){//for loop that run from the value X to the width of all map and if so advances the variable
for(int y=0;y<this.getHeight();y++) // for loop in for loop that runs from the value Y to the high of all map and if so advances the variable
{
if(this.getPixel(x,y)==colorFill)map[x][y]=-1; // if loop that check if the point in place of (x,y) is Worth to the color of point P will be the color white
if(this.getPixel(x,y)!=colorFill)map[x][y]=-2; // if loop that check if the point in place of (x,y) is not Worth to the color of point P will be the other color
}
}
map[p.ix()][p.iy()]=0; // point in place of value X and value Y will be 0
while(radius<this.getWidth()*this.getHeight()) //the maximal path
{
for(int x=0;x<this.getWidth();x++)// for loop that runs from 0 to the width of all the map if run add one to value of X
{
for(int y=0;y<this.getHeight();y++)// for loop in for loop that runs from 0 to the high of all the map if run add one to value of x
{
if(map[x][y]==radius) { // if loop point that check if point in the place of value X and y is worth to the radius
if(x+1>=0 && x+1<this.getWidth() && y>=0 && y<this.getHeight()) {if(map[x+1][y]==-1) {map[x+1][y]=radius+1;}}
//if loop point that check if point in the place of value X +1 and value of Y biggest or worth to 0 and will be in white color than add to the radius one
if(x-1>=0 && x-1<this.getWidth() && y>=0 && y<this.getHeight()) {if(map[x-1][y]==-1 ) {map[x-1][y]=radius+1;}}
//if loop point that check if point in the place of value X -1 and value of Y biggest or worth to 0 and will be in white color than add to the radius one
if(x>=0 && x<this.getWidth() && y-1>=0 && y-1<this.getHeight()){if(map[x][y-1]==-1 ) {map[x][y-1]=radius+1;}}
//if loop point that check if point in the place of value X worth or biggest to 0 and value of Y biggest or worth to 0 and will be in white color than add to the radius one
if(x>=0 && x<this.getWidth() && y+1>=0 && y+1<this.getHeight()) {if(map[x][y+1]==-1) {map[x][y+1]=radius+1;}}
//if loop point that check if point in the place of value X +1 and value of Y+1 biggest or worth to 0 and will be in white color than add to the radius one
}
}
}
radius++; // add to the radius one
}
int count=0; // placement
for(int x=0;x<this.getWidth();x++) // for loop that runs from 0 to all of width of all map
{
for(int y=0;y<this.getHeight();y++) // for loop in for loop that runs from 0 to the high of all the map
{
if(map[x][y]>=0 && this.getPixel(x, y)==colorFill) { // if loop that check if the point in the value of X and Y bigger than 0 and worth to the color
this.setPixel(x,y,new_v); // placement
count++; // if run in the loop add to count one
}
}
}
radius=0;//placement
return count;
}
@Override
public int fill(int x, int y, int new_v) {
int [][]map = new int[this.getWidth()][this.getHeight()];
int colorFill=this.getPixel(P);
for(int x1=0;x1<this.getWidth();x1++)// for loop that run from 0 to the width of all the map
{
for(int y1=0;y1<this.getHeight();y1++) // for loop in for loop that runs from 0 to the high of all the map
{
if(this.getPixel(x1,y1)==colorFill)map[x1][y1]=-1;//if loop that check if the point in the value of X and Y bigger than 0 and worth to the white color
if(this.getPixel(x1,y1)!=colorFill)map[x1][y1]=-2;//if loop that check if the point in the value of X and Y bigger than 0 and worth to the other color
}
}
map[x][y]=0;
while(radius<this.getWidth()*this.getHeight()) //the maximal path
{
for(int x1=0;x1<this.getWidth();x1++)// for loop that run from 0 to the width of all the map
{
for(int y1=0;y1<this.getHeight();y1++) // for loop in for loop that runs from 0 to the high of all the map
{
if(map[x1][y1]==radius) { // if loop that check if the point in the value of X and Y is worth to the radius
if(x1+1>=0 && x1+1<this.getWidth() && y1>=0 && y1<this.getHeight()) {if(map[x1+1][y1]==-1) {map[x1+1][y1]=radius+1;}}
//if loop point that check if point in the place of value X +1 and value of Y biggest or worth to 0 and will be in white color than add to the radius one
if(x1-1>=0 && x1-1<this.getWidth() && y1>=0 && y1<this.getHeight()) {if(map[x1-1][y1]==-1 ) {map[x1-1][y1]=radius+1;}}
//if loop point that check if point in the place of value X +1 and value of Y biggest or worth to 0 and will be in white color than add to the radius one
if(x1>=0 && x1<this.getWidth() && y1-1>=0 && y1-1<this.getHeight()){if(map[x1][y1-1]==-1 ) {map[x1][y1-1]=radius+1;}}
//if loop point that check if point in the place of value X +1 and value of Y biggest or worth to 0 and will be in white color than add to the radius one
if(x1>=0 && x1<this.getWidth() && y1+1>=0 && y1+1<this.getHeight()) {if(map[x1][y1+1]==-1) {map[x1][y1+1]=radius+1;}}
//if loop point that check if point in the place of value X +1 and value of Y biggest or worth to 0 and will be in white color than add to the radius one
}
}
}
radius++; // add to the radius one
}
int count=0; // placement
for(int x1=0;x1<this.getWidth();x1++) // for loop that run from 0 to the width of all the map
{
for(int y1=0;y1<this.getHeight();y1++) // for loop in for loop that runs from 0 to the high of all the map
{
if(map[x1][y1]>=0 && this.getPixel(x1, y1)==colorFill) { // if loop that check if the point in the value of X and Y bigger than 0 and worth to the color
this.setPixel(x1,y1,new_v); // placement
count++;
}
}
}
radius=0; //placement
return count;
}
public boolean distanceEqualOne(Point2D p1,Point2D p2) {
if(p1.iy()== p2.iy() && Math.max(p1.ix(),p2.ix())-Math.min(p1.ix(),p2.ix())==1)return true;
// if loop that check if the point in the value of X and Y bigger than 0 and worth to the color if yes return true
if(p1.ix()== p2.ix() && Math.max(p1.iy(),p2.iy())-Math.min(p1.iy(),p2.iy())==1)return true;
// if loop that check if the point in the value of X and Y bigger than 0 and worth to the color if yes return true
return false;
// if not return false
}
static int radius=0;
static Point2D PshortestPath;
@Override
public Point2D[] shortestPath(Point2D p1, Point2D p2) {
if(p1.ix()==p2.ix()&&p1.iy()==p2.iy())
// if loop that check if the point in the value of X and Y bigger than 0 and worth to the color if yes return true
{
Point2D [] shortestPathArray1 = new Point2D[1]; // placement
shortestPathArray1[0]=new Point2D(p2); // placement
return shortestPathArray1;
}
int [][]map = new int[this.getWidth()][this.getHeight()]; //placement
int colorShortestPath=this.getPixel(PshortestPath);
for(int x=0;x<this.getWidth();x++)// for loop that run from 0 to the width of all the map
{
for(int y=0;y<this.getHeight();y++)// for loop in for loop that runs from 0 to the high of all the map
{
if(this.getPixel(x,y)==colorShortestPath)map[x][y]=-1;
// if loop that check if the point in the value of X and Y bigger than 0 and worth to the white color
if(this.getPixel(x,y)!=colorShortestPath)map[x][y]=-2;
// if loop that check if the point in the value of X and Y bigger than 0 and worth to the other color
}
}
map[p2.ix()][p2.iy()]=-1; // // if loop that check if the point in the value of X and Y bigger than 0 and worth to the white color
map[p1.ix()][p1.iy()]=0; // if loop that check if the point in the value of X and Y bigger than 0 and worth to the other color
Point2D End = new Point2D(-1,-1); // placement
while((End.ix()==p2.ix()&& End.iy()==p2.iy())==false) //the maximal path
{
for(int x=0;x<this.getWidth();x++) // for loop that run from 0 to the width of all the map
{
for(int y=0;y<this.getHeight();y++) // for loop in for loop that runs from 0 to the high of all the map
{
End = new Point2D(x,y); // Setting up a new point
if(map[x][y]==radius) { // if the point of value X Y is worth to the radius
if(End.ix()==p2.ix()&& End.iy()==p2.iy()) {y=this.getHeight();x=this.getWidth();End=p2;}else {
// if loop that check if the point in the value of X and Y bigger than 0 and worth to the other color
if(x+1>=0 && x+1<this.getWidth() && y>=0 && y<this.getHeight()) {if(map[x+1][y]==-1) {map[x+1][y]=radius+1;}}
// if loop that check if the point in the value of X and Y bigger than 0 and worth to the other color
if(x-1>=0 && x-1<this.getWidth() && y>=0 && y<this.getHeight()) {if(map[x-1][y]==-1 ) {map[x-1][y]=radius+1;}}
// if loop that check if the point in the value of X and Y bigger than 0 and worth to the other color
if(x>=0 && x<this.getWidth() && y-1>=0 && y-1<this.getHeight()){if(map[x][y-1]==-1 ) {map[x][y-1]=radius+1;}}
// if loop that check if the point in the value of X and Y bigger than 0 and worth to the other color
if(x>=0 && x<this.getWidth() && y+1>=0 && y+1<this.getHeight()) {if(map[x][y+1]==-1) {map[x][y+1]=radius+1;}}
// if loop that check if the point in the value of X and Y bigger than 0 and worth to the other color
}
}
}
}
if(radius>this.getWidth()*this.getHeight()) {;return null;}//return null if It is not possible to pass a road between these two points
radius++;
}
Point2D [] shortestPathArray = new Point2D[radius]; //placement
shortestPathArray[0]=new Point2D(p2); // placement
for(int i=0;i<shortestPathArray.length;i++) // for loop that run from 0 to the length of short way if turn in than add to i one each time
{
for(int x=0;x<this.getWidth();x++) // for loop in for loop that runs from 0 to the width of all the map
{
for(int y=0;y<this.getHeight();y++) // for loop in for loop that runs from 0 to the high of all the map
{
Point2D a = new Point2D(x,y); // placement
if(map[x][y]==(radius-2) && distanceEqualOne(shortestPathArray[i],a)) { // if loop that check if the point in the value of X and Y bigger than 0 and worth to the other color
shortestPathArray[i+1]=new Point2D(x,y); // placement
if((radius-2)==0) {i=shortestPathArray.length;} // if loop that check if the point in the value of X and Y bigger than 0 and worth to the other color
y=this.getHeight(); // placement
x=this.getWidth(); // placement
}
}
}
radius--;
}
radius=0;
return shortestPathArray;
}
static int color;
@Override
public int shortestPathDist(Point2D p1, Point2D p2) {
Point2D [] shortestPathArray = shortestPath(p1,p2);
if(shortestPathArray==null)return 0;
//return null if It is not possible to pass a road between these two points
for(int i=0;i<shortestPathArray.length;i++) { //for loop that run from 0 to the length of short way if turn in than add to i one each time
this.setPixel(shortestPathArray[i],color);
}
return shortestPathArray.length;
}
@Override
public void nextGenGol() {
Point2D [] Add = new Point2D[8];
Add[0] = new Point2D(-1,0);
Add[1] = new Point2D(0,1);
Add[2] = new Point2D(0,-1);
Add[3] = new Point2D(1,1);
Add[4] = new Point2D(1,0);
Add[5] = new Point2D(1,-1);
Add[6] = new Point2D(-1,-1);
Add[7] = new Point2D(-1,1);
int count=0;
int [][]map = new int[getWidth()][getHeight()];
for(int x=0;x<this.getWidth();x++) //for loop that run from 0 to the length of short way if turn in than add to i one each time
{
for(int y=0;y<this.getHeight();y++)//for loop in for loop that run from 0 to the length of short way if turn in than add to i one each time
{
map[x][y]=WHITE; // placement in value of X Y the color white
}
}
for(int x=0;x<this.getWidth();x++) //for loop that run from 0 to the length of short way if turn in than add to i one each time
{
for(int y=0;y<this.getHeight();y++) //for loop in for loop that run from 0 to the length of short way if turn in than add to i one each time
{
Point2D p = new Point2D(x,y); // placement
for(int i=0;i<8;i++) //for loop that run from 0 to the length of short way if turn in than add to i one each time
{
if(x+Add[i].ix()>=0 && x+Add[i].ix()<this.getWidth() && y+Add[i].iy()>=0 && y+Add[i].iy()<this.getHeight()) {
//for loop that run from 0 to the length of short way if turn in than add to i one each time
Point2D pAdd = p.add(Add[i]); // placement
if(this.getPixel(pAdd)==BLACK) {count++;}// if loop that check if point in place is worth to the color black
}
}
if(this.getPixel(x,y)==WHITE && count==3) {map[x][y] = BLACK;}
// if loop that check if point in place of X AND y is worth to the color white and if count is worth to the number 3 than the point of place X and y than put the color black
if(this.getPixel(x,y)==BLACK && (count==2 || count==3)) {map[x][y] = BLACK;}
// if loop that check if point in place of X AND y is worth to the color black and if count is worth to the number 3 than the point of place X and y than put the color black
count=0;
}
}
for(int x=0;x<this.getWidth();x++)// for loop that run from o to the width of all the map
{
for(int y=0;y<this.getHeight();y++)//for loop in for loop that run from 0 to the length of high if turn in than add to y one each time
{
_map[x][y]=map[x][y];//placement
}
}
}
@Override
public void fill(int c) {
for(int x = 0;x<this.getWidth();x++) {
for(int y = 0;y<this.getHeight();y++) {
this.setPixel(x, y, c);
}
}
}
}