abrahimladha 9 yıl önce
işleme
404580b6c5
7 değiştirilmiş dosya ile 1671 ekleme ve 0 silme
  1. 679 0
      AI_PROJ1.java
  2. 639 0
      AI_PROJ1.java~
  3. 84 0
      Anchor.java
  4. 82 0
      Anchor.java~
  5. 94 0
      Node.java
  6. 92 0
      Node.java~
  7. 1 0
      README.md

+ 679 - 0
AI_PROJ1.java

@ -0,0 +1,679 @@
1
//Made by Abrahim Ladha
2
//HASHEMI AI PROJECT 1
3
import javafx.scene.Scene;
4
import java.util.*;
5
import javafx.application.Application;
6
import javafx.beans.property.*;
7
import javafx.beans.value.*;
8
import javafx.collections.*;
9
import javafx.event.EventHandler;
10
import javafx.event.ActionEvent;
11
import javafx.scene.*;
12
import javafx.scene.input.*;
13
import javafx.scene.paint.Color;
14
import javafx.scene.shape.*;
15
import javafx.stage.Stage;
16
import javafx.geometry.Point2D;
17
import javafx.scene.control.*;
18
import javafx.scene.control.Button;
19
import javafx.scene.layout.*;
20
import javafx.animation.*;
21
import javafx.util.*;
22
import javafx.scene.shape.Path;
23
/** Drag the anchors around to change a polygon's points. */
24
public class AI_PROJ1 extends Application {
25
26
  public static void main(String[] args) throws Exception { launch(args); }
27
    static Polygon vpoly1 = new Polygon();
28
    static Polygon vpoly2 = new Polygon();
29
    static Polygon vpoly3 = new Polygon();    
30
    static Shape megaunion;
31
    static Polygon poly1 = createFirstPolygon();
32
    static Polygon poly2 = createSecondPolygon();
33
    static Polygon poly3 = createThirdPolygon();
34
    static Polygon error1 = new Polygon();
35
    static Polygon error2 = new Polygon();
36
    static Polygon error3 = new Polygon();
37
    static double a = 30.0;
38
    static Polygon robit = drawRobit(a);
39
    static Polygon goal = new Polygon();
40
    static ArrayList<Polyline> visibles = new ArrayList<>();
41
    static Group root = new Group();
42
    static Pane possiblepaths = new Pane();
43
    static PriorityQueue<Node> openList;
44
    static HashSet<Node> closedList;
45
    HashMap<Node, Double> gVals = new HashMap<>();
46
    HashMap<Node, Double> fVals = new HashMap<>();
47
    static ArrayList<Node> allnodes = new ArrayList<>();
48
    static Polyline sline = new Polyline();
49
    Button calculatebutton = new Button("CALCULATE");
50
    Button plusbutton = new Button("+");
51
    Button minusbutton = new Button("-");
52
    Button resetbutton = new Button("RESET");
53
    HBox hb = new HBox();
54
    VBox vb = new VBox();
55
    Pane box = new Pane();
56
    static Path path = new Path();
57
    static PathTransition pt = new PathTransition();
58
    @Override public void start(final Stage stage) throws Exception {        
59
    goal.getPoints().setAll(700d, 700d, 700d, 670d, 670d, 670d, 670d, 700d);
60
    goal.setFill(Color.RED); 
61
    goal.setStroke(Color.BLACK);
62
    box.setPrefSize(700,700); 
63
    plusButton handler1 = new plusButton();
64
    plusbutton.setOnAction(handler1);
65
    minusButton handler2 = new minusButton();
66
    minusbutton.setOnAction(handler2);
67
    resetButton handler3 = new resetButton();
68
    resetbutton.setOnAction(handler3);
69
    calculateButton handler4 = new calculateButton();
70
    calculatebutton.setOnAction(handler4);
71
    hb.setHgrow(plusbutton, Priority.ALWAYS);
72
    hb.setHgrow(minusbutton, Priority.ALWAYS);
73
    hb.setHgrow(calculatebutton, Priority.ALWAYS);
74
    hb.setHgrow(resetbutton, Priority.ALWAYS);
75
    plusbutton.setMaxWidth(Double.MAX_VALUE);
76
    minusbutton.setMaxWidth(Double.MAX_VALUE);
77
    resetbutton.setMaxWidth(Double.MAX_VALUE);
78
    calculatebutton.setMaxWidth(Double.MAX_VALUE);
79
    hb.getChildren().addAll(plusbutton,minusbutton,resetbutton,calculatebutton);
80
    vpoly1.getPoints().setAll(getConvexHull(pointSorter(virtualToRealPolygon(poly1.getPoints(),a))));
81
    vpoly1.setStroke(Color.BLACK);
82
    vpoly1.setStrokeWidth(1);
83
    vpoly1.setFill(Color.TRANSPARENT);
84
    vpoly1.setVisible(false);
85
    vpoly2.getPoints().setAll(getConvexHull(pointSorter(virtualToRealPolygon(poly2.getPoints(),a))));
86
    vpoly2.setStroke(Color.BLACK);
87
    vpoly2.setStrokeWidth(1);
88
    vpoly2.setFill(Color.TRANSPARENT);
89
    vpoly2.setVisible(false);
90
    vpoly3.getPoints().setAll(getConvexHull(pointSorter(virtualToRealPolygon(poly3.getPoints(),a))));
91
    vpoly3.setStroke(Color.BLACK);
92
    vpoly3.setStrokeWidth(1);
93
    vpoly3.setFill(Color.TRANSPARENT);
94
    vpoly3.setVisible(false);
95
    error1.getPoints().setAll(getConvexHull(pointSorter(virtualToRealPolygon(poly1.getPoints(),a*0.99))));
96
    error2.getPoints().setAll(getConvexHull(pointSorter(virtualToRealPolygon(poly2.getPoints(),a*0.99))));
97
    error3.getPoints().setAll(getConvexHull(pointSorter(virtualToRealPolygon(poly3.getPoints(),a*0.99))));
98
    box.getChildren().addAll(vpoly1,vpoly2,vpoly3);
99
    box.getChildren().addAll(poly1,poly2,poly3);
100
    box.getChildren().addAll(robit,goal);
101
    box.getChildren().addAll(createControlAnchorsFor(poly1.getPoints()));
102
    box.getChildren().addAll(createControlAnchorsFor(poly2.getPoints()));
103
    box.getChildren().addAll(createControlAnchorsFor(poly3.getPoints()));
104
    box.getChildren().addAll(possiblepaths);
105
    vb.getChildren().addAll(box, hb);
106
    root.getChildren().addAll(vb);
107
    Scene scene1 = new Scene(root, 700, 725, Color.ALICEBLUE);
108
    scene1.setOnMouseDragged(new EventHandler<MouseEvent>() {
109
        @Override public void handle(MouseEvent mouseEvent) {
110
         vpoly1.getPoints().setAll(getConvexHull(pointSorter(virtualToRealPolygon(poly1.getPoints(),a))));
111
         vpoly2.getPoints().setAll(getConvexHull(pointSorter(virtualToRealPolygon(poly2.getPoints(),a))));
112
         vpoly3.getPoints().setAll(getConvexHull(pointSorter(virtualToRealPolygon(poly3.getPoints(),a))));
113
         error1.getPoints().setAll(getConvexHull(pointSorter(virtualToRealPolygon(poly1.getPoints(),a*0.99))));
114
         error2.getPoints().setAll(getConvexHull(pointSorter(virtualToRealPolygon(poly2.getPoints(),a*0.99))));
115
         error3.getPoints().setAll(getConvexHull(pointSorter(virtualToRealPolygon(poly3.getPoints(),a*0.99))));
116
        }
117
	});
118
    stage.setTitle("AI_PROJ1");
119
    stage.setScene(
120
    	scene1
121
    );
122
    stage.show();
123
  }
124
//plusbutton handler
125
class plusButton implements EventHandler<ActionEvent> {
126
    @Override
127
    public void handle(ActionEvent e){
128
      if(a < 75.0){
129
        a += 1.0;
130
        vpoly1.getPoints().setAll(getConvexHull(pointSorter(virtualToRealPolygon(poly1.getPoints(),a))));
131
        vpoly2.getPoints().setAll(getConvexHull(pointSorter(virtualToRealPolygon(poly2.getPoints(),a))));
132
        vpoly3.getPoints().setAll(getConvexHull(pointSorter(virtualToRealPolygon(poly3.getPoints(),a))));
133
        error1.getPoints().setAll(getConvexHull(pointSorter(virtualToRealPolygon(poly1.getPoints(),a*0.99))));
134
        error2.getPoints().setAll(getConvexHull(pointSorter(virtualToRealPolygon(poly2.getPoints(),a*0.99))));
135
        error3.getPoints().setAll(getConvexHull(pointSorter(virtualToRealPolygon(poly3.getPoints(),a*0.99))));       
136
        robit.getPoints().setAll(drawRobit(a).getPoints());
137
        }
138
    }
139
}
140
//minusbutton handler
141
class minusButton implements EventHandler<ActionEvent> {
142
    @Override 
143
     public void handle(ActionEvent e){
144
        if(a > 1.1){
145
        a -= 1.0;
146
        vpoly1.getPoints().setAll(getConvexHull(pointSorter(virtualToRealPolygon(poly1.getPoints(),a))));
147
        vpoly2.getPoints().setAll(getConvexHull(pointSorter(virtualToRealPolygon(poly2.getPoints(),a))));
148
        vpoly3.getPoints().setAll(getConvexHull(pointSorter(virtualToRealPolygon(poly3.getPoints(),a))));
149
        error1.getPoints().setAll(getConvexHull(pointSorter(virtualToRealPolygon(poly1.getPoints(),a*0.99))));
150
        error2.getPoints().setAll(getConvexHull(pointSorter(virtualToRealPolygon(poly2.getPoints(),a*0.99))));
151
        error3.getPoints().setAll(getConvexHull(pointSorter(virtualToRealPolygon(poly3.getPoints(),a*0.99))));
152
        robit.getPoints().setAll(drawRobit(a).getPoints());
153
        }
154
    }
155
}
156
//resetbutton handler
157
class resetButton implements EventHandler<ActionEvent> {
158
    @Override
159
    public void handle(ActionEvent e){
160
        visibles.clear();
161
        calculatebutton.setDisable(false);
162
        Pane pnew = new Pane();
163
        box = new VBox();
164
        a = 30.0;
165
        robit = drawRobit(a);
166
        poly1 = createFirstPolygon();
167
        poly2 = createSecondPolygon();
168
        poly3 = createThirdPolygon();
169
        box.getChildren().clear();
170
        root.getChildren().clear();
171
        hb.getChildren().clear();
172
        sline.getPoints().clear();
173
        possiblepaths.getChildren().clear();
174
        hb.getChildren().addAll(plusbutton,minusbutton,resetbutton,calculatebutton);
175
        pnew.getChildren().addAll(poly1,poly2,poly3,robit,goal);
176
        vpoly1.getPoints().setAll(getConvexHull(pointSorter(virtualToRealPolygon(poly1.getPoints(),a))));
177
        vpoly2.getPoints().setAll(getConvexHull(pointSorter(virtualToRealPolygon(poly3.getPoints(),a)))); 
178
        vpoly3.getPoints().setAll(getConvexHull(pointSorter(virtualToRealPolygon(poly3.getPoints(),a)))); 
179
        vpoly1.setVisible(false);
180
        vpoly2.setVisible(false);
181
        vpoly3.setVisible(false);
182
        error1.getPoints().setAll(getConvexHull(pointSorter(virtualToRealPolygon(poly1.getPoints(),a*0.99))));
183
        error2.getPoints().setAll(getConvexHull(pointSorter(virtualToRealPolygon(poly2.getPoints(),a*0.99))));
184
        error3.getPoints().setAll(getConvexHull(pointSorter(virtualToRealPolygon(poly3.getPoints(),a*0.99))));
185
        pnew.getChildren().addAll(vpoly1,vpoly2);
186
        pnew.getChildren().addAll(vpoly3);
187
        pnew.getChildren().addAll(createControlAnchorsFor(poly1.getPoints()));
188
        pnew.getChildren().addAll(createControlAnchorsFor(poly2.getPoints()));
189
        pnew.getChildren().addAll(createControlAnchorsFor(poly3.getPoints()));
190
        box.getChildren().addAll(pnew,hb);
191
        root.getChildren().addAll(box);
192
    }
193
}
194
//calculatebutton handler
195
class calculateButton implements EventHandler<ActionEvent> {
196
    @Override
197
    public void handle(ActionEvent e){
198
        possiblepaths.getChildren().clear();
199
        calculatebutton.setDisable(true);
200
        vpoly1.setVisible(true);
201
        vpoly2.setVisible(true);
202
        vpoly3.setVisible(true);
203
        boolean intersecter = false;
204
        Shape s12 = Shape.intersect(poly1,poly2);
205
        root.getChildren().addAll(s12);
206
        
207
        Shape s13 = Shape.intersect(poly1,poly3);
208
        root.getChildren().addAll(s13);
209
210
        Shape s23 = Shape.intersect(poly2,poly3);
211
        root.getChildren().addAll(s23);
212
213
         
214
        if(s12.getLayoutBounds().getHeight() >= 0 || s12.getLayoutBounds().getWidth() >= 0){
215
            System.out.println("s12 fired");
216
            return;
217
        }
218
        else if(s13.getLayoutBounds().getHeight() >= 0 || s13.getLayoutBounds().getWidth() >= 0){
219
            System.out.println("s13 fired");
220
            return;
221
        }
222
        
223
        else if(s23.getLayoutBounds().getHeight() >= 0 || s23.getLayoutBounds().getWidth() >= 0){
224
            System.out.println("s23 fired");
225
            return;
226
        }
227
        //if(!intersecter)
228
            //return;
229
230
        
231
        for(int i = 0; i < vpoly2.getPoints().size(); i+=2){
232
            setVis(vpoly2.getPoints().get(i),vpoly2.getPoints().get(i+1),vpoly1);
233
            int size2 = vpoly2.getPoints().size();
234
            Node n = new Node(vpoly2.getPoints().get(i),vpoly2.getPoints().get(i+1));
235
            Node next = new Node(vpoly2.getPoints().get((i+2)%size2),vpoly2.getPoints().get((i+3)%size2));
236
            Node prev = new Node(vpoly2.getPoints().get((i+size2 -2)%size2),vpoly2.getPoints().get((i-1 + size2)%size2));
237
            if(!n.getNeighbors().contains(next))
238
                n.addNeighbor(next);
239
            if(!n.getNeighbors().contains(prev))
240
                n.addNeighbor(prev);
241
            allnodes.add(n);
242
        }
243
        for(int j = 0; j < vpoly3.getPoints().size(); j+=2){
244
            setVis(vpoly3.getPoints().get(j),vpoly3.getPoints().get(j+1),vpoly2);
245
            int size3 = vpoly3.getPoints().size();
246
            Node n = new Node(vpoly3.getPoints().get(j),vpoly3.getPoints().get(j+1));
247
            Node next = new Node(vpoly3.getPoints().get((j+2)%size3),vpoly3.getPoints().get((j+3)%size3));
248
            Node prev = new Node(vpoly3.getPoints().get((j+size3-2)%size3),vpoly3.getPoints().get((j+size3-1)%size3));
249
            if(!n.getNeighbors().contains(next))
250
                n.addNeighbor(next);
251
            if(!n.getNeighbors().contains(prev))
252
                n.addNeighbor(prev); 
253
            allnodes.add(n);
254
        }
255
        for(int k = 0; k < vpoly1.getPoints().size(); k+=2){
256
            setVis(vpoly1.getPoints().get(k),vpoly1.getPoints().get(k+1),vpoly3);
257
            int size1 = vpoly1.getPoints().size();
258
            Node n = new Node(vpoly1.getPoints().get(k),vpoly1.getPoints().get(k+1));
259
            Node next = new Node(vpoly1.getPoints().get((k+2)%size1),vpoly1.getPoints().get((k+3)%size1));
260
            Node prev = new Node(vpoly1.getPoints().get((k+size1-2)%size1),vpoly1.getPoints().get((k+size1-1)%size1));
261
            if(!n.getNeighbors().contains(next))
262
                n.addNeighbor(next);
263
            if(!n.getNeighbors().contains(prev))
264
                n.addNeighbor(prev);
265
            allnodes.add(n);
266
        }
267
        Node startnode = new Node(robit.getPoints().get(2),robit.getPoints().get(3));
268
        Node endnode = new Node(goal.getPoints().get(4),goal.getPoints().get(5));
269
        startnode.setNeighbors(whatsVisible(startnode));
270
        endnode.setNeighbors(whatsVisible(endnode));
271
        allnodes.add(startnode);
272
        allnodes.add(endnode);
273
        for(int w = 0; w < allnodes.size(); w++){
274
            allnodes.get(w).setNeighbors(whatsVisible(allnodes.get(w)));
275
        }
276
        for(Polyline path : visibles){
277
            path.setStroke(Color.LIGHTGRAY);   
278
            root.getChildren().add(path);
279
        }
280
        for(Node tempnode : allnodes)
281
            tempnode.setData(tempnode.getX() + "  " + tempnode.getY());
282
        traverse(endnode,startnode);
283
        sline.setStrokeWidth(4);
284
        root.getChildren().addAll(sline);
285
        playAnim();
286
    }
287
 }
288
//put all the animation stuff in one method
289
public void playAnim(){
290
    pt = new PathTransition();
291
    path = new Path();
292
    MoveTo mt = new MoveTo(robit.getPoints().get(2),robit.getPoints().get(3));
293
    path.getElements().add(mt);
294
    Polygon balance = new Polygon();
295
    balance.getPoints().addAll(
296
        robit.getPoints().get(2),robit.getPoints().get(3),
297
        robit.getPoints().get(2) - a,robit.getPoints().get(3),
298
        robit.getPoints().get(2) - 0.5*a, robit.getPoints().get(3) + 0.866*a
299
        );
300
    for(int i = 0; i < sline.getPoints().size(); i+=2){
301
        LineTo lt = new LineTo();
302
        lt.setX(sline.getPoints().get(i));
303
        lt.setY(sline.getPoints().get(i+1));
304
        path.getElements().add(lt);
305
    }
306
    pt.setDuration(Duration.seconds(8));
307
    pt.setPath(path);
308
    Group g = new Group();
309
    g.getChildren().addAll(balance,robit);
310
    balance.setFill(Color.TRANSPARENT);
311
    root.getChildren().addAll(g);
312
    pt.setNode(g);
313
    pt.setAutoReverse(true);
314
    robit.toFront();
315
    pt.play();
316
}
317
//literally the implementation of A*
318
public void traverse(Node begin, Node end) {
319
   openList = new PriorityQueue<Node>(1000, new fCompare());
320
    closedList = new HashSet<>();
321
     openList.clear(); 
322
    HashMap<Node,Node> cameFrom = new HashMap<Node,Node>();
323
    gVals.put(begin,0.0);
324
    openList.add(begin);
325
    while(!openList.isEmpty()) {
326
      Node current = openList.element();
327
        if (current.equals(end)) {
328
            begin.setParent(null);
329
            System.out.println("Goal Reached!");
330
            printPath(current);
331
            return;
332
        }
333
        closedList.add(openList.poll());
334
        HashSet<Node> neighbors = current.getNeighbors();
335
        for (Node neighbor : neighbors) {
336
            double gScore = gVals.get(current);
337
            double fScore = gScore + h(neighbor, end); 
338
            if(closedList.contains(neighbor)) {
339
                    gVals.put(neighbor, gScore);
340
                    fVals.put(neighbor, fScore);
341
                if(fScore >= fVals.get(neighbor)) {
342
                    continue;
343
                }
344
            }
345
            if (!openList.contains(neighbor) || fScore < fVals.get(neighbor)){
346
                neighbor.setParent(current);
347
                gVals.put(neighbor, gScore);
348
                fVals.put(neighbor, fScore);           
349
                if(!openList.contains(neighbor)) {
350
                    openList.add(neighbor);
351
                }
352
            }           
353
        }
354
    }
355
      System.out.println("FAIL");
356
}
357
public double h(Node node, Node goal) {
358
    double x = node.getX() - goal.getX();
359
    double y = node.getY()- goal.getY();
360
    return Math.sqrt(x*x +y*y);
361
}
362
public void printPath(Node node) {
363
    sline.getPoints().clear();
364
    sline.getPoints().add(node.getX());
365
    sline.getPoints().add(node.getY());
366
    while (node.getParent() != null) {
367
        node = node.getParent();
368
        sline.getPoints().add(node.getX());
369
        sline.getPoints().add(node.getY());
370
        System.out.println(node.getData());
371
    }
372
}
373
class fCompare implements Comparator<Node> {
374
    public int compare(Node o1, Node o2) {
375
        if(fVals.get(o1) < fVals.get(o2)) {
376
            return -1;
377
        }
378
        else if(fVals.get(o1) > fVals.get(o2)) {
379
        return 1;
380
        }
381
        else
382
            return 0;
383
    }
384
}
385
//For some vertex param, returns the hashset of whats visible
386
private static HashSet<Node> whatsVisible(Node vertex){
387
    HashSet<Node> childs = new HashSet<>();
388
    ArrayList<Polygon> triangles = new ArrayList<>();
389
    for(int i = 0; i < vpoly1.getPoints().size(); i+=2){
390
        Polygon triangle = new Polygon();
391
        triangle.getPoints().setAll(
392
                vertex.getX(),vertex.getY(),
393
                vpoly1.getPoints().get(i),
394
                vpoly1.getPoints().get(i+1),
395
                vpoly1.getPoints().get((i+2)%vpoly1.getPoints().size()),
396
                vpoly1.getPoints().get((i+3)%vpoly1.getPoints().size())
397
        );
398
        triangles.add(triangle);
399
    }
400
    for(int i = 0; i < vpoly2.getPoints().size(); i+=2){
401
       Polygon triangle = new Polygon();
402
       triangle.getPoints().setAll(
403
                vertex.getX(),vertex.getY(),
404
                vpoly2.getPoints().get(i),
405
                vpoly2.getPoints().get(i+1),
406
                vpoly2.getPoints().get((i+2)%vpoly2.getPoints().size()),
407
                vpoly2.getPoints().get((i+3)%vpoly2.getPoints().size())
408
        );
409
        triangles.add(triangle);
410
    }
411
    for(int i = 0; i < vpoly3.getPoints().size(); i+=2){
412
        Polygon triangle = new Polygon();
413
        triangle.getPoints().setAll(
414
                vertex.getX(),vertex.getY(),
415
                vpoly3.getPoints().get(i),
416
                vpoly3.getPoints().get(i+1),
417
                vpoly3.getPoints().get((i+2)%vpoly3.getPoints().size()),
418
                vpoly3.getPoints().get((i+3)%vpoly3.getPoints().size())
419
       );
420
       triangles.add(triangle);
421
    }
422
       Polygon triangle = new Polygon();
423
        triangle.getPoints().setAll(
424
                vertex.getX(),vertex.getY(),
425
                670d, 670d,
426
                670.0000001,670.0000001
427
                );
428
        triangles.add(triangle);
429
        triangle = new Polygon();
430
        triangle.getPoints().setAll(
431
                vertex.getX(),vertex.getY(),
432
                robit.getPoints().get(2),
433
                robit.getPoints().get(3),
434
                robit.getPoints().get(2) +0.0000001,
435
                robit.getPoints().get(3) + 0.0000001 
436
                );
437
        triangles.add(triangle);
438
        Shape totalError = Shape.union(error1,error2);
439
        totalError = Shape.union(totalError,error3);
440
    for(Polygon tri : triangles){   
441
        Shape inter = Shape.intersect(totalError,tri);
442
        if(inter.getLayoutBounds().getHeight() < 0 || inter.getLayoutBounds().getWidth() < 0){
443
            Node one = new Node(tri.getPoints().get(2),tri.getPoints().get(3));
444
            Node two = new Node(tri.getPoints().get(4),tri.getPoints().get(5));
445
            int dex1 = allnodes.indexOf(one);
446
            int dex2 = allnodes.indexOf(two);
447
            for(Node node : allnodes){
448
                if(one.equals(node)){
449
                    if(!childs.contains(node))
450
                        childs.add(node);
451
                }
452
                if(two.equals(node)){
453
                    if(!childs.contains(node))
454
                        childs.add(node);
455
                }
456
            }
457
        }
458
    }   
459
    return childs;
460
}
461
//adds cools lines to draw to visibles based on what doesnt intersect
462
private static void setVis(double x, double y, Polygon poly){
463
    for(int i = 0; i < poly.getPoints().size(); i+=2){
464
        Polygon triangle = new Polygon();
465
        triangle.getPoints().setAll(
466
                x,y,
467
                poly.getPoints().get(i),
468
                poly.getPoints().get(i+1),
469
                poly.getPoints().get((i+2)%poly.getPoints().size()),
470
                poly.getPoints().get((i+3)%poly.getPoints().size())
471
        );
472
        Shape totalError = Shape.union(error1,error2);
473
        totalError = Shape.union(error3,totalError);
474
        Shape inter = Shape.intersect(totalError,triangle);
475
        if(inter.getLayoutBounds().getHeight() <= 0 || inter.getLayoutBounds().getWidth() <= 0){
476
            Polyline one = new Polyline();
477
            Polyline two = new Polyline();
478
            one.getPoints().setAll(x,y,poly.getPoints().get(i),poly.getPoints().get(i+1));
479
            two.getPoints().setAll(x,y,poly.getPoints().get((i+2)%poly.getPoints().size()),
480
                    poly.getPoints().get((i+3)%poly.getPoints().size()));
481
            visibles.add(one);
482
            visibles.add(two);
483
        }
484
    }
485
}
486
//allows robit to change with the double value of a
487
private static Polygon drawRobit(double a) {
488
    double corner = 1.0;
489
   Polygon robit = new Polygon();
490
   robit.getPoints().setAll(
491
           (double)(corner + 0.5*a), corner,
492
           corner,(double)(corner + .866*a), 
493
           (double)(corner + a), (double)(corner + .866*a) 
494
           );
495
   robit.setStroke(Color.RED);
496
   robit.setFill(Color.YELLOW);
497
   return robit;
498
}
499
//creates first polygon
500
private static Polygon createFirstPolygon() {
501
    Polygon triangle = new Polygon();
502
    triangle.getPoints().setAll(
503
        280d, 110d,
504
        350d, 150d, 
505
        330d, 210d,
506
        300d, 280d,
507
        180d, 270d,
508
        170d, 220d,
509
        200d, 160d
510
    );
511
    triangle.setStroke(Color.BLUE);
512
    triangle.setStrokeWidth(4);
513
    triangle.setStrokeLineCap(StrokeLineCap.ROUND);
514
    triangle.setFill(Color.BLUE);
515
    return triangle;
516
}
517
//creates second polygon
518
private static Polygon createSecondPolygon() {
519
    Polygon triangle = new Polygon();
520
    triangle.getPoints().setAll(
521
        320d, 410d,
522
        390d, 440d,
523
        420d, 480d, 
524
        400d, 540d, 
525
        360d, 590d, 
526
        280d, 580d,
527
        250d, 520d,
528
        270d, 460d
529
    );
530
    triangle.setStroke(Color.BLUE);
531
    triangle.setStrokeWidth(4);
532
    triangle.setStrokeLineCap(StrokeLineCap.ROUND);
533
    triangle.setFill(Color.BLUE);
534
    return triangle;
535
}
536
//creates third polygon
537
  private static Polygon createThirdPolygon() {
538
    Polygon triangle = new Polygon();
539
    triangle.getPoints().setAll(
540
        560d, 200d,
541
        630d, 220d,
542
        650d, 300d,
543
        610d, 370d,
544
        580d, 400d,
545
        510d, 380d,
546
        460d, 340d,
547
        430d, 270d,
548
        490d, 210d
549
    );
550
    triangle.setStroke(Color.BLUE);
551
    triangle.setStrokeWidth(4);
552
    triangle.setStrokeLineCap(StrokeLineCap.ROUND);
553
    triangle.setFill(Color.BLUE);
554
    return triangle;  
555
}
556
//sorts points to comply with javafxs polygon renderer
557
private ObservableList<Double> pointSorter(ObservableList<Double> s){
558
    double centerX = 0, centerY = 0;
559
    for(int i = 0; i < s.size(); i+=2){
560
        centerX += s.get(i);
561
        centerY += s.get(i+1);
562
    }
563
    centerX /= (s.size()/2);
564
    centerY /= (s.size()/2);
565
    ArrayList<Double> angles = new ArrayList<>();
566
    for (int j = 0; j < s.size(); j+=2){
567
        angles.add(Math.atan2(s.get(j+1) - centerY , s.get(j) - centerX));
568
    }
569
    for(int k = angles.size() - 1; k >= 0; k--){
570
        for(int w = 1; w < k; w++){
571
            if(angles.get(w-1) > angles.get(w)){
572
                double temp = angles.get(w-1);
573
                angles.set(w-1,angles.get(w));
574
                angles.set(w, temp);
575
                double tempx = s.get(2*(w-1));
576
                double tempy = s.get(2*(w-1) + 1);
577
                s.set(2*(w-1), s.get(2*w));
578
                s.set(2*(w-1)+1, s.get((2*w) + 1));
579
                s.set(2*w, tempx);
580
                s.set((2*w)+1, tempy);
581
            }
582
        }
583
    }
584
    return s;
585
}
586
//convex implementation modified slightly from liangs book
587
private ObservableList<Double> getConvexHull(ObservableList<Double> s){
588
    Point2D[] myPoints = new Point2D[s.size()/2];
589
    for(int i = 0; i < myPoints.length; i++)
590
        myPoints[i] = new Point2D(s.get(2*i),s.get((2*i)+1));
591
      Point2D h0 = getRightMostLowestPoint(myPoints);
592
      ArrayList<Point2D> H = new ArrayList<Point2D>();
593
      H.add(h0);
594
      Point2D t0 = h0;
595
      while (true) {
596
        Point2D t1 = myPoints[0];
597
        for(int i = 1; i < myPoints.length; i++){
598
            double status = whichSide(t0.getX(), t0.getY(), t1.getX(), t1.getY(), myPoints[i].getX(), myPoints[i].getY());
599
            if(status > 0)
600
                t1 = myPoints[i];
601
            else  if (status == 0){
602
                if(distance(myPoints[i].getX(), myPoints[i].getY(), t0.getX(), t0.getY()) > distance(t1.getX(), t1.getY(), t0.getX(), t0.getY()))
603
                    t1 = myPoints[i];
604
            } 
605
        }
606
      if(t1.getX() == h0.getX() && t1.getY() == h0.getY())
607
          break;
608
      else {
609
        H.add(t1);
610
        t0 = t1;
611
      }
612
   }
613
    ObservableList<Double> pnts = FXCollections.observableArrayList();
614
    for(int j = 0; j < H.size(); j++){
615
        pnts.add(H.get(j).getX());
616
        pnts.add(H.get(j).getY());
617
    }
618
    return pnts;
619
  }
620
  public double whichSide(double x0, double y0, double x1, double y1, double x2, double y2){
621
    return (x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0);
622
  }
623
  public double distance(double x1, double y1, double x2, double y2) {
624
  return Math.sqrt((x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2));
625
  }
626
  static Point2D getRightMostLowestPoint(Point2D[] p){
627
    int rightMostIndex = 0;
628
    double rightMostX = p[0].getX();
629
    double rightMostY = p[0].getY();
630
    for(int i = 0; i < p.length; i++){
631
        if(rightMostY < p[i].getY()) {
632
            rightMostY = p[i].getY();
633
            rightMostX = p[i].getX();
634
            rightMostIndex = i;
635
        }
636
        else if (rightMostY == p[i].getY() && rightMostX < p[i].getX()){
637
            rightMostX = p[i].getX();
638
            rightMostIndex = i;
639
        }     
640
   }
641
    return p[rightMostIndex];
642
}
643
//creates virtual polygon per a value and polygons observable list values. used with convex hull
644
private ObservableList<Double> virtualToRealPolygon(final ObservableList<Double> rpoints, double a){
645
   ObservableList<Double> vpoints = FXCollections.observableArrayList();
646
    for(int i = 0; i < rpoints.size(); i+= 2){
647
        vpoints.add(rpoints.get(i));
648
        vpoints.add(rpoints.get(i+1));
649
    }
650
    for(int j = 0; j < rpoints.size(); j+= 2){
651
        vpoints.add(rpoints.get(j)-(a/2));
652
        vpoints.add(rpoints.get(j+1)+(.866*a));
653
        vpoints.add(rpoints.get(j) - a);
654
        vpoints.add(rpoints.get(j+1));
655
    }
656
    return vpoints;
657
}
658
//returns a list of draggable anchors for polygon specified at each vertice
659
private ObservableList<Anchor> createControlAnchorsFor(final ObservableList<Double> points) {
660
    ObservableList<Anchor> anchors = FXCollections.observableArrayList();
661
    for (int i = 0; i < points.size(); i+=2) {
662
      final int idx = i;
663
        DoubleProperty xProperty = new SimpleDoubleProperty(points.get(i));
664
        DoubleProperty yProperty = new SimpleDoubleProperty(points.get(i + 1));
665
        xProperty.addListener(new ChangeListener<Number>() {
666
            @Override public void changed(ObservableValue<? extends Number> ov, Number oldX, Number x) {
667
                points.set(idx, (double) x);
668
            }
669
        });
670
        yProperty.addListener(new ChangeListener<Number>() {
671
            @Override public void changed(ObservableValue<? extends Number> ov, Number oldY, Number y) {
672
                points.set(idx + 1, (double) y);
673
            }
674
        });
675
        anchors.add(new Anchor(Color.BLUE, xProperty, yProperty));
676
    }
677
    return anchors;
678
  }
679
}

