Skip to content
This repository was archived by the owner on May 8, 2023. It is now read-only.

Commit 6b9596b

Browse files
authored
Merge pull request #38 from NineNineFive/Lars
Merge branch 'master' into Lars
2 parents f881ac1 + 928e84e commit 6b9596b

File tree

18 files changed

+240
-26
lines changed

18 files changed

+240
-26
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
/workspace.xml
33
/PathfindingProject.iml
44
PathfindingProject.iml
5-
/out/
5+
/out/

src/files/Controller.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package files;
22

33
import files.controllers.Input;
4-
import files.models.Graph;
4+
import files.interfaces.Geometry;
5+
import files.models.Shape;
6+
7+
import java.util.ArrayList;
58

69
public class Controller {
710
private static boolean created = false;
@@ -15,6 +18,7 @@ public class Controller {
1518
this.view = view;
1619
this.input = new Input(model,view);
1720
this.run();
21+
this.updateView();
1822
}
1923

2024
private void onlyOneInstance (){
@@ -33,4 +37,14 @@ private void run(){
3337
//view.getUI().getGraphic().setGraph(graph);
3438
//view.getUI().getGraphic().draw();
3539
}
40+
41+
private void updateView(){
42+
// TODO: Insert Environment
43+
ArrayList<Shape> shapes = model.getEnvironment().getShapes();
44+
//view.setEnvironment();
45+
}
46+
47+
private void updateModel(){
48+
49+
}
3650
}

src/files/Model.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
public class Model {
1212
private static boolean created = false;
1313
private Graph graph;
14+
private Environment environment;
15+
private Pathfinding pathfinding;
1416

1517
public Model(){
1618
this.onlyOneInstance(); // Limit to only 1 instance of the Model class
17-
Environment environment = new Environment();
18-
Pathfinding pathfinding = new Pathfinding();
19+
environment = new Environment();
20+
pathfinding = new Pathfinding();
1921
//graph = new Graph(6);
2022
//graph.graphTest(6);
2123
}
@@ -36,4 +38,8 @@ public Graph getGraph() {
3638
public void setGraph(Graph graph) {
3739
this.graph = graph;
3840
}
41+
42+
public Environment getEnvironment() {
43+
return environment;
44+
}
3945
}

src/files/views/Environment.java renamed to src/files/interfaces/Environment.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package files.views;
1+
package files.interfaces;
22

33
import javafx.scene.canvas.GraphicsContext;
44

src/files/interfaces/Geometry.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package files.interfaces;
2+
3+
import files.models.shapes.Point;
4+
5+
public interface Geometry {
6+
boolean isColliding(Point point);
7+
}

src/files/models/Environment.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,22 @@
11
package files.models;
22

3+
import files.models.shapes.Rectangle2D;
4+
import java.util.ArrayList;
5+
36
public class Environment {
7+
ArrayList<Shape> shapes = new ArrayList<Shape>();
8+
9+
public void generateEnvironment(){
10+
//TODO: Make random map generator
11+
}
12+
13+
public void addShape() {
14+
shapes.add(new Rectangle2D(50, 50, 20, 50));
15+
shapes.add(new Rectangle2D(350, 50, 15, 150));
16+
}
17+
18+
public ArrayList<Shape> getShapes(){
19+
return shapes;
20+
}
421
}
22+

src/files/models/Shape.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package files.models;
2+
3+
import files.interfaces.Geometry;
4+
5+
public abstract class Shape implements Geometry {
6+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package files.models.shapes;
2+
3+
import files.interfaces.Geometry;
4+
import files.models.Shape;
5+
6+
public class Line2D extends Shape {
7+
private Point start;
8+
private Point end;
9+
10+
Line2D(Point start, Point end) {
11+
this.start = start;
12+
this.end = end;
13+
}
14+
15+
public Point getStart() {
16+
return start;
17+
}
18+
19+
public Point getEnd() {
20+
return end;
21+
}
22+
23+
public boolean isColliding(Point point){
24+
return point.isOnLine(this);
25+
}
26+
}

src/files/models/shapes/Point.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package files.models.shapes;
2+
3+
public class Point {
4+
private double x, y, z;
5+
6+
Point(double x) {
7+
this(x, 0, 0);
8+
}
9+
10+
Point(double x, double y) {
11+
this(x, y, 0);
12+
}
13+
14+
Point(double x, double y, double z) {
15+
this.x = x;
16+
this.y = y;
17+
this.z = z;
18+
}
19+
20+
int calcRelation(Line2D line) {
21+
// Will return greater than 0 if point is left of line
22+
// Will return 0 if point is on the line
23+
// Will return left than 0 if point is right of line
24+
// Formula: d=(x−x1)(y2−y1)−(y−y1)(x2−x1)
25+
// we use d as result, we use x as point.getX(), we use y as point.getY(),
26+
// we use x1 as start.getX(), we use y1 as start.getY(), we use x2 as end.getX(), and we use y2 as end.getY()
27+
double result = (x - line.getStart().getX()) * (line.getEnd().getY() - line.getStart().getY()) - (y - line.getStart().getY()) * (line.getEnd().getX() - line.getStart().getX());
28+
if (0 < result) {
29+
return -1;
30+
} else if (0 > result) {
31+
return 1;
32+
} else {
33+
return 0;
34+
}
35+
}
36+
37+
boolean isLeftOfLine(Line2D line) {
38+
if (0 < calcRelation(line))
39+
return true; // if the calculation is positive, the point is on the left side of the line
40+
else return false;
41+
}
42+
43+
boolean isOnLine(Line2D line) {
44+
if (0 == calcRelation(line)) return true; // if the calculation is zero, the point is on the line
45+
else return false;
46+
}
47+
48+
boolean isRightOfLine(Line2D line) {
49+
// Our Choice: This is the only important check, as we chose to use this continiously for collision detection in objects
50+
if (0 > calcRelation(line))
51+
return true; // if the calculation is negative, the point is on the right side of the line
52+
else return false;
53+
}
54+
55+
public double getX() {
56+
return x;
57+
}
58+
59+
public double getY() {
60+
return y;
61+
}
62+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package files.models.shapes;
2+
import files.interfaces.Geometry;
3+
import files.models.Shape;
4+
5+
public class Rectangle2D extends Shape {
6+
private Point A, B, C, D;
7+
private Line2D AB, BD, CD, AC;
8+
private Point position;
9+
private Point size;
10+
private Triangle2D firstHalf;
11+
private Triangle2D secondHalf;
12+
13+
public Rectangle2D(double x, double y, double width, double height) {
14+
A = new Point(x,y);
15+
B = new Point(x+width,y);
16+
C = new Point(x,y+height);
17+
D = new Point(x+width,y+height);
18+
AB = new Line2D(A,B);
19+
BD = new Line2D(B,D);
20+
CD = new Line2D(C,D);
21+
AC = new Line2D(A,C);
22+
firstHalf = new Triangle2D(A, B, C);
23+
secondHalf = new Triangle2D(D, B, C);
24+
25+
this.position = A;
26+
this.size = new Point(width, height);
27+
}
28+
29+
public boolean isColliding(Point obj) {
30+
return firstHalf.isColliding(obj) || secondHalf.isColliding(obj);
31+
}
32+
}

0 commit comments

Comments
 (0)