diff --git a/src/Car.java b/src/Car.java index bef1ec0..3d14a30 100644 --- a/src/Car.java +++ b/src/Car.java @@ -4,6 +4,7 @@ import static java.lang.Math.PI; import static processing.core.PApplet.degrees; import static processing.core.PApplet.radians; +import Graph.*; import Vector.Vector; import processing.core.PApplet; @@ -37,17 +38,17 @@ public class Car{ } //draw the car and its views - public void drawCar(ArrayList walls){ + public void drawCar(PointGraph g){ proc.stroke(255); proc.ellipse(pose.x, pose.y, carWidth, carLength); - this.updateScan(walls); + this.updateScan(g); // this.slam.drawLines(); } //With all the views that the car has, get their point list - void updateScan(ArrayList walls){ + void updateScan(PointGraph map){ for(View view : views){ - view.look(walls); + view.look(map); } for(View view : views){ diff --git a/src/Graph/LineEdge.java b/src/Graph/LineEdge.java index e9dbb6e..03fd302 100644 --- a/src/Graph/LineEdge.java +++ b/src/Graph/LineEdge.java @@ -5,6 +5,8 @@ import processing.core.PApplet; import java.awt.*; +import static java.lang.Math.PI; + public class LineEdge extends Edge implements LineInterface{ protected PointVertex vStart; protected PointVertex vEnd; @@ -41,5 +43,12 @@ public class LineEdge extends Edge implements LineInterface{ public void draw(PApplet proc){ line.draw(proc); + Vector leftFlange = line.getDirection().rotate2D((float)(-3*PI/4)).normalize().mul(20); + Vector rightFlange = line.getDirection().rotate2D((float) (3*PI/4)).normalize().mul(20); + Line l1 = new Line(line.endPoint(), line.endPoint().add(leftFlange)); + Line l2 = new Line(line.endPoint(), line.endPoint().add(rightFlange)); + l1.draw(proc); + l2.draw(proc); } + } diff --git a/src/Graph/PointGraph.java b/src/Graph/PointGraph.java index 532fa43..197ba6f 100644 --- a/src/Graph/PointGraph.java +++ b/src/Graph/PointGraph.java @@ -11,7 +11,7 @@ import java.util.ArrayList; public class PointGraph extends Graph { PointVertex selectedVertex; - PointGraph(){ + public PointGraph(){ super(); } @@ -26,6 +26,21 @@ public class PointGraph extends Graph { } } + public void addEdge(PointVertex vStart, PointVertex vEnd){ + addVertex(vStart); + + // don't add the edge if it is already added + for(Edge e : adjList.get(vStart)){ + if(e.getEndVertex() == (Vertex) vEnd){ + return; + } + } + + addVertex(vEnd); + LineEdge edge = new LineEdge(vStart, vEnd); + adjList.get((Vertex) vStart).add(new LineEdge(vStart, vEnd)); + } + /** * @param v set this vertex as the selected vertex in the graph */ diff --git a/src/Graph/PointVertex.java b/src/Graph/PointVertex.java index 758d16c..d1a6ccf 100644 --- a/src/Graph/PointVertex.java +++ b/src/Graph/PointVertex.java @@ -10,7 +10,7 @@ public class PointVertex extends Vertex { * @param xPos the x position of the vertex * @param yPos the y posiiton of the vertex */ - PointVertex(float xPos, float yPos){ + public PointVertex(float xPos, float yPos){ super(); this.position = new Vector(xPos, yPos); } diff --git a/src/Processing.java b/src/Processing.java index ddf5701..f2e4d8d 100644 --- a/src/Processing.java +++ b/src/Processing.java @@ -1,14 +1,14 @@ -import Vector.Vector; +import Graph.*; import processing.core.PApplet; -import java.util.ArrayList; public class Processing extends PApplet { Car car; - ArrayList objects = new ArrayList<>(); public static PApplet processing; + PointGraph map = new PointGraph(); + public static void main(String[] args) { PApplet.main("Processing"); } @@ -18,23 +18,18 @@ public class Processing extends PApplet { car = new Car(processing, 100,100,50,40); size(1000, 1000); car.addView(360,180); - for(int i = 0; i < 15; i++){ - Wall wall = new Wall(processing, new Vector((int)random(50, 950), (int)random(50, 950)), new Vector((int)random(50, 950), (int)random(50, 950))); - objects.add(wall); + + for(int i = 0; i < 10; i++){ + PointVertex vStart = new PointVertex(random(50, 950), random(50, 950)); + PointVertex vEnd = new PointVertex(random(50, 950), random(50, 950)); + map.addEdge(vStart, vEnd); } + } public void draw(){ background(0); -// for(Wall object : objects){ -// object.drawWall(); -// } - car.drawCar(objects); - View view = car.views.get(0); - view.look(objects); - ArrayList points = view.getPoints(); - for(Vector point: points){ - circle(point.x, point.y, 5); - } + map.draw(processing); + car.drawCar(map); strokeWeight(2); stroke(255); //car.drive(new int[] {0, 0}); diff --git a/src/Ray.java b/src/Ray.java index dd2776a..0b5b13a 100644 --- a/src/Ray.java +++ b/src/Ray.java @@ -1,3 +1,4 @@ +import Graph.*; import Vector.*; import processing.core.PApplet; @@ -26,10 +27,10 @@ public class Ray extends Line { //checks to see at what coordinate the ray will collide with an object and sets the ray length to meet that point. - public void castRay(ArrayList walls){ + public void castRay(PointGraph map){ float shortestWallDistance = maxRayDistance; - int[] newColor = new int[]{255, 255, 255}; - for(Wall wall : walls){ + ArrayList walls = map.getAllEdges(); + for(LineEdge wall : walls){ // get the necessary vectors for two parameterized lines // parameterized lines are of the form L = d*t + p @@ -51,7 +52,6 @@ public class Ray extends Line { float distance = d1.mul(t).add(p1).sub(this.position).mag(); if(distance < shortestWallDistance){ shortestWallDistance = distance; - newColor = new int[]{wall.r, wall.g, wall.b}; } } @@ -59,11 +59,9 @@ public class Ray extends Line { // if we collided with a wall, set the ray's length to the distance from it to the collision if(shortestWallDistance != maxRayDistance){ this.direction = this.direction.normalize().mul(shortestWallDistance); - this.color = newColor; } else{ this.direction = this.direction.normalize().mul(maxRayDistance); - this.color = new int[]{255, 255, 255}; } } @@ -92,8 +90,8 @@ public class Ray extends Line { public void setRayLength(int rayLength){this.direction = this.direction.normalize().mul(rayLength);} public void setAngle(float angle){ - float distance = this.direction.mag(); - this.direction = new Vector(cos(angle), sin(angle)).mul(distance); + float currentAngle = direction.angle(); + this.direction = direction.rotate2D(angle - currentAngle); } } \ No newline at end of file diff --git a/src/Vector/Vector.java b/src/Vector/Vector.java index 304216a..8bdf9e8 100644 --- a/src/Vector/Vector.java +++ b/src/Vector/Vector.java @@ -1,6 +1,8 @@ package Vector; import static java.lang.Math.*; +import static processing.core.PApplet.cos; +import static processing.core.PApplet.sin; public class Vector { public float x = 0; @@ -82,4 +84,10 @@ public class Vector { public float angle(){ return (float) atan2(y, x); } + + public Vector rotate2D(float angle){ + float distance = mag(); + float currentAngle = this.angle(); + return new Vector(cos(currentAngle + angle), sin(currentAngle + angle)).mul(distance); + } } diff --git a/src/View.java b/src/View.java index ff95b9b..7134083 100644 --- a/src/View.java +++ b/src/View.java @@ -1,3 +1,4 @@ +import Graph.PointGraph; import Vector.Vector; import processing.core.*; import java.util.ArrayList; @@ -30,9 +31,9 @@ public class View { } //sees if the ray will collide with the walls in the wall list - public void look(ArrayList walls) { + public void look(PointGraph map) { for (Ray ray : rays) { - ray.castRay(walls); + ray.castRay(map); if(ray.hasCollided()){ ray.drawRay(proc); } diff --git a/src/Wall.java b/src/Wall.java deleted file mode 100644 index 64dae15..0000000 --- a/src/Wall.java +++ /dev/null @@ -1,40 +0,0 @@ -import Vector.*; -import processing.core.*; - -import static processing.core.PApplet.*; - -public class Wall extends Line { - int r; - int g; - int b; - - private final PApplet proc; - - Wall(PApplet proc, Vector pos, float angle, int wallLength){ - super(pos, (new Vector(cos(angle), sin(angle)).mul(wallLength)).add(pos)); - this.proc = proc; - r = (int)proc.random(50, 255); - g = (int)proc.random(50, 255); - b = (int)proc.random(50, 255); - } - - Wall(PApplet proc, Vector startPos, Vector endPos){ - super(startPos, endPos); - this.proc = proc; - r = (int)proc.random(50, 255); - g = (int)proc.random(50, 255); - b = (int)proc.random(50, 255); - } - - void drawWall(){ - proc.stroke(r,g,b); - proc.strokeWeight(10); - proc.circle(position.x, position.y, 10); - proc.strokeWeight(2); - proc.line(position.x, position.y, position.x + direction.x, position.y + direction.y); - } - - Vector getPos(){ - return position; - } -} \ No newline at end of file