+ 639 - 0
AI_PROJ1.java~

@ -0,0 +1,639 @@
1
import javafx.scene.Scene;
2
import java.util.*;
3
import javafx.application.Application;
4
import javafx.beans.property.*;
5
import javafx.beans.value.*;
6
import javafx.collections.*;
7
import javafx.event.EventHandler;
8
import javafx.event.ActionEvent;
9
import javafx.scene.*;
10
import javafx.scene.input.*;
11
import javafx.scene.paint.Color;
12
import javafx.scene.shape.*;
13
import javafx.stage.Stage;
14
import javafx.geometry.Point2D;
15
import javafx.scene.control.*;
16
import javafx.scene.control.Button;
17
import javafx.scene.layout.*;
18
import javafx.animation.*;
19
import javafx.util.*;
20
import javafx.scene.shape.Path;
21
/** Drag the anchors around to change a polygon's points. */
22
public class AI_PROJ1 extends Application {
23
24
  public static void main(String[] args) throws Exception { launch(args); }
25
    static Polygon vpoly1 = new Polygon();
26
    static Polygon vpoly2 = new Polygon();
27
    static Polygon vpoly3 = new Polygon();    
28
    static Polygon poly1 = createFirstPolygon();
29
    static Polygon poly2 = createSecondPolygon();
30
    static Polygon poly3 = createThirdPolygon();
31
    static Polygon error1 = new Polygon();
32
    static Polygon error2 = new Polygon();
33
    static Polygon error3 = new Polygon();
34
    static double a = 30.0;
35
    static Polygon robit = drawRobit(a);
36
    static Polygon goal = new Polygon();
37
    static ArrayList<Polyline> visibles = new ArrayList<>();
38
    static Group root = new Group();
39
    static Pane possiblepaths = new Pane();
40
    static PriorityQueue<Node> openList;
41
    static HashSet<Node> closedList;
42
    HashMap<Node, Double> gVals = new HashMap<>();
43
    HashMap<Node, Double> fVals = new HashMap<>();
44
    static ArrayList<Node> allnodes = new ArrayList<>();
45
    static Polyline sline = new Polyline();
46
    Button calculatebutton = new Button("CALCULATE");
47
    Button plusbutton = new Button("+");
48
    Button minusbutton = new Button("-");
49
    Button resetbutton = new Button("RESET");
50
    HBox hb = new HBox();
51
    VBox vb = new VBox();
52
    Pane box = new Pane();
53
    static Path path = new Path();
54
    static PathTransition pt = new PathTransition();
55
    @Override public void start(final Stage stage) throws Exception {        
56
    goal.getPoints().setAll(700d, 700d, 700d, 670d, 670d, 670d, 670d, 700d);
57
    goal.setFill(Color.RED); 
58
    goal.setStroke(Color.BLACK);
59
    box.setPrefSize(700,700); 
60
    plusButton handler1 = new plusButton();
61
    plusbutton.setOnAction(handler1);
62
    minusButton handler2 = new minusButton();
63
    minusbutton.setOnAction(handler2);
64
    resetButton handler3 = new resetButton();
65
    resetbutton.setOnAction(handler3);
66
    calculateButton handler4 = new calculateButton();
67
    calculatebutton.setOnAction(handler4);
68
    hb.setHgrow(plusbutton, Priority.ALWAYS);
69
    hb.setHgrow(minusbutton, Priority.ALWAYS);
70
    hb.setHgrow(calculatebutton, Priority.ALWAYS);
71
    hb.setHgrow(resetbutton, Priority.ALWAYS);
72
    plusbutton.setMaxWidth(Double.MAX_VALUE);
73
    minusbutton.setMaxWidth(Double.MAX_VALUE);
74
    resetbutton.setMaxWidth(Double.MAX_VALUE);
75
    calculatebutton.setMaxWidth(Double.MAX_VALUE);
76
    hb.getChildren().addAll(plusbutton,minusbutton,resetbutton,calculatebutton);
77
    vpoly1.getPoints().setAll(getConvexHull(pointSorter(virtualToRealPolygon(poly1.getPoints(),a))));
78
    vpoly1.setStroke(Color.BLACK);
79
    vpoly1.setStrokeWidth(1);
80
    vpoly1.setFill(Color.TRANSPARENT);
81
    vpoly2.getPoints().setAll(getConvexHull(pointSorter(virtualToRealPolygon(poly2.getPoints(),a))));
82
    vpoly2.setStroke(Color.BLACK);
83
    vpoly2.setStrokeWidth(1);
84
    vpoly2.setFill(Color.TRANSPARENT);
85
    vpoly3.getPoints().setAll(getConvexHull(pointSorter(virtualToRealPolygon(poly3.getPoints(),a))));
86
    vpoly3.setStroke(Color.BLACK);
87
    vpoly3.setStrokeWidth(1);
88
    vpoly3.setFill(Color.TRANSPARENT);
89
    error1.getPoints().setAll(getConvexHull(pointSorter(virtualToRealPolygon(poly1.getPoints(),a*0.99))));
90
    error2.getPoints().setAll(getConvexHull(pointSorter(virtualToRealPolygon(poly2.getPoints(),a*0.99))));
91
    error3.getPoints().setAll(getConvexHull(pointSorter(virtualToRealPolygon(poly3.getPoints(),a*0.99))));
92
    box.getChildren().addAll(vpoly1,vpoly2,vpoly3);
93
    box.getChildren().addAll(poly1,poly2,poly3);
94
    box.getChildren().addAll(robit,goal);
95
    box.getChildren().addAll(createControlAnchorsFor(poly1.getPoints()));
96
    box.getChildren().addAll(createControlAnchorsFor(poly2.getPoints()));
97
    box.getChildren().addAll(createControlAnchorsFor(poly3.getPoints()));
98
    box.getChildren().addAll(possiblepaths);
99
    vb.getChildren().addAll(box, hb);
100
    root.getChildren().addAll(vb);
101
    Scene scene1 = new Scene(root, 700, 725, Color.ALICEBLUE);
102
    scene1.setOnMouseDragged(new EventHandler<MouseEvent>() {
103
        @Override public void handle(MouseEvent mouseEvent) {
104
         vpoly1.getPoints().setAll(getConvexHull(pointSorter(virtualToRealPolygon(poly1.getPoints(),a))));
105
         vpoly2.getPoints().setAll(getConvexHull(pointSorter(virtualToRealPolygon(poly2.getPoints(),a))));
106
         vpoly3.getPoints().setAll(getConvexHull(pointSorter(virtualToRealPolygon(poly3.getPoints(),a))));
107
         error1.getPoints().setAll(getConvexHull(pointSorter(virtualToRealPolygon(poly1.getPoints(),a*0.99))));
108
         error2.getPoints().setAll(getConvexHull(pointSorter(virtualToRealPolygon(poly2.getPoints(),a*0.99))));
109
         error3.getPoints().setAll(getConvexHull(pointSorter(virtualToRealPolygon(poly3.getPoints(),a*0.99))));
110
        }
111
	});
112
    stage.setTitle("AI_PROJ1");
113
    stage.setScene(
114
    	scene1
115
    );
116
    stage.show();
117
  }
118
//plusbutton handler
119
class plusButton implements EventHandler<ActionEvent> {
120
    @Override
121
    public void handle(ActionEvent e){
122
      if(a < 75.0){
123
        a += 1.0;
124
        vpoly1.getPoints().setAll(getConvexHull(pointSorter(virtualToRealPolygon(poly1.getPoints(),a))));
125
        vpoly2.getPoints().setAll(getConvexHull(pointSorter(virtualToRealPolygon(poly2.getPoints(),a))));
126
        vpoly3.getPoints().setAll(getConvexHull(pointSorter(virtualToRealPolygon(poly3.getPoints(),a))));
127
        error1.getPoints().setAll(getConvexHull(pointSorter(virtualToRealPolygon(poly1.getPoints(),a*0.99))));
128
        error2.getPoints().setAll(getConvexHull(pointSorter(virtualToRealPolygon(poly2.getPoints(),a*0.99))));
129
        error3.getPoints().setAll(getConvexHull(pointSorter(virtualToRealPolygon(poly3.getPoints(),a*0.99))));       
130
        robit.getPoints().setAll(drawRobit(a).getPoints());
131
        }
132
    }
133
}
134
//minusbutton handler
135
class minusButton implements EventHandler<ActionEvent> {
136
    @Override 
137
     public void handle(ActionEvent e){
138
        if(a > 1.1){
139
        a -= 1.0;
140
        vpoly1.getPoints().setAll(getConvexHull(pointSorter(virtualToRealPolygon(poly1.getPoints(),a))));
141
        vpoly2.getPoints().setAll(getConvexHull(pointSorter(virtualToRealPolygon(poly2.getPoints(),a))));
142
        vpoly3.getPoints().setAll(getConvexHull(pointSorter(virtualToRealPolygon(poly3.getPoints(),a))));
143
        error1.getPoints().setAll(getConvexHull(pointSorter(virtualToRealPolygon(poly1.getPoints(),a*0.99))));
144
        error2.getPoints().setAll(getConvexHull(pointSorter(virtualToRealPolygon(poly2.getPoints(),a*0.99))));
145
        error3.getPoints().setAll(getConvexHull(pointSorter(virtualToRealPolygon(poly3.getPoints(),a*0.99))));
146
        robit.getPoints().setAll(drawRobit(a).getPoints());
147
        }
148
    }
149
}
150
//resetbutton handler
151
class resetButton implements EventHandler<ActionEvent> {
152
    @Override
153
    public void handle(ActionEvent e){
154
        visibles.clear();
155
        calculatebutton.setDisable(false);
156
        Pane pnew = new Pane();
157
        box = new VBox();
158
        a = 30.0;
159
        robit = drawRobit(a);
160
        poly1 = createFirstPolygon();
161
        poly2 = createSecondPolygon();
162
        poly3 = createThirdPolygon();
163
        box.getChildren().clear();
164
        root.getChildren().clear();
165
        hb.getChildren().clear();
166
        sline.getPoints().clear();
167
        possiblepaths.getChildren().clear();
168
        hb.getChildren().addAll(plusbutton,minusbutton,resetbutton,calculatebutton);
169
        pnew.getChildren().addAll(poly1,poly2,poly3,robit,goal);
170
        vpoly1.getPoints().setAll(getConvexHull(pointSorter(virtualToRealPolygon(poly1.getPoints(),a))));
171
        vpoly2.getPoints().setAll(getConvexHull(pointSorter(virtualToRealPolygon(poly3.getPoints(),a)))); 
172
        vpoly3.getPoints().setAll(getConvexHull(pointSorter(virtualToRealPolygon(poly3.getPoints(),a)))); 
173
        error1.getPoints().setAll(getConvexHull(pointSorter(virtualToRealPolygon(poly1.getPoints(),a*0.99))));
174
        error2.getPoints().setAll(getConvexHull(pointSorter(virtualToRealPolygon(poly2.getPoints(),a*0.99))));
175
        error3.getPoints().setAll(getConvexHull(pointSorter(virtualToRealPolygon(poly3.getPoints(),a*0.99))));
176
        pnew.getChildren().addAll(vpoly1,vpoly2);
177
        pnew.getChildren().addAll(vpoly3);
178
        pnew.getChildren().addAll(createControlAnchorsFor(poly1.getPoints()));
179
        pnew.getChildren().addAll(createControlAnchorsFor(poly2.getPoints()));
180
        pnew.getChildren().addAll(createControlAnchorsFor(poly3.getPoints()));
181
        box.getChildren().addAll(pnew,hb);
182
        root.getChildren().addAll(box);
183
    }
184
}
185
//calculatebutton handler
186
class calculateButton implements EventHandler<ActionEvent> {
187
    @Override
188
    public void handle(ActionEvent e){
189
        possiblepaths.getChildren().clear();
190
        calculatebutton.setDisable(true);
191
        for(int i = 0; i < vpoly2.getPoints().size(); i+=2){
192
            setVis(vpoly2.getPoints().get(i),vpoly2.getPoints().get(i+1),vpoly1);
193
            int size2 = vpoly2.getPoints().size();
194
            Node n = new Node(vpoly2.getPoints().get(i),vpoly2.getPoints().get(i+1));
195
            Node next = new Node(vpoly2.getPoints().get((i+2)%size2),vpoly2.getPoints().get((i+3)%size2));
196
            Node prev = new Node(vpoly2.getPoints().get((i+size2 -2)%size2),vpoly2.getPoints().get((i-1 + size2)%size2));
197
            if(!n.getNeighbors().contains(next))
198
                n.addNeighbor(next);
199
            if(!n.getNeighbors().contains(prev))
200
                n.addNeighbor(prev);
201
            allnodes.add(n);
202
        }
203
        for(int j = 0; j < vpoly3.getPoints().size(); j+=2){
204
            setVis(vpoly3.getPoints().get(j),vpoly3.getPoints().get(j+1),vpoly2);
205
            int size3 = vpoly3.getPoints().size();
206
            Node n = new Node(vpoly3.getPoints().get(j),vpoly3.getPoints().get(j+1));
207
            Node next = new Node(vpoly3.getPoints().get((j+2)%size3),vpoly3.getPoints().get((j+3)%size3));
208
            Node prev = new Node(vpoly3.getPoints().get((j+size3-2)%size3),vpoly3.getPoints().get((j+size3-1)%size3));
209
            if(!n.getNeighbors().contains(next))
210
                n.addNeighbor(next);
211
            if(!n.getNeighbors().contains(prev))
212
                n.addNeighbor(prev);
213
            allnodes.add(n);
214
        }
215
        for(int k = 0; k < vpoly1.getPoints().size(); k+=2){
216
            setVis(vpoly1.getPoints().get(k),vpoly1.getPoints().get(k+1),vpoly3);
217
            int size1 = vpoly1.getPoints().size();
218
            Node n = new Node(vpoly1.getPoints().get(k),vpoly1.getPoints().get(k+1));
219
            Node next = new Node(vpoly1.getPoints().get((k+2)%size1),vpoly1.getPoints().get((k+3)%size1));
220
            Node prev = new Node(vpoly1.getPoints().get((k+size1-2)%size1),vpoly1.getPoints().get((k+size1-1)%size1));
221
            if(!n.getNeighbors().contains(next))
222
                n.addNeighbor(next);
223
            if(!n.getNeighbors().contains(prev))
224
                n.addNeighbor(prev);
225
            allnodes.add(n);
226
        }
227
        Node startnode = new Node(robit.getPoints().get(2),robit.getPoints().get(3));
228
        Node endnode = new Node(goal.getPoints().get(4),goal.getPoints().get(5));
229
        startnode.setNeighbors(whatsVisible(startnode));
230
        endnode.setNeighbors(whatsVisible(endnode));
231
        allnodes.add(startnode);
232
        allnodes.add(endnode);
233
        for(int w = 0; w < allnodes.size(); w++){
234
            allnodes.get(w).setNeighbors(whatsVisible(allnodes.get(w)));
235
        }
236
        for(Polyline path : visibles){
237
            path.setStroke(Color.LIGHTGRAY);   
238
            root.getChildren().add(path);
239
        }
240
        for(Node tempnode : allnodes)
241
            tempnode.setData(tempnode.getX() + "  " + tempnode.getY());
242
        traverse(endnode,startnode);
243
        sline.setStrokeWidth(4);
244
        root.getChildren().addAll(sline);
245
        playAnim();
246
    }
247
 }
248
//put all the animation stuff in one method
249
public void playAnim(){
250
    pt = new PathTransition();
251
    path = new Path();
252
    MoveTo mt = new MoveTo(robit.getPoints().get(2),robit.getPoints().get(3));
253
    path.getElements().add(mt);
254
    Polygon balance = new Polygon();
255
    balance.getPoints().addAll(
256
        robit.getPoints().get(2),robit.getPoints().get(3),
257
        robit.getPoints().get(2) - a,robit.getPoints().get(3),
258
        robit.getPoints().get(2) - 0.5*a, robit.getPoints().get(3) + 0.866*a
259
        );
260
    for(int i = 0; i < sline.getPoints().size(); i+=2){
261
        LineTo lt = new LineTo();
262
        lt.setX(sline.getPoints().get(i));
263
        lt.setY(sline.getPoints().get(i+1));
264
        path.getElements().add(lt);
265
    }
266
    pt.setDuration(Duration.seconds(8));
267
    pt.setPath(path);
268
    Group g = new Group();
269
    g.getChildren().addAll(balance,robit);
270
    balance.setFill(Color.TRANSPARENT);
271
    root.getChildren().addAll(g);
272
    pt.setNode(g);
273
    pt.setAutoReverse(true);
274
    robit.toFront();
275
    pt.play();
276
}
277
//literally the implementation of A*
278
public void traverse(Node begin, Node end) {
279
   openList = new PriorityQueue<Node>(1000, new fCompare());
280
    closedList = new HashSet<>();
281
     openList.clear(); 
282
    HashMap<Node,Node> cameFrom = new HashMap<Node,Node>();
283
    gVals.put(begin,0.0);
284
    openList.add(begin);
285
    while(!openList.isEmpty()) {
286
      Node current = openList.element();
287
        if (current.equals(end)) {
288
            begin.setParent(null);
289
            System.out.println("Goal Reached!");
290
            printPath(current);
291
            return;
292
        }
293
        closedList.add(openList.poll());
294
        HashSet<Node> neighbors = current.getNeighbors();
295
        for (Node neighbor : neighbors) {
296
            double gScore = gVals.get(current);
297
            double fScore = gScore + h(neighbor, end); 
298
            if(closedList.contains(neighbor)) {
299
                    gVals.put(neighbor, gScore);
300
                    fVals.put(neighbor, fScore);
301
                if(fScore >= fVals.get(neighbor)) {
302
                    continue;
303
                }
304
            }
305
            if (!openList.contains(neighbor) || fScore < fVals.get(neighbor)){
306
                neighbor.setParent(current);
307
                gVals.put(neighbor, gScore);
308
                fVals.put(neighbor, fScore);           
309
                if(!openList.contains(neighbor)) {
310
                    openList.add(neighbor);
311
                }
312
            }           
313
        }
314
    }
315
      System.out.println("FAIL");
316
}
317
public double h(Node node, Node goal) {
318
    double x = node.getX() - goal.getX();
319
    double y = node.getY()- goal.getY();
320
    return Math.sqrt(x*x +y*y);
321
}
322
public void printPath(Node node) {
323
    sline.getPoints().clear();
324
    sline.getPoints().add(node.getX());
325
    sline.getPoints().add(node.getY());
326
    while (node.getParent() != null) {
327
        node = node.getParent();
328
        sline.getPoints().add(node.getX());
329
        sline.getPoints().add(node.getY());
330
        System.out.println(node.getData());
331
    }
332
}
333
class fCompare implements Comparator<Node> {
334
    public int compare(Node o1, Node o2) {
335
        if(fVals.get(o1) < fVals.get(o2)) {
336
            return -1;
337
        }
338
        else if(fVals.get(o1) > fVals.get(o2)) {
339
        return 1;
340
        }
341
        else
342
            return 0;
343
    }
344
}
345
//For some vertex param, returns the hashset of whats visible
346
private static HashSet<Node> whatsVisible(Node vertex){
347
    HashSet<Node> childs = new HashSet<>();
348
    ArrayList<Polygon> triangles = new ArrayList<>();
349
    for(int i = 0; i < vpoly1.getPoints().size(); i+=2){
350
        Polygon triangle = new Polygon();
351
        triangle.getPoints().setAll(
352
                vertex.getX(),vertex.getY(),
353
                vpoly1.getPoints().get(i),
354
                vpoly1.getPoints().get(i+1),
355
                vpoly1.getPoints().get((i+2)%vpoly1.getPoints().size()),
356
                vpoly1.getPoints().get((i+3)%vpoly1.getPoints().size())
357
        );
358
        triangles.add(triangle);
359
    }
360
    for(int i = 0; i < vpoly2.getPoints().size(); i+=2){
361
       Polygon triangle = new Polygon();
362
       triangle.getPoints().setAll(
363
                vertex.getX(),vertex.getY(),
364
                vpoly2.getPoints().get(i),
365
                vpoly2.getPoints().get(i+1),
366
                vpoly2.getPoints().get((i+2)%vpoly2.getPoints().size()),
367
                vpoly2.getPoints().get((i+3)%vpoly2.getPoints().size())
368
        );
369
        triangles.add(triangle);
370
    }
371
    for(int i = 0; i < vpoly3.getPoints().size(); i+=2){
372
        Polygon triangle = new Polygon();
373
        triangle.getPoints().setAll(
374
                vertex.getX(),vertex.getY(),
375
                vpoly3.getPoints().get(i),
376
                vpoly3.getPoints().get(i+1),
377
                vpoly3.getPoints().get((i+2)%vpoly3.getPoints().size()),
378
                vpoly3.getPoints().get((i+3)%vpoly3.getPoints().size())
379
       );
380
       triangles.add(triangle);
381
    }
382
       Polygon triangle = new Polygon();
383
        triangle.getPoints().setAll(
384
                vertex.getX(),vertex.getY(),
385
                670d, 670d,
386
                670.0000001,670.0000001
387
                );
388
        triangles.add(triangle);
389
        triangle = new Polygon();
390
        triangle.getPoints().setAll(
391
                vertex.getX(),vertex.getY(),
392
                robit.getPoints().get(2),
393
                robit.getPoints().get(3),
394
                robit.getPoints().get(2) +0.0000001,
395
                robit.getPoints().get(3) + 0.0000001 
396
                );
397
        triangles.add(triangle);
398
        Shape totalError = Shape.union(error1,error2);
399
        totalError = Shape.union(totalError,error3);
400
    for(Polygon tri : triangles){   
401
        Shape inter = Shape.intersect(totalError,tri);
402
        if(inter.getLayoutBounds().getHeight() < 0 || inter.getLayoutBounds().getWidth() < 0){
403
            Node one = new Node(tri.getPoints().get(2),tri.getPoints().get(3));
404
            Node two = new Node(tri.getPoints().get(4),tri.getPoints().get(5));
405
            int dex1 = allnodes.indexOf(one);
406
            int dex2 = allnodes.indexOf(two);
407
            for(Node node : allnodes){
408
                if(one.equals(node)){
409
                    if(!childs.contains(node))
410
                        childs.add(node);
411
                }
412
                if(two.equals(node)){
413
                    if(!childs.contains(node))
414
                        childs.add(node);
415
                }
416
            }
417
        }
418
    }   
419
    return childs;
420
}
421
//adds cools lines to draw to visibles based on what doesnt intersect
422
private static void setVis(double x, double y, Polygon poly){
423
    for(int i = 0; i < poly.getPoints().size(); i+=2){
424
        Polygon triangle = new Polygon();
425
        triangle.getPoints().setAll(
426
                x,y,
427
                poly.getPoints().get(i),
428
                poly.getPoints().get(i+1),
429
                poly.getPoints().get((i+2)%poly.getPoints().size()),
430
                poly.getPoints().get((i+3)%poly.getPoints().size())
431
        );
432
        Shape totalError = Shape.union(error1,error2);
433
        totalError = Shape.union(error3,totalError);
434
        Shape inter = Shape.intersect(totalError,triangle);
435
        if(inter.getLayoutBounds().getHeight() <= 0 || inter.getLayoutBounds().getWidth() <= 0){
436
            Polyline one = new Polyline();
437
            Polyline two = new Polyline();
438
            one.getPoints().setAll(x,y,poly.getPoints().get(i),poly.getPoints().get(i+1));
439
            two.getPoints().setAll(x,y,poly.getPoints().get((i+2)%poly.getPoints().size()),
440
                    poly.getPoints().get((i+3)%poly.getPoints().size()));
441
            visibles.add(one);
442
            visibles.add(two);
443
        }
444
    }
445
}
446
//allows robit to change with the double value of a
447
private static Polygon drawRobit(double a) {
448
    double corner = 1.0;
449
   Polygon robit = new Polygon();
450
   robit.getPoints().setAll(
451
           (double)(corner + 0.5*a), corner,
452
           corner,(double)(corner + .866*a), 
453
           (double)(corner + a), (double)(corner + .866*a) 
454
           );
455
   robit.setStroke(Color.RED);
456
   robit.setFill(Color.YELLOW);
457
   return robit;
458
}
459
//creates first polygon
460
private static Polygon createFirstPolygon() {
461
    Polygon triangle = new Polygon();
462
    triangle.getPoints().setAll(
463
        280d, 110d,
464
        350d, 150d, 
465
        330d, 210d,
466
        300d, 280d,
467
        180d, 270d,
468
        170d, 220d,
469
        200d, 160d
470
    );
471
    triangle.setStroke(Color.BLUE);
472
    triangle.setStrokeWidth(4);
473
    triangle.setStrokeLineCap(StrokeLineCap.ROUND);
474
    triangle.setFill(Color.BLUE);
475
    return triangle;
476
}
477
//creates second polygon
478
private static Polygon createSecondPolygon() {
479
    Polygon triangle = new Polygon();
480
    triangle.getPoints().setAll(
481
        320d, 410d,
482
        390d, 440d,
483
        420d, 480d, 
484
        400d, 540d, 
485
        360d, 590d, 
486
        280d, 580d,
487
        250d, 520d,
488
        270d, 460d
489
    );
490
    triangle.setStroke(Color.BLUE);
491
    triangle.setStrokeWidth(4);
492
    triangle.setStrokeLineCap(StrokeLineCap.ROUND);
493
    triangle.setFill(Color.BLUE);
494
    return triangle;
495
}
496
//creates third polygon
497
  private static Polygon createThirdPolygon() {
498
    Polygon triangle = new Polygon();
499
    triangle.getPoints().setAll(
500
        560d, 200d,
501
        630d, 220d,
502
        650d, 300d,
503
        610d, 370d,
504
        580d, 400d,
505
        510d, 380d,
506
        460d, 340d,
507
        430d, 270d,
508
        490d, 210d
509
    );
510
    triangle.setStroke(Color.BLUE);
511
    triangle.setStrokeWidth(4);
512
    triangle.setStrokeLineCap(StrokeLineCap.ROUND);
513
    triangle.setFill(Color.BLUE);
514
    return triangle;  
515
}
516
//sorts points to comply with javafxs polygon renderer
517
private ObservableList<Double> pointSorter(ObservableList<Double> s){
518
    double centerX = 0, centerY = 0;
519
    for(int i = 0; i < s.size(); i+=2){
520
        centerX += s.get(i);
521
        centerY += s.get(i+1);
522
    }
523
    centerX /= (s.size()/2);
524
    centerY /= (s.size()/2);
525
    ArrayList<Double> angles = new ArrayList<>();
526
    for (int j = 0; j < s.size(); j+=2){
527
        angles.add(Math.atan2(s.get(j+1) - centerY , s.get(j) - centerX));
528
    }
529
    for(int k = angles.size() - 1; k >= 0; k--){
530
        for(int w = 1; w < k; w++){
531
            if(angles.get(w-1) > angles.get(w)){
532
                double temp = angles.get(w-1);
533
                angles.set(w-1,angles.get(w));
534
                angles.set(w, temp);
535
                double tempx = s.get(2*(w-1));
536
                double tempy = s.get(2*(w-1) + 1);
537
                s.set(2*(w-1), s.get(2*w));
538
                s.set(2*(w-1)+1, s.get((2*w) + 1));
539
                s.set(2*w, tempx);
540
                s.set((2*w)+1, tempy);
541
            }
542
        }
543
    }
544
    return s;
545
}
546
//convex implementation modified slightly from liangs book
547
private ObservableList<Double> getConvexHull(ObservableList<Double> s){
548
    Point2D[] myPoints = new Point2D[s.size()/2];
549
    for(int i = 0; i < myPoints.length; i++)
550
        myPoints[i] = new Point2D(s.get(2*i),s.get((2*i)+1));
551
      Point2D h0 = getRightMostLowestPoint(myPoints);
552
      ArrayList<Point2D> H = new ArrayList<Point2D>();
553
      H.add(h0);
554
      Point2D t0 = h0;
555
      while (true) {
556
        Point2D t1 = myPoints[0];
557
        for(int i = 1; i < myPoints.length; i++){
558
            double status = whichSide(t0.getX(), t0.getY(), t1.getX(), t1.getY(), myPoints[i].getX(), myPoints[i].getY());
559
            if(status > 0)
560
                t1 = myPoints[i];
561
            else  if (status == 0){
562
                if(distance(myPoints[i].getX(), myPoints[i].getY(), t0.getX(), t0.getY()) > distance(t1.getX(), t1.getY(), t0.getX(), t0.getY()))
563
                    t1 = myPoints[i];
564
            } 
565
        }
566
      if(t1.getX() == h0.getX() && t1.getY() == h0.getY())
567
          break;
568
      else {
569
        H.add(t1);
570
        t0 = t1;
571
      }
572
   }
573
    ObservableList<Double> pnts = FXCollections.observableArrayList();
574
    for(int j = 0; j < H.size(); j++){
575
        pnts.add(H.get(j).getX());
576
        pnts.add(H.get(j).getY());
577
    }
578
    return pnts;
579
  }
580
  public double whichSide(double x0, double y0, double x1, double y1, double x2, double y2){
581
    return (x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0);
582
  }
583
  public double distance(double x1, double y1, double x2, double y2) {
584
  return Math.sqrt((x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2));
585
  }
586
  static Point2D getRightMostLowestPoint(Point2D[] p){
587
    int rightMostIndex = 0;
588
    double rightMostX = p[0].getX();
589
    double rightMostY = p[0].getY();
590
    for(int i = 0; i < p.length; i++){
591
        if(rightMostY < p[i].getY()) {
592
            rightMostY = p[i].getY();
593
            rightMostX = p[i].getX();
594
            rightMostIndex = i;
595
        }
596
        else if (rightMostY == p[i].getY() && rightMostX < p[i].getX()){
597
            rightMostX = p[i].getX();
598
            rightMostIndex = i;
599
        }     
600
   }
601
    return p[rightMostIndex];
602
}
603
//creates virtual polygon per a value and polygons observable list values. used with convex hull
604
private ObservableList<Double> virtualToRealPolygon(final ObservableList<Double> rpoints, double a){
605
   ObservableList<Double> vpoints = FXCollections.observableArrayList();
606
    for(int i = 0; i < rpoints.size(); i+= 2){
607
        vpoints.add(rpoints.get(i));
608
        vpoints.add(rpoints.get(i+1));
609
    }
610
    for(int j = 0; j < rpoints.size(); j+= 2){
611
        vpoints.add(rpoints.get(j)-(a/2));
612
        vpoints.add(rpoints.get(j+1)+(.866*a));
613
        vpoints.add(rpoints.get(j) - a);
614
        vpoints.add(rpoints.get(j+1));
615
    }
616
    return vpoints;
617
}
618
//returns a list of draggable anchors for polygon specified at each vertice
619
private ObservableList<Anchor> createControlAnchorsFor(final ObservableList<Double> points) {
620
    ObservableList<Anchor> anchors = FXCollections.observableArrayList();
621
    for (int i = 0; i < points.size(); i+=2) {
622
      final int idx = i;
623
        DoubleProperty xProperty = new SimpleDoubleProperty(points.get(i));
624
        DoubleProperty yProperty = new SimpleDoubleProperty(points.get(i + 1));
625
        xProperty.addListener(new ChangeListener<Number>() {
626
            @Override public void changed(ObservableValue<? extends Number> ov, Number oldX, Number x) {
627
                points.set(idx, (double) x);
628
            }
629
        });
630
        yProperty.addListener(new ChangeListener<Number>() {
631
            @Override public void changed(ObservableValue<? extends Number> ov, Number oldY, Number y) {
632
                points.set(idx + 1, (double) y);
633
            }
634
        });
635
        anchors.add(new Anchor(Color.BLUE, xProperty, yProperty));
636
    }
637
    return anchors;
638
  }
639
}

