From 3321e77061b7570120db120916465a604dbf6c4d Mon Sep 17 00:00:00 2001 From: Quinn Date: Tue, 2 May 2023 09:49:38 -0500 Subject: [PATCH] Added a Graph datastructure and refactored the Line/Vertex classes --- .idea/uiDesigner.xml | 124 +++++++++++++++++++ src/Car.java | 2 + src/Graph/Edge.java | 59 +++++++++ src/Graph/Graph.java | 225 ++++++++++++++++++++++++++++++++++ src/Graph/LineEdge.java | 45 +++++++ src/Graph/PointGraph.java | 219 +++++++++++++++++++++++++++++++++ src/Graph/PointVertex.java | 61 +++++++++ src/Graph/Vertex.java | 45 +++++++ src/Processing.java | 15 ++- src/Ray.java | 13 +- src/SLAM.java | 7 +- src/{ => Vector}/Line.java | 18 +-- src/Vector/LineInterface.java | 20 +++ src/{ => Vector}/Vector.java | 34 ++--- src/View.java | 11 +- src/Wall.java | 3 +- tests/LineTest.java | 20 +-- tests/VectorTest.java | 6 +- 18 files changed, 859 insertions(+), 68 deletions(-) create mode 100644 .idea/uiDesigner.xml create mode 100644 src/Graph/Edge.java create mode 100644 src/Graph/Graph.java create mode 100644 src/Graph/LineEdge.java create mode 100644 src/Graph/PointGraph.java create mode 100644 src/Graph/PointVertex.java create mode 100644 src/Graph/Vertex.java rename src/{ => Vector}/Line.java (90%) create mode 100644 src/Vector/LineInterface.java rename src/{ => Vector}/Vector.java (70%) diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/Car.java b/src/Car.java index 5f3000e..bef1ec0 100644 --- a/src/Car.java +++ b/src/Car.java @@ -3,6 +3,8 @@ import java.util.ArrayList; import static java.lang.Math.PI; import static processing.core.PApplet.degrees; import static processing.core.PApplet.radians; + +import Vector.Vector; import processing.core.PApplet; public class Car{ diff --git a/src/Graph/Edge.java b/src/Graph/Edge.java new file mode 100644 index 0000000..fc87508 --- /dev/null +++ b/src/Graph/Edge.java @@ -0,0 +1,59 @@ +package Graph; + +public class Edge { + Vertex vStart = null; + Vertex vEnd = null; + float weight = 0; + + /** + * @param vStart the vertex the edge starts at + * @param vEnd the vertex the edge ends at + * @param weight the weight of the edge + */ + Edge(Vertex vStart, Vertex vEnd, float weight){ + this.vStart = vStart; + this.vEnd = vEnd; + this.weight = weight; + } + + /** + * @param vStart the vertex the edge starts at + * @param vEnd the vertex the edge ends at + */ + Edge(Vertex vStart, Vertex vEnd){ + this.vStart = vStart; + this.vEnd = vEnd; + } + + Edge(){} + + /** + * @return the weight of the edge + */ + public float getWeight(){ + return weight; + } + + /** + * @param newWeight set the edge's weight to something new + */ + public void setWeight(float newWeight){ + this.weight = newWeight; + } + + /** + * @return the vertex at the end of the edge + */ + public Vertex getEndVertex(){ + return vEnd; + } + + /** + * @return the vertex at the start of the edge + */ + public Vertex getStartVertex(){ + return vStart; + } + +} + diff --git a/src/Graph/Graph.java b/src/Graph/Graph.java new file mode 100644 index 0000000..a75558b --- /dev/null +++ b/src/Graph/Graph.java @@ -0,0 +1,225 @@ +package Graph; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedList; + +public class Graph { + + /** + * Create a new empty graph + */ + public Graph(){ + // hash maps require an initial size to get started + adjList = new HashMap>(1024); + } + + /** + * @return the total number of vertices in the graph + */ + public int numVertices(){ + return adjList.size(); + } + + /** + * @return the total number of edges in the graph. + */ + public int numEdges(){ + int sum = 0; + for(Vertex v : adjList.keySet()){ + sum += adjList.get(v).size(); + } + return sum; + } + + /** + * @param v add the vertex v to the graph + */ + public void addVertex(Vertex v){ + // check if it's already in the adjacency listVertex + if(adjList.containsKey(v)){ + return; + } + adjList.put(v, new LinkedList()); + + } + + /** + * @param vStart the starting vertex + * @param vEnd the ending vertex + */ + public void addEdge(Vertex vStart, Vertex vEnd){ + // don't add the edge if it is already added + for(Edge e : adjList.get(vStart)){ + if(e.getEndVertex() == vEnd){ + return; + } + } + adjList.get(vStart).add(new Edge(vStart, vEnd)); + } + + /** + * @brief Gets how many other vertices a given vertex points to + * @param v the vertex you want to know about + * @return the number of vertices this vertex points to + */ + public int outDegree(Vertex v){ + return adjList.get(v).size(); + } + + /** + * @param v the vertex of interest + * @return the number of edges going into the vertex + */ + public int inDegree(Vertex v){ + int count = 0; + for(Vertex v1 : adjList.keySet()){ + for(Edge edge : adjList.get(v1)){ + if(edge.getEndVertex() == v){ + count++; + } + } + } + return count; + } + + /** + * Print out all vertexes in the graph + */ + public void print(){ + for(Vertex key : adjList.keySet()){ + System.out.print("Key: " + key.getLabel() + ": "); + for(Edge edge : adjList.get(key)){ + System.out.print(edge.getEndVertex().getLabel()); + System.out.print(","); + } + System.out.println("NULL"); + } + } + + /** + * @param v the vertext to start the traverse at + * @return a list of vertexes in order of when they were visited from first to last. + */ + public ArrayList depthFirstTraverse(Vertex v){ + ArrayList vertexList = new ArrayList(); + DFSTraverse(v, vertexList); + resetVisits(); + + return vertexList; + } + + /** + * @param v the vertex to begin the traverse at + * @param vertexList the vertex list to append visited vertexes to + */ + private void DFSTraverse(Vertex v, ArrayList vertexList){ + vertexList.add(v); + v.setVisitedStatus(true); + LinkedList edgeList = adjList.get(v); + for(Edge edge : edgeList){ + if(!(edge.getEndVertex().visitedStatus())){ + DFSTraverse(edge.getEndVertex(), vertexList); + } + } + } + + /** + * @post all vertexes in the graph will be set to unvisited + */ + private void resetVisits(){ + for(Vertex v : adjList.keySet()){ + v.setVisitedStatus(false); + } + } + + /** + * @param v the vertex that you wish to see the neighbors of + * @return a list of vertexes that are connected to v with ingoing or outgoing edges + */ + public ArrayList getNeightborVerts(Vertex v){ + // iterate through all of the vertexes and make sure they're marked as unvisited + for(Vertex v1 : adjList.keySet()){ + v1.setVisitedStatus(false); + } + + ArrayList neighbors = new ArrayList<>(); + for(Vertex v1 : adjList.keySet()){ + for(Edge edge : adjList.get(v1)){ + if(edge.getStartVertex() == v && !edge.getEndVertex().visitedStatus()){ + edge.getEndVertex().setVisitedStatus(true); + neighbors.add(edge.getEndVertex()); + } + + if(edge.getEndVertex() == v && !edge.getStartVertex().visitedStatus()){ + edge.getStartVertex().setVisitedStatus(true); + neighbors.add(edge.getStartVertex()); + } + } + } + + resetVisits(); + + return neighbors; + } + + /** + * @param v the vertex that you wish to start the search at + * @return A list of vertexes in order of visitation where the search goes wide and then deep + */ + public ArrayList breadthFirstSearch(Vertex v){ + ArrayList queue = new ArrayList<>(); + ArrayList outputList = new ArrayList<>(); + outputList.add(v); + queue.add(v); + v.setVisitedStatus(true); + + while(queue.size() > 0){ + Vertex next = queue.get(0); + queue.remove(0); + LinkedList edgeList = adjList.get(next); + for(Edge edge : edgeList){ + if(!edge.getEndVertex().visitedStatus()){ + queue.add(edge.getEndVertex()); + outputList.add(edge.getEndVertex()); + edge.getEndVertex().setVisitedStatus(true); + } + } + } + + resetVisits(); + + return outputList; + } + + /** + * @brief remove the given vertex and all edges that reference it from the graph + * @param v the vertex to remove from the graph. + */ + public void removeVertex(Vertex v){ + + // find all edges that point to the removed vertex + ArrayList edgesToRemove = new ArrayList<>(); + ArrayList startVertex = new ArrayList<>(); + for(Vertex v1 : adjList.keySet()){ + for(Edge e : adjList.get(v1)){ + if(e.getEndVertex() == v){ + edgesToRemove.add(e); + startVertex.add(e.getStartVertex()); + } + } + } + + // remove all of those edges from the adjList + int i = 0; + for(Edge e : edgesToRemove){ + adjList.get(startVertex.get(i)).remove(e); + i++; + } + // remove the vertex from the adjacency list + adjList.remove(v); + } + + protected HashMap> adjList; +} + diff --git a/src/Graph/LineEdge.java b/src/Graph/LineEdge.java new file mode 100644 index 0000000..e9dbb6e --- /dev/null +++ b/src/Graph/LineEdge.java @@ -0,0 +1,45 @@ +package Graph; + +import Vector.*; +import processing.core.PApplet; + +import java.awt.*; + +public class LineEdge extends Edge implements LineInterface{ + protected PointVertex vStart; + protected PointVertex vEnd; + protected Line line; + public LineEdge(PointVertex vStart, PointVertex vEnd) { + this.vStart = vStart; + this.vEnd = vEnd; + this.line = new Line(vStart.getPos(), vEnd.getPos()); + } + + public Vector getDirection(){ + return line.getDirection(); + } + + public Vector getPosition(){ + return line.getPosition(); + } + + public float getLength(){ + return line.getLength(); + } + + public float getAngle(){ + return line.getAngle(); + } + + public Vector endPoint(){ + return line.endPoint(); + } + + public float getDistance(Vector point){ + return line.getDistance(point); + } + + public void draw(PApplet proc){ + line.draw(proc); + } +} diff --git a/src/Graph/PointGraph.java b/src/Graph/PointGraph.java new file mode 100644 index 0000000..532fa43 --- /dev/null +++ b/src/Graph/PointGraph.java @@ -0,0 +1,219 @@ +package Graph; + +import Vector.Vector; +import processing.core.PApplet; + +import static java.lang.Math.pow; +import static java.lang.Math.sqrt; + +import java.util.ArrayList; + + +public class PointGraph extends Graph { + PointVertex selectedVertex; + PointGraph(){ + super(); + } + + public void draw(PApplet proc){ + for(Vertex v : adjList.keySet()){ + PointVertex v1 = (PointVertex) v; + v1.draw(proc); + for(Edge e : adjList.get(v)){ + LineEdge e1 = (LineEdge) e; + e1.draw(proc); + } + } + } + + /** + * @param v set this vertex as the selected vertex in the graph + */ + public void setSelectedVertex(PointVertex v){ + if(selectedVertex != null){ + deselectVertex(); + } + selectedVertex = v; + selectedVertex.setColor(new int[]{255, 0, 255, 0}); + } + + /** + * @pre a vertex needs to be selected + * @return the current pointvertex which is selected in the graph + */ + public PointVertex getSelectedVertex(){ + if(selectedVertex == null){ + if(super.adjList.size() > 0){ + selectedVertex = (PointVertex)super.adjList.keySet().iterator().next(); + } + else{ + throw new NullPointerException(); + } + } + return selectedVertex; + } + + /** + * @return true if a vertex is currently selected in the graph + */ + public boolean vertexIsSelected(){ + return selectedVertex != null; + } + + /** + * @brief deselect the currently selected vertex + */ + public void deselectVertex(){ + if(selectedVertex == null){ + return; + } + selectedVertex.setColor(new int[]{127, 255, 0, 0}); + selectedVertex = null; + } + + /** + * @param x the x coordinate to search by + * @param y the y coordinate to search by + * @return the pointvertex closest to the given x, y coordinates + */ + public PointVertex getClosestVertex(float x, float y){ + if(super.adjList.size() == 0){ + // TODO: choose a better exception name + throw new NullPointerException(); + } + + PointVertex closestVertex = (PointVertex) super.adjList.keySet().iterator().next(); + float closestDist = -1; + for(Vertex v : super.adjList.keySet()){ + PointVertex v1 = (PointVertex) v; + Vector p2 = v1.getPos(); + float dist = (float)sqrt(pow(x-p2.x, 2) + pow(y-p2.y, 2)); + if(dist < closestDist || closestDist == -1){ + closestDist = dist; + closestVertex = v1; + } + } + + return closestVertex; + } + + /** + * @return a bundle with all of the graphs vertex and edge information saved into it + */ + /* + public Bundle saveToBundle(){ + Bundle bundle = new Bundle(); + bundle.putInt("numVerts", super.numVertices()); + bundle.putInt("numEdges", super.numEdges()); + + // turn the hash map into something linear + ArrayList verts = new ArrayList<>(); + ArrayList edges = new ArrayList<>(); + for(Vertex v : super.adjList.keySet()){ + verts.add((PointVertex) v); + for(Edge e : super.adjList.get(v)){ + edges.add(e); + } + } + + // save the vertexes + int countVerts = 0; + for(PointVertex v : verts){ + String countVertsString = "vert"+String.valueOf(countVerts); + // save the vertex position + bundle.putFloatArray(countVertsString+"Pos", v.getPos()); + // save the vertex label + bundle.putString(countVertsString+"Label", v.getLabel()); + + // save if the vertex is selected + if(selectedVertex != null){ + // save if it is the selected vertex + bundle.putBoolean(countVertsString+"isSelected", v == selectedVertex); + } + else{ + bundle.putBoolean(countVertsString+"isSelected", false); + } + countVerts++; + } + + // save the edges + int countEdges = 0; + for(Edge e : edges){ + int idx = 0; + String countEdgesString = "edge" + String.valueOf(countEdges); + for(PointVertex v : verts){ + if(e.getStartVertex() == (Vertex)v){ + bundle.putInt(countEdgesString+"Start", idx); + } + else if(e.getEndVertex() == (Vertex)v){ + bundle.putInt(countEdgesString+"End", idx); + } + idx++; + } + countEdges++; + } + return bundle; + } + */ + /** + * @ brief add all graph information in ta bundle to the graph + * @param bundle the bundle to add to the graph + */ + /* + public void addBundleToGraph(Bundle bundle){ + int numVerts = bundle.getInt("numVerts", 0); + int numEdges = bundle.getInt("numEdges", 0); + ArrayList verts = new ArrayList<>(); + + // add all of the vertexes from the bundle + for(int i = 0; i < numVerts; i++){ + String countVertsString = "vert"+String.valueOf(i); + float[] pos = bundle.getFloatArray(countVertsString+"Pos"); + String label = bundle.getString(countVertsString+"Label"); + boolean isSelected = bundle.getBoolean(countVertsString+"isSelected"); + PointVertex v = new PointVertex(pos[0], pos[1], label); + addVertex(v); + verts.add(v); + if(isSelected){ + setSelectedVertex(v); + } + } + + // add all of the edges from the bundle + for(int i = 0; i < numEdges; i++){ + String countEdgesString = "edge" + String.valueOf(i); + int startVertIndex = bundle.getInt(countEdgesString+"Start"); + int endVertIndex = bundle.getInt(countEdgesString+"End"); + PointVertex vStart = verts.get(startVertIndex); + PointVertex vEnd = verts.get(endVertIndex); + addEdge(vStart, vEnd); + } + } + */ + + /** + * @return all edges in the graph + */ + public ArrayList getAllEdges(){ + ArrayList edges = new ArrayList<>(); + for(Vertex v : adjList.keySet()){ + for(Edge e : adjList.get(v)){ + LineEdge e1 = (LineEdge) e; + edges.add(e1); + } + } + return edges; + } + + /** + * @return all vertexes in the graph + */ + public ArrayList getAllVertexes(){ + ArrayList points = new ArrayList<>(); + for(Vertex v : adjList.keySet()){ + PointVertex v1 = (PointVertex) v; + points.add(v1); + } + return points; + } +} diff --git a/src/Graph/PointVertex.java b/src/Graph/PointVertex.java new file mode 100644 index 0000000..758d16c --- /dev/null +++ b/src/Graph/PointVertex.java @@ -0,0 +1,61 @@ +package Graph; +import Vector.Vector; +import processing.core.PApplet; + +public class PointVertex extends Vertex { + private Vector position; + private int[] color = new int[]{127, 255, 0, 0}; + + /** + * @param xPos the x position of the vertex + * @param yPos the y posiiton of the vertex + */ + PointVertex(float xPos, float yPos){ + super(); + this.position = new Vector(xPos, yPos); + } + + /** + * @param xPos the x position of the vertex + * @param yPos the y posiiton of the vertex + * @param label the label of the vertex + */ + PointVertex(float xPos, float yPos, String label){ + super(label); + this.position = new Vector(xPos, yPos); + } + + /** + * @param x the new x position of the vertex + * @param y the new y posiiton of the vertex + */ + public void setPos(float x, float y){ + this.position = new Vector(x, y); + } + + /** + * @return a two eleement float array containing the x and y coordinates of the vertex respectively. + */ + public Vector getPos(){ + return position; + } + + /** + * @param newColor a 4 element int array containing th alpha, r, g ,and b components respectively + */ + public void setColor(int[] newColor){ + this.color = newColor; + } + + /** + * @return a 4 element int array containing th alpha, r, g ,and b components respectively + */ + public int[] getColor(){ + return color; + } + + public void draw(PApplet proc){ + proc.stroke(color[1], color[2], color[3], color[0]); + proc.circle(position.x, position.y, 20); + } +} \ No newline at end of file diff --git a/src/Graph/Vertex.java b/src/Graph/Vertex.java new file mode 100644 index 0000000..4bb5bc0 --- /dev/null +++ b/src/Graph/Vertex.java @@ -0,0 +1,45 @@ +package Graph; + +public class Vertex { + private String vertexLabel; + private boolean isVisitedStatus = false; + + /** + * Create a new vertex with a default label + */ + public Vertex(){ + this.vertexLabel = "Unassigned"; + } + + /** + * @param label create a new vertex with this label + */ + public Vertex(String label){ + this.vertexLabel = label; + } + + /** + * @return the label of the vertex + */ + public String getLabel(){ + return vertexLabel; + } + + public void setLabel(String newLabel){ + vertexLabel = newLabel; + } + + /** + * @return if the node has been visited or not + */ + public boolean visitedStatus(){ + return this.isVisitedStatus; + } + + /** + * @param status set the visited status to true or false + */ + public void setVisitedStatus(boolean status){ + this.isVisitedStatus = status; + } +} \ No newline at end of file diff --git a/src/Processing.java b/src/Processing.java index 19a4be4..ddf5701 100644 --- a/src/Processing.java +++ b/src/Processing.java @@ -1,3 +1,4 @@ +import Vector.Vector; import processing.core.PApplet; import java.util.ArrayList; @@ -16,7 +17,7 @@ public class Processing extends PApplet { processing = this; car = new Car(processing, 100,100,50,40); size(1000, 1000); - car.addView(180,90); + 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); @@ -24,10 +25,16 @@ public class Processing extends PApplet { } public void draw(){ background(0); - for(Wall object : objects){ - object.drawWall(); - } +// 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); + } strokeWeight(2); stroke(255); //car.drive(new int[] {0, 0}); diff --git a/src/Ray.java b/src/Ray.java index 3eb0b35..dd2776a 100644 --- a/src/Ray.java +++ b/src/Ray.java @@ -1,11 +1,12 @@ +import Vector.*; + import processing.core.PApplet; -import processing.core.PVector; import java.util.ArrayList; import static processing.core.PApplet.*; -public class Ray extends Line{ +public class Ray extends Line { float maxRayDistance = 1000; int[] color = new int[]{255, 255, 255}; //takes the starting position of the ray, the length of the ray, and it's casting angle (radians) @@ -33,9 +34,9 @@ public class Ray extends Line{ // get the necessary vectors for two parameterized lines // parameterized lines are of the form L = d*t + p Vector d1 = this.direction.normalize().mul(maxRayDistance); - Vector d2 = wall.direction; + Vector d2 = wall.getDirection(); Vector p1 = this.position; - Vector p2 = wall.position; + Vector p2 = wall.getPosition(); // calculate the parameters for the intersection t and u float t = -(d2.x*(p2.y-p1.y) + d2.y*(p1.x-p2.x))/(d1.x*d2.y - d2.x*d1.y); @@ -72,13 +73,13 @@ public class Ray extends Line{ public boolean hasCollided(){ - return this.direction.mag() != maxRayDistance; + return abs(this.direction.mag() - maxRayDistance) > 0.001; } //returns the absolute position of the point public Vector getPoint(){ if(this.direction.mag() == 0){ - return new Vector(); + return this.position; } return this.position.add(this.direction); diff --git a/src/SLAM.java b/src/SLAM.java index 3c5b443..1516a7b 100644 --- a/src/SLAM.java +++ b/src/SLAM.java @@ -1,3 +1,4 @@ +import Vector.*; import processing.core.*; import java.util.ArrayList; @@ -99,10 +100,4 @@ public class SLAM{ } - public void drawLines(){ - for(Line line : lines){ - line.draw(proc); - } - } - } \ No newline at end of file diff --git a/src/Line.java b/src/Vector/Line.java similarity index 90% rename from src/Line.java rename to src/Vector/Line.java index 3068e95..d5b2815 100644 --- a/src/Line.java +++ b/src/Vector/Line.java @@ -1,16 +1,19 @@ +package Vector; + +import Vector.Vector; import processing.core.PApplet; import java.util.List; import static processing.core.PApplet.*; -public class Line{ +public class Line implements LineInterface{ // vector which represents the direction and length of the line from its starting position protected Vector direction = new Vector(0,0); // store the starting position of the line protected Vector position = new Vector(0,0); - Line(Vector startPosition, Vector endPosition){ + public Line(Vector startPosition, Vector endPosition){ direction = endPosition.sub(startPosition); position = startPosition; } @@ -19,7 +22,7 @@ public class Line{ * attempt to find the line of best fit for the given points * @param points the points to get the line of best for */ - Line(List points){ + public Line(List points){ bestFit(points); } @@ -75,11 +78,6 @@ public class Line{ return direction.mag(); } - public void draw(PApplet screen){ - Vector endPoint = this.position.add(this.direction); - screen.line(position.x, position.y, endPoint.x, endPoint.y); - } - public float getAngle(){return atan2(this.direction.y, this.direction.x);} public Vector endPoint(){ @@ -93,4 +91,8 @@ public class Line{ public float getDistance(Vector point){ return (point.sub(position).cross(direction)).mag() / direction.mag(); } + + public void draw(PApplet proc){ + proc.line(position.x, position.y, endPoint().x, endPoint().y); + } } \ No newline at end of file diff --git a/src/Vector/LineInterface.java b/src/Vector/LineInterface.java new file mode 100644 index 0000000..45d289b --- /dev/null +++ b/src/Vector/LineInterface.java @@ -0,0 +1,20 @@ +package Vector; + +import processing.core.PApplet; + +public interface LineInterface { + Vector getDirection(); + + Vector getPosition(); + + float getLength(); + + float getAngle(); + + Vector endPoint(); + + float getDistance(Vector point); + + void draw(PApplet proc); + +} diff --git a/src/Vector.java b/src/Vector/Vector.java similarity index 70% rename from src/Vector.java rename to src/Vector/Vector.java index 0d859d7..304216a 100644 --- a/src/Vector.java +++ b/src/Vector/Vector.java @@ -1,3 +1,5 @@ +package Vector; + import static java.lang.Math.*; public class Vector { @@ -6,62 +8,62 @@ public class Vector { public float z = 0; Vector(){} - Vector(float x, float y){ + public Vector(float x, float y){ this.x = x; this.y = y; } - Vector(float x, float y, float z){ + public Vector(float x, float y, float z){ this.x = x; this.y = y; this.z = z; } - Vector add(Vector other){ + public Vector add(Vector other){ return new Vector(this.x + other.x, this.y + other.y, this.z + other.z); } - Vector add(float x, float y){ + public Vector add(float x, float y){ return new Vector(this.x + x, this.y + y); } - Vector add(float x, float y, float z){ + public Vector add(float x, float y, float z){ return new Vector(this.x + x, this.y + y, this.z + z); } - Vector sub(Vector other){ + public Vector sub(Vector other){ return new Vector(this.x - other.x, this.y - other.y, this.z - other.z); } - Vector sub(float x, float y){ + public Vector sub(float x, float y){ return new Vector(this.x - x, this.y - y); } - Vector sub(float x, float y, float z){ + public Vector sub(float x, float y, float z){ return new Vector(this.x - x, this.y - y, this.z - z); } - Vector mul(float scalar){ + public Vector mul(float scalar){ return new Vector(this.x * scalar, this.y * scalar, this.z * scalar); } - Vector div(float scalar){ + public Vector div(float scalar){ return mul(1/scalar); } - float mag(){ + public float mag(){ return (float)sqrt(x*x + y*y + z*z); } - float dot(Vector other){ + public float dot(Vector other){ return x * other.x + y * other.y + z * other.z; } - Vector cross(Vector other){ + public Vector cross(Vector other){ return new Vector(this.y*other.z - this.z*other.y, this.z*other.x - this.x*other.z, this.x*other.y - this.y*other.x); } - Vector normalize(){ + public Vector normalize(){ float mag = this.mag(); return new Vector(x / mag, y / mag, z / mag); } @@ -70,14 +72,14 @@ public class Vector { * @param other * @return */ - float angleDiff(Vector other){ + public float angleDiff(Vector other){ float dot = this.dot(other); // dot product float det = this.x*other.y - this.y*other.x; // determinant float angle = (float) atan2(det, dot); // atan2(y, x) or atan2(sin, cos) return angle; } - float angle(){ + public float angle(){ return (float) atan2(y, x); } } diff --git a/src/View.java b/src/View.java index df3bbf7..ff95b9b 100644 --- a/src/View.java +++ b/src/View.java @@ -1,8 +1,6 @@ +import Vector.Vector; import processing.core.*; import java.util.ArrayList; -import java.util.Objects; - -import static processing.core.PApplet.*; public class View { Vector pose; @@ -35,7 +33,9 @@ public class View { public void look(ArrayList walls) { for (Ray ray : rays) { ray.castRay(walls); - ray.drawRay(proc); + if(ray.hasCollided()){ + ray.drawRay(proc); + } } } @@ -80,8 +80,7 @@ public class View { ArrayList points = new ArrayList<>(); for (Ray ray : rays) { - if (!Objects.equals(ray.getPoint(), new Vector(0, 0) { - })) { + if (ray.hasCollided()){ points.add(ray.getPoint()); } } diff --git a/src/Wall.java b/src/Wall.java index 065e970..64dae15 100644 --- a/src/Wall.java +++ b/src/Wall.java @@ -1,8 +1,9 @@ +import Vector.*; import processing.core.*; import static processing.core.PApplet.*; -public class Wall extends Line{ +public class Wall extends Line { int r; int g; int b; diff --git a/tests/LineTest.java b/tests/LineTest.java index 4101fb2..91205bb 100644 --- a/tests/LineTest.java +++ b/tests/LineTest.java @@ -1,8 +1,8 @@ +import Vector.Vector; +import Vector.Line; import org.junit.jupiter.api.Test; -import processing.core.PApplet; import java.util.ArrayList; -import java.util.List; import java.util.Random; import static java.lang.Math.abs; @@ -83,20 +83,4 @@ class LineTest{ assertFloatEquals(v1.y, line.getPosition().y); } - @Test - public void testAngle(){ - Line ray = new Line(new Vector(680, 560), new Vector(680+116, 560+1)); - Line wall = new Line(new Vector(675, 587), new Vector(675-114, 587-29)); - - - float angle1 = wall.position.sub(ray.position).angle(); - float angle2 = wall.endPoint().sub(ray.position).angle(); - float rayAngle = ray.direction.angle(); - - System.out.println("Angle 1: " + String.valueOf(angle1) + ", Angle 2: " + String.valueOf(angle2) + ", Ray Angle: " + String.valueOf(rayAngle)); - assertFloatEquals(1.753907144057f, angle1, 0.001f); - assertFloatEquals(3.1583977941048f, angle2, 0.001f); - assertFloatEquals(0.0086204761121f, rayAngle, 0.001f); - } - } \ No newline at end of file diff --git a/tests/VectorTest.java b/tests/VectorTest.java index e677370..c159efd 100644 --- a/tests/VectorTest.java +++ b/tests/VectorTest.java @@ -1,5 +1,5 @@ +import Vector.Vector; import org.junit.jupiter.api.Test; -import processing.core.PApplet; import static java.lang.Math.*; import static org.junit.jupiter.api.Assertions.*; @@ -28,8 +28,8 @@ class VectorTest{ assertFloatEquals((float)sqrt(x2*x2 + y2*y2), v2.mag()); // test dot product - assertFloatEquals((float)(x1*x2+y1*y2), v1.dot(v2)); - assertFloatEquals((float)(x1*x2+y1*y2), v2.dot(v1)); + assertFloatEquals((x1*x2+y1*y2), v1.dot(v2)); + assertFloatEquals((x1*x2+y1*y2), v2.dot(v1)); // test addition Vector vSum = v1.add(v2);