+ 84 - 0
Anchor.java

@ -0,0 +1,84 @@
1
//Abrahim Ladha
2
//Hashemi AI Project 1
3
import javafx.scene.Scene;
4
import java.util.*;
5
import javafx.application.Application;
6
import javafx.beans.property.*;
7
import javafx.beans.value.*;
8
import javafx.collections.*;
9
import javafx.event.EventHandler;
10
import javafx.event.ActionEvent;
11
import javafx.scene.*;
12
import javafx.scene.input.*;
13
import javafx.scene.paint.Color;
14
import javafx.scene.shape.*;
15
import javafx.geometry.Point2D;
16
import javafx.scene.control.*;
17
import javafx.scene.layout.*;
18
public class Anchor extends Circle {
19
    private final DoubleProperty x, y;
20
21
    Anchor(Color color, DoubleProperty x, DoubleProperty y) {
22
      super(x.get(), y.get(), 5);
23
      setFill(color);
24
      setStroke(color);
25
      setStrokeWidth(1);
26
      setStrokeType(StrokeType.OUTSIDE);
27
      this.x = x;
28
      this.y = y;
29
      x.bind(centerXProperty());
30
      y.bind(centerYProperty());
31
      enableDrag();
32
    }
33
    // drag a vertice of the polygon around
34
    private void enableDrag() {
35
      final Delta dragDelta = new Delta();
36
      //mouse down?
37
      setOnMousePressed(new EventHandler<MouseEvent>() {
38
        @Override public void handle(MouseEvent mouseEvent) {
39
          // delta change from drag and drop distance.
40
          dragDelta.x = getCenterX() - mouseEvent.getX();
41
          dragDelta.y = getCenterY() - mouseEvent.getY();
42
          getScene().setCursor(Cursor.MOVE);
43
        }
44
      });
45
      //mouse lifted?
46
      setOnMouseReleased(new EventHandler<MouseEvent>() {
47
        @Override public void handle(MouseEvent mouseEvent) {
48
          getScene().setCursor(Cursor.HAND);
49
50
        }
51
      });
52
      //mouse down and moving?
53
      setOnMouseDragged(new EventHandler<MouseEvent>() {
54
        @Override public void handle(MouseEvent mouseEvent) {
55
          double newX = mouseEvent.getX() + dragDelta.x;
56
          if (newX > 0 && newX < getScene().getWidth()) {
57
            setCenterX(newX);
58
          }
59
          double newY = mouseEvent.getY() + dragDelta.y;
60
          if (newY > 0 && newY < getScene().getHeight()) {
61
            setCenterY(newY);
62
          }
63
        }
64
      });
65
      //mouse entered?
66
      setOnMouseEntered(new EventHandler<MouseEvent>() {
67
        @Override public void handle(MouseEvent mouseEvent) {
68
          if (!mouseEvent.isPrimaryButtonDown()) {
69
            getScene().setCursor(Cursor.HAND);
70
          }
71
        }
72
      });
73
      //mouse exited?
74
      setOnMouseExited(new EventHandler<MouseEvent>() {
75
        @Override public void handle(MouseEvent mouseEvent) {
76
          if (!mouseEvent.isPrimaryButtonDown()) {
77
            getScene().setCursor(Cursor.DEFAULT);
78
          }
79
        }
80
      });
81
    }
82
    // keeps relative x's and y's
83
    private class Delta { double x, y; }
84
  }

+ 82 - 0
Anchor.java~

@ -0,0 +1,82 @@
1
import javafx.scene.Scene;
2
import java.util.*;
3
import javafx.application.Application;
4
import javafx.beans.property.*;
5
import javafx.beans.value.*;
6
import javafx.collections.*;
7
import javafx.event.EventHandler;
8
import javafx.event.ActionEvent;
9
import javafx.scene.*;
10
import javafx.scene.input.*;
11
import javafx.scene.paint.Color;
12
import javafx.scene.shape.*;
13
import javafx.geometry.Point2D;
14
import javafx.scene.control.*;
15
import javafx.scene.layout.*;
16
public class Anchor extends Circle {
17
    private final DoubleProperty x, y;
18
19
    Anchor(Color color, DoubleProperty x, DoubleProperty y) {
20
      super(x.get(), y.get(), 5);
21
      setFill(color);
22
      setStroke(color);
23
      setStrokeWidth(1);
24
      setStrokeType(StrokeType.OUTSIDE);
25
      this.x = x;
26
      this.y = y;
27
      x.bind(centerXProperty());
28
      y.bind(centerYProperty());
29
      enableDrag();
30
    }
31
    // drag a vertice of the polygon around
32
    private void enableDrag() {
33
      final Delta dragDelta = new Delta();
34
      //mouse down?
35
      setOnMousePressed(new EventHandler<MouseEvent>() {
36
        @Override public void handle(MouseEvent mouseEvent) {
37
          // delta change from drag and drop distance.
38
          dragDelta.x = getCenterX() - mouseEvent.getX();
39
          dragDelta.y = getCenterY() - mouseEvent.getY();
40
          getScene().setCursor(Cursor.MOVE);
41
        }
42
      });
43
      //mouse lifted?
44
      setOnMouseReleased(new EventHandler<MouseEvent>() {
45
        @Override public void handle(MouseEvent mouseEvent) {
46
          getScene().setCursor(Cursor.HAND);
47
48
        }
49
      });
50
      //mouse down and moving?
51
      setOnMouseDragged(new EventHandler<MouseEvent>() {
52
        @Override public void handle(MouseEvent mouseEvent) {
53
          double newX = mouseEvent.getX() + dragDelta.x;
54
          if (newX > 0 && newX < getScene().getWidth()) {
55
            setCenterX(newX);
56
          }
57
          double newY = mouseEvent.getY() + dragDelta.y;
58
          if (newY > 0 && newY < getScene().getHeight()) {
59
            setCenterY(newY);
60
          }
61
        }
62
      });
63
      //mouse entered?
64
      setOnMouseEntered(new EventHandler<MouseEvent>() {
65
        @Override public void handle(MouseEvent mouseEvent) {
66
          if (!mouseEvent.isPrimaryButtonDown()) {
67
            getScene().setCursor(Cursor.HAND);
68
          }
69
        }
70
      });
71
      //mouse exited?
72
      setOnMouseExited(new EventHandler<MouseEvent>() {
73
        @Override public void handle(MouseEvent mouseEvent) {
74
          if (!mouseEvent.isPrimaryButtonDown()) {
75
            getScene().setCursor(Cursor.DEFAULT);
76
          }
77
        }
78
      });
79
    }
80
    // keeps relative x's and y's
81
    private class Delta { double x, y; }
82
  }

+ 94 - 0
Node.java

@ -0,0 +1,94 @@
1
//Abrahim Ladha
2
//Hashemi AI Project 1
3
import java.util.ArrayList;
4
import java.util.Arrays;
5
import java.util.Comparator;
6
import java.util.HashMap;
7
import java.util.*;
8
public class Node {
9
    private Node parent;
10
    private HashSet<Node> neighbors;
11
    private double x;
12
    private double y;
13
    private Object data;
14
    //empty constructor
15
    public Node() {
16
        neighbors = new HashSet<Node>();
17
        data = new Object();
18
    }
19
    //constructor
20
    public Node(double x, double y) {
21
        this();
22
        this.x = x;
23
        this.y = y;
24
    }
25
    //also a constructor
26
    public Node(Node parent) {
27
        this();
28
        this.parent = parent;
29
    }
30
    //also also a constructor
31
    public Node(Node parent, double x, double y) {
32
        this();
33
        this.parent = parent;
34
        this.x = x;
35
        this.y = y;
36
    }
37
    //returns the set of neighbors
38
    public HashSet<Node> getNeighbors() {
39
        return neighbors;
40
    }
41
    //replaces all neighbors with the hashset param
42
    public void setNeighbors(HashSet<Node> neighbors) {
43
        this.neighbors = neighbors;
44
    }
45
    //adds a neighbors to this nodes hashset
46
    public void addNeighbor(Node node) {
47
        this.neighbors.add(node);
48
    }
49
    //adds multiple neighbors to this nodes hashset
50
    public void addNeighbors(Node... node) {
51
        this.neighbors.addAll(Arrays.asList(node));
52
    }
53
    //returns this nodes parent
54
    public Node getParent() {
55
        return parent;
56
    }
57
    //sets the parent of this node
58
    public void setParent(Node parent) {
59
        this.parent = parent;
60
    }
61
    //returns the x coordinate of this node
62
    public double getX() {
63
        return x;
64
    }
65
    //sets the x coordinate
66
    public void setX(double x) {
67
        this.x = x;
68
    }
69
    //returns the y coordinate
70
    public double getY() {
71
        return y;
72
    }
73
    //sets the y coordinate
74
    public void setY(double y) {
75
        this.y = y;
76
    }
77
    //set the x and y coordinate at the same time
78
    public void setXY(double x, double y) {
79
        this.x = x;
80
        this.y = y;
81
    }
82
    //obligatory data just to hook in
83
    public Object getData() {
84
        return data;
85
    }
86
    //changes the data
87
    public void setData(Object data) {
88
        this.data = data;
89
    }
90
    //for contains and other comparisons
91
    public boolean equals(Node n) {
92
        return this.x == n.x && this.y == n.y;
93
    }
94
}

+ 92 - 0
Node.java~

@ -0,0 +1,92 @@
1
import java.util.ArrayList;
2
import java.util.Arrays;
3
import java.util.Comparator;
4
import java.util.HashMap;
5
import java.util.*;
6
public class Node {
7
    private Node parent;
8
    private HashSet<Node> neighbors;
9
    private double x;
10
    private double y;
11
    private Object data;
12
    //empty constructor
13
    public Node() {
14
        neighbors = new HashSet<Node>();
15
        data = new Object();
16
    }
17
    //constructor
18
    public Node(double x, double y) {
19
        this();
20
        this.x = x;
21
        this.y = y;
22
    }
23
    //also a constructor
24
    public Node(Node parent) {
25
        this();
26
        this.parent = parent;
27
    }
28
    //also also a constructor
29
    public Node(Node parent, double x, double y) {
30
        this();
31
        this.parent = parent;
32
        this.x = x;
33
        this.y = y;
34
    }
35
    //returns the set of neighbors
36
    public HashSet<Node> getNeighbors() {
37
        return neighbors;
38
    }
39
    //replaces all neighbors with the hashset param
40
    public void setNeighbors(HashSet<Node> neighbors) {
41
        this.neighbors = neighbors;
42
    }
43
    //adds a neighbors to this nodes hashset
44
    public void addNeighbor(Node node) {
45
        this.neighbors.add(node);
46
    }
47
    //adds multiple neighbors to this nodes hashset
48
    public void addNeighbors(Node... node) {
49
        this.neighbors.addAll(Arrays.asList(node));
50
    }
51
    //returns this nodes parent
52
    public Node getParent() {
53
        return parent;
54
    }
55
    //sets the parent of this node
56
    public void setParent(Node parent) {
57
        this.parent = parent;
58
    }
59
    //returns the x coordinate of this node
60
    public double getX() {
61
        return x;
62
    }
63
    //sets the x coordinate
64
    public void setX(double x) {
65
        this.x = x;
66
    }
67
    //returns the y coordinate
68
    public double getY() {
69
        return y;
70
    }
71
    //sets the y coordinate
72
    public void setY(double y) {
73
        this.y = y;
74
    }
75
    //set the x and y coordinate at the same time
76
    public void setXY(double x, double y) {
77
        this.x = x;
78
        this.y = y;
79
    }
80
    //obligatory data just to hook in
81
    public Object getData() {
82
        return data;
83
    }
84
    //changes the data
85
    public void setData(Object data) {
86
        this.data = data;
87
    }
88
    //for contains and other comparisons
89
    public boolean equals(Node n) {
90
        return this.x == n.x && this.y == n.y;
91
    }
92
}

+ 1 - 0
README.md

@ -0,0 +1 @@
1
# hashemi-robot