Compare commits
1 Commits
interactiv
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9d9f50a006 |
124
.idea/uiDesigner.xml
generated
124
.idea/uiDesigner.xml
generated
@@ -1,124 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project version="4">
|
|
||||||
<component name="Palette2">
|
|
||||||
<group name="Swing">
|
|
||||||
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
|
||||||
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
|
|
||||||
</item>
|
|
||||||
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
|
||||||
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
|
|
||||||
</item>
|
|
||||||
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
|
||||||
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
|
|
||||||
</item>
|
|
||||||
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.svg" removable="false" auto-create-binding="false" can-attach-label="true">
|
|
||||||
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
|
|
||||||
</item>
|
|
||||||
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
|
||||||
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
|
|
||||||
<initial-values>
|
|
||||||
<property name="text" value="Button" />
|
|
||||||
</initial-values>
|
|
||||||
</item>
|
|
||||||
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
|
||||||
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
|
|
||||||
<initial-values>
|
|
||||||
<property name="text" value="RadioButton" />
|
|
||||||
</initial-values>
|
|
||||||
</item>
|
|
||||||
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
|
||||||
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
|
|
||||||
<initial-values>
|
|
||||||
<property name="text" value="CheckBox" />
|
|
||||||
</initial-values>
|
|
||||||
</item>
|
|
||||||
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
|
||||||
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
|
|
||||||
<initial-values>
|
|
||||||
<property name="text" value="Label" />
|
|
||||||
</initial-values>
|
|
||||||
</item>
|
|
||||||
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
|
||||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
|
||||||
<preferred-size width="150" height="-1" />
|
|
||||||
</default-constraints>
|
|
||||||
</item>
|
|
||||||
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
|
||||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
|
||||||
<preferred-size width="150" height="-1" />
|
|
||||||
</default-constraints>
|
|
||||||
</item>
|
|
||||||
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
|
||||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
|
||||||
<preferred-size width="150" height="-1" />
|
|
||||||
</default-constraints>
|
|
||||||
</item>
|
|
||||||
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
|
||||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
|
||||||
<preferred-size width="150" height="50" />
|
|
||||||
</default-constraints>
|
|
||||||
</item>
|
|
||||||
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
|
||||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
|
||||||
<preferred-size width="150" height="50" />
|
|
||||||
</default-constraints>
|
|
||||||
</item>
|
|
||||||
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
|
||||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
|
||||||
<preferred-size width="150" height="50" />
|
|
||||||
</default-constraints>
|
|
||||||
</item>
|
|
||||||
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
|
||||||
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
|
|
||||||
</item>
|
|
||||||
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
|
||||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
|
||||||
<preferred-size width="150" height="50" />
|
|
||||||
</default-constraints>
|
|
||||||
</item>
|
|
||||||
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
|
||||||
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
|
|
||||||
<preferred-size width="150" height="50" />
|
|
||||||
</default-constraints>
|
|
||||||
</item>
|
|
||||||
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
|
||||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
|
||||||
<preferred-size width="150" height="50" />
|
|
||||||
</default-constraints>
|
|
||||||
</item>
|
|
||||||
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
|
||||||
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
|
|
||||||
<preferred-size width="200" height="200" />
|
|
||||||
</default-constraints>
|
|
||||||
</item>
|
|
||||||
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
|
||||||
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
|
|
||||||
<preferred-size width="200" height="200" />
|
|
||||||
</default-constraints>
|
|
||||||
</item>
|
|
||||||
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
|
||||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
|
|
||||||
</item>
|
|
||||||
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
|
||||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
|
|
||||||
</item>
|
|
||||||
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
|
||||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
|
|
||||||
</item>
|
|
||||||
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
|
||||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
|
|
||||||
</item>
|
|
||||||
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
|
||||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
|
|
||||||
<preferred-size width="-1" height="20" />
|
|
||||||
</default-constraints>
|
|
||||||
</item>
|
|
||||||
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
|
||||||
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
|
|
||||||
</item>
|
|
||||||
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
|
||||||
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
|
|
||||||
</item>
|
|
||||||
</group>
|
|
||||||
</component>
|
|
||||||
</project>
|
|
||||||
2
README.md
Normal file
2
README.md
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
# SLAM-Sim
|
||||||
|
SImulate SLAM algorithm using java and processing for visualizaiton.
|
||||||
79
map.txt
79
map.txt
@@ -1,79 +0,0 @@
|
|||||||
numVerts,43
|
|
||||||
numEdges,34
|
|
||||||
vert,601.0,491.0
|
|
||||||
vert,602.0,652.0
|
|
||||||
vert,300.0,277.0
|
|
||||||
vert,213.0,812.0
|
|
||||||
vert,715.0,655.0
|
|
||||||
vert,294.0,378.0
|
|
||||||
vert,259.0,476.0
|
|
||||||
vert,224.0,247.0
|
|
||||||
vert,304.0,479.0
|
|
||||||
vert,485.0,693.0
|
|
||||||
vert,713.0,810.0
|
|
||||||
vert,367.0,347.0
|
|
||||||
vert,223.0,273.0
|
|
||||||
vert,404.0,666.0
|
|
||||||
vert,329.0,345.0
|
|
||||||
vert,482.0,615.0
|
|
||||||
vert,606.0,811.0
|
|
||||||
vert,483.0,810.0
|
|
||||||
vert,267.0,616.0
|
|
||||||
vert,603.0,537.0
|
|
||||||
vert,330.0,376.0
|
|
||||||
vert,267.0,723.0
|
|
||||||
vert,714.0,488.0
|
|
||||||
vert,401.0,616.0
|
|
||||||
vert,294.0,347.0
|
|
||||||
vert,206.0,522.0
|
|
||||||
vert,711.0,234.0
|
|
||||||
vert,493.0,238.0
|
|
||||||
vert,302.0,249.0
|
|
||||||
vert,403.0,483.0
|
|
||||||
vert,334.0,727.0
|
|
||||||
vert,168.0,471.0
|
|
||||||
vert,487.0,485.0
|
|
||||||
vert,327.0,615.0
|
|
||||||
vert,405.0,237.0
|
|
||||||
vert,211.0,346.0
|
|
||||||
vert,210.0,380.0
|
|
||||||
vert,165.0,235.0
|
|
||||||
vert,411.0,811.0
|
|
||||||
vert,165.0,809.0
|
|
||||||
vert,713.0,539.0
|
|
||||||
vert,367.0,375.0
|
|
||||||
vert,165.0,521.0
|
|
||||||
edge,start,1,end,4
|
|
||||||
edge,start,1,end,16
|
|
||||||
edge,start,3,end,25
|
|
||||||
edge,start,9,end,17
|
|
||||||
edge,start,10,end,26
|
|
||||||
edge,start,11,end,14
|
|
||||||
edge,start,11,end,41
|
|
||||||
edge,end,7,start,12
|
|
||||||
edge,end,2,start,12
|
|
||||||
edge,start,13,end,38
|
|
||||||
edge,end,0,start,19
|
|
||||||
edge,start,19,end,40
|
|
||||||
edge,end,14,start,20
|
|
||||||
edge,start,20,end,41
|
|
||||||
edge,end,18,start,21
|
|
||||||
edge,start,21,end,30
|
|
||||||
edge,start,24,end,35
|
|
||||||
edge,end,5,start,24
|
|
||||||
edge,start,26,end,27
|
|
||||||
edge,end,7,start,28
|
|
||||||
edge,end,2,start,28
|
|
||||||
edge,end,23,start,29
|
|
||||||
edge,end,8,start,29
|
|
||||||
edge,end,6,start,31
|
|
||||||
edge,end,15,start,32
|
|
||||||
edge,end,22,start,32
|
|
||||||
edge,end,18,start,33
|
|
||||||
edge,end,30,start,33
|
|
||||||
edge,start,34,end,37
|
|
||||||
edge,end,35,start,36
|
|
||||||
edge,end,5,start,36
|
|
||||||
edge,start,37,end,39
|
|
||||||
edge,end,10,start,39
|
|
||||||
edge,end,25,start,42
|
|
||||||
15
src/Car.java
15
src/Car.java
@@ -3,9 +3,6 @@ import java.util.ArrayList;
|
|||||||
import static java.lang.Math.PI;
|
import static java.lang.Math.PI;
|
||||||
import static processing.core.PApplet.degrees;
|
import static processing.core.PApplet.degrees;
|
||||||
import static processing.core.PApplet.radians;
|
import static processing.core.PApplet.radians;
|
||||||
|
|
||||||
import Graph.*;
|
|
||||||
import Vector.Vector;
|
|
||||||
import processing.core.PApplet;
|
import processing.core.PApplet;
|
||||||
|
|
||||||
public class Car{
|
public class Car{
|
||||||
@@ -38,22 +35,22 @@ public class Car{
|
|||||||
}
|
}
|
||||||
|
|
||||||
//draw the car and its views
|
//draw the car and its views
|
||||||
public void drawCar(PointGraph g){
|
public void drawCar(ArrayList<Wall> walls){
|
||||||
proc.stroke(255);
|
proc.stroke(255);
|
||||||
proc.ellipse(pose.x, pose.y, carWidth, carLength);
|
proc.ellipse(pose.x, pose.y, carWidth, carLength);
|
||||||
this.updateScan(g);
|
this.updateScan(walls);
|
||||||
// this.slam.drawLines();
|
this.slam.drawLines();
|
||||||
}
|
}
|
||||||
|
|
||||||
//With all the views that the car has, get their point list
|
//With all the views that the car has, get their point list
|
||||||
void updateScan(PointGraph map){
|
void updateScan(ArrayList<Wall> walls){
|
||||||
for(View view : views){
|
for(View view : views){
|
||||||
view.look(map);
|
view.look(walls);
|
||||||
}
|
}
|
||||||
|
|
||||||
for(View view : views){
|
for(View view : views){
|
||||||
ArrayList<Vector> pointList = view.getPoints();
|
ArrayList<Vector> pointList = view.getPoints();
|
||||||
// slam.RANSAC(pointList, view.getFOV() / view.getRayNum());
|
slam.RANSAC(pointList, view.getFOV() / view.getRayNum());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,59 +0,0 @@
|
|||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,225 +0,0 @@
|
|||||||
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<Vertex, LinkedList<Edge>>(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<Edge>());
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @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<Vertex> depthFirstTraverse(Vertex v){
|
|
||||||
ArrayList<Vertex> vertexList = new ArrayList<Vertex>();
|
|
||||||
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<Vertex> vertexList){
|
|
||||||
vertexList.add(v);
|
|
||||||
v.setVisitedStatus(true);
|
|
||||||
LinkedList<Edge> 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<Vertex> 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<Vertex> 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<Vertex> breadthFirstSearch(Vertex v){
|
|
||||||
ArrayList<Vertex> queue = new ArrayList<>();
|
|
||||||
ArrayList<Vertex> 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<Edge> 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<Edge> edgesToRemove = new ArrayList<>();
|
|
||||||
ArrayList<Vertex> 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<Vertex, LinkedList<Edge>> adjList;
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,53 +0,0 @@
|
|||||||
package Graph;
|
|
||||||
|
|
||||||
import Vector.*;
|
|
||||||
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;
|
|
||||||
protected Line line;
|
|
||||||
public LineEdge(PointVertex vStart, PointVertex vEnd) {
|
|
||||||
super(vStart, 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);
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,152 +0,0 @@
|
|||||||
package Graph;
|
|
||||||
|
|
||||||
import Vector.Vector;
|
|
||||||
import processing.core.PApplet;
|
|
||||||
|
|
||||||
import java.io.File; // Import the File class
|
|
||||||
import java.io.FileWriter;
|
|
||||||
import java.io.IOException; // Import the IOException class to handle errors
|
|
||||||
|
|
||||||
import static java.lang.Math.pow;
|
|
||||||
import static java.lang.Math.sqrt;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
|
|
||||||
|
|
||||||
public class PointGraph extends Graph {
|
|
||||||
PointVertex selectedVertex;
|
|
||||||
public 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
*/
|
|
||||||
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(Vector point){
|
|
||||||
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 = p2.sub(point).mag();
|
|
||||||
if(dist < closestDist || closestDist == -1){
|
|
||||||
closestDist = dist;
|
|
||||||
closestVertex = v1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return closestVertex;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void removeVertex(PointVertex v){
|
|
||||||
if(selectedVertex == v){
|
|
||||||
selectedVertex = null;
|
|
||||||
}
|
|
||||||
super.removeVertex(v);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return all edges in the graph
|
|
||||||
*/
|
|
||||||
public ArrayList<LineEdge> getAllEdges(){
|
|
||||||
ArrayList<LineEdge> 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<PointVertex> getAllVertexes(){
|
|
||||||
ArrayList<PointVertex> points = new ArrayList<>();
|
|
||||||
for(Vertex v : adjList.keySet()){
|
|
||||||
PointVertex v1 = (PointVertex) v;
|
|
||||||
points.add(v1);
|
|
||||||
}
|
|
||||||
return points;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,115 +0,0 @@
|
|||||||
package Graph;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileNotFoundException;
|
|
||||||
import java.io.FileWriter;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Scanner;
|
|
||||||
|
|
||||||
public class PointGraphWriter {
|
|
||||||
public void save(String filename, PointGraph g) throws IOException {
|
|
||||||
FileWriter file = new FileWriter(filename);
|
|
||||||
|
|
||||||
file.write("numVerts," + g.numVertices());
|
|
||||||
file.write("\nnumEdges," + g.numEdges());
|
|
||||||
|
|
||||||
// turn the hash map into something linear
|
|
||||||
ArrayList<PointVertex> verts = g.getAllVertexes();
|
|
||||||
ArrayList<LineEdge> edges = g.getAllEdges();
|
|
||||||
|
|
||||||
// save the vertexes
|
|
||||||
int countVerts = 0;
|
|
||||||
for(PointVertex v : verts){
|
|
||||||
// save the vertex position
|
|
||||||
file.write("\nvert,"+v.getPos().x+","+v.getPos().y);
|
|
||||||
countVerts++;
|
|
||||||
}
|
|
||||||
|
|
||||||
// save the edges
|
|
||||||
for(Edge e : edges){
|
|
||||||
int idx = 0;
|
|
||||||
file.write("\nedge,");
|
|
||||||
boolean otherIsWritten = false;
|
|
||||||
for(PointVertex v : verts){
|
|
||||||
if(e.getStartVertex() == (Vertex)v){
|
|
||||||
file.write("start,"+ idx);
|
|
||||||
if(!otherIsWritten){
|
|
||||||
file.write(",");
|
|
||||||
otherIsWritten = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if(e.getEndVertex() == (Vertex)v){
|
|
||||||
file.write("end," + idx);
|
|
||||||
if(!otherIsWritten){
|
|
||||||
file.write(",");
|
|
||||||
otherIsWritten = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
idx++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
file.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
public PointGraph loadFile(String filename) throws FileNotFoundException, NumberFormatException {
|
|
||||||
PointGraph g = new PointGraph();
|
|
||||||
File file = new File(filename);
|
|
||||||
Scanner reader = new Scanner(file);
|
|
||||||
ArrayList<PointVertex> vertices = new ArrayList<>();
|
|
||||||
while(reader.hasNextLine()){
|
|
||||||
String line = reader.nextLine();
|
|
||||||
ArrayList<String> args = parseLine(line);
|
|
||||||
String key = args.get(0);
|
|
||||||
switch (key) {
|
|
||||||
case "numVerts" -> System.out.println("Number of Vertexes: " + Integer.parseInt(args.get(1)));
|
|
||||||
case "numEdges" -> System.out.println("Number of Edges: " + Integer.parseInt(args.get(1)));
|
|
||||||
case "vert" -> {
|
|
||||||
float x = Float.parseFloat(args.get(1));
|
|
||||||
float y = Float.parseFloat(args.get(2));
|
|
||||||
PointVertex v = new PointVertex(x, y);
|
|
||||||
g.addVertex(v);
|
|
||||||
vertices.add(v);
|
|
||||||
}
|
|
||||||
case "edge" -> {
|
|
||||||
int startIdx;
|
|
||||||
int endIdx;
|
|
||||||
if (args.get(1).contains("start")) {
|
|
||||||
startIdx = Integer.parseInt(args.get(2));
|
|
||||||
endIdx = Integer.parseInt(args.get(4));
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
startIdx = Integer.parseInt(args.get(4));
|
|
||||||
endIdx = Integer.parseInt(args.get(2));
|
|
||||||
}
|
|
||||||
g.addEdge(vertices.get(startIdx), vertices.get(endIdx));
|
|
||||||
}
|
|
||||||
default -> System.out.println("Unrecognized Line: " + line);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return g;
|
|
||||||
}
|
|
||||||
|
|
||||||
private ArrayList<String> parseLine(String line){
|
|
||||||
ArrayList<String> args = new ArrayList<>();
|
|
||||||
StringBuilder arg = new StringBuilder();
|
|
||||||
|
|
||||||
for(char letter : line.toCharArray()){
|
|
||||||
if(letter == ','){
|
|
||||||
args.add(arg.toString());
|
|
||||||
arg = new StringBuilder();
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if(letter == '\n'){
|
|
||||||
args.add(arg.toString());
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
arg.append(letter);
|
|
||||||
}
|
|
||||||
if(!args.get(args.size()-1).contains(arg.toString())){
|
|
||||||
args.add(arg.toString());
|
|
||||||
}
|
|
||||||
return args;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,65 +0,0 @@
|
|||||||
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
|
|
||||||
*/
|
|
||||||
public PointVertex(float xPos, float yPos){
|
|
||||||
this.position = new Vector(xPos, yPos);
|
|
||||||
}
|
|
||||||
|
|
||||||
public PointVertex(Vector position){
|
|
||||||
super();
|
|
||||||
this.position = position;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,45 +0,0 @@
|
|||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,19 +1,14 @@
|
|||||||
package Vector;
|
|
||||||
|
|
||||||
import Vector.Vector;
|
|
||||||
import processing.core.PApplet;
|
import processing.core.PApplet;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static processing.core.PApplet.*;
|
import static processing.core.PApplet.*;
|
||||||
|
|
||||||
public class Line implements LineInterface{
|
public class Line{
|
||||||
// vector which represents the direction and length of the line from its starting position
|
private Vector direction = new Vector(0,0);
|
||||||
protected Vector direction = new Vector(0,0);
|
private Vector position = new Vector(0,0);
|
||||||
// store the starting position of the line
|
|
||||||
protected Vector position = new Vector(0,0);
|
|
||||||
|
|
||||||
public Line(Vector startPosition, Vector endPosition){
|
Line(Vector startPosition, Vector endPosition){
|
||||||
direction = endPosition.sub(startPosition);
|
direction = endPosition.sub(startPosition);
|
||||||
position = startPosition;
|
position = startPosition;
|
||||||
}
|
}
|
||||||
@@ -22,7 +17,7 @@ public class Line implements LineInterface{
|
|||||||
* attempt to find the line of best fit for the given points
|
* attempt to find the line of best fit for the given points
|
||||||
* @param points the points to get the line of best for
|
* @param points the points to get the line of best for
|
||||||
*/
|
*/
|
||||||
public Line(List<Vector> points){
|
Line(List<Vector> points){
|
||||||
bestFit(points);
|
bestFit(points);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,10 +73,9 @@ public class Line implements LineInterface{
|
|||||||
return direction.mag();
|
return direction.mag();
|
||||||
}
|
}
|
||||||
|
|
||||||
public float getAngle(){return atan2(this.direction.y, this.direction.x);}
|
public void draw(PApplet screen){
|
||||||
|
Vector endPoint = this.position.add(this.direction);
|
||||||
public Vector endPoint(){
|
screen.line(position.x, position.y, endPoint.x, endPoint.y);
|
||||||
return this.position.add(this.direction);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -91,8 +85,4 @@ public class Line implements LineInterface{
|
|||||||
public float getDistance(Vector point){
|
public float getDistance(Vector point){
|
||||||
return (point.sub(position).cross(direction)).mag() / direction.mag();
|
return (point.sub(position).cross(direction)).mag() / direction.mag();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void draw(PApplet proc){
|
|
||||||
proc.line(position.x, position.y, endPoint().x, endPoint().y);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1,17 +1,13 @@
|
|||||||
import Graph.*;
|
|
||||||
import Vector.Vector;
|
|
||||||
import processing.core.PApplet;
|
import processing.core.PApplet;
|
||||||
|
|
||||||
import java.io.FileNotFoundException;
|
import java.util.ArrayList;
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
|
|
||||||
public class Processing extends PApplet {
|
public class Processing extends PApplet {
|
||||||
|
|
||||||
Car car;
|
Car car;
|
||||||
public static PApplet processing;
|
ArrayList<Wall> objects = new ArrayList<>();
|
||||||
|
|
||||||
PointGraph map = new PointGraph();
|
public static PApplet processing;
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
PApplet.main("Processing");
|
PApplet.main("Processing");
|
||||||
@@ -21,36 +17,33 @@ public class Processing extends PApplet {
|
|||||||
processing = this;
|
processing = this;
|
||||||
car = new Car(processing, 100,100,50,40);
|
car = new Car(processing, 100,100,50,40);
|
||||||
size(1000, 1000);
|
size(1000, 1000);
|
||||||
car.addView(360,180);
|
car.addView(180,180);
|
||||||
|
for(int i = 0; i < 20; i++){
|
||||||
// for(int i = 0; i < 10; i++){
|
Wall wall = new Wall(processing, new Vector((int)random(40, 1840), (int)random(40, 960)), (int)random(360), (int)random(100, 1000));
|
||||||
// PointVertex vStart = new PointVertex(random(50, 950), random(50, 950));
|
objects.add(wall);
|
||||||
// PointVertex vEnd = new PointVertex(random(50, 950), random(50, 950));
|
}
|
||||||
// map.addEdge(vStart, vEnd);
|
|
||||||
// }
|
|
||||||
|
|
||||||
}
|
}
|
||||||
public void draw(){
|
public void draw(){
|
||||||
background(0);
|
background(0);
|
||||||
map.draw(processing);
|
// for(Wall object : objects){
|
||||||
car.drawCar(map);
|
// object.drawWall();
|
||||||
strokeWeight(2);
|
// }
|
||||||
stroke(255);
|
car.drawCar(objects);
|
||||||
//car.drive(new int[] {0, 0});
|
//car.drive(new int[] {0, 0});
|
||||||
}
|
}
|
||||||
|
|
||||||
public void keyPressed(){
|
public void keyPressed(){
|
||||||
if(key == 'd'){
|
if(key == 'd'){
|
||||||
car.setPose(car.getPose().add(10, 0));
|
car.setPose(car.getPose().add(1, 0));
|
||||||
}
|
}
|
||||||
if(key == 'w'){
|
if(key == 'w'){
|
||||||
car.setPose(car.getPose().add(0, -10));
|
car.setPose(car.getPose().add(0, -1));
|
||||||
}
|
}
|
||||||
if(key == 'a'){
|
if(key == 'a'){
|
||||||
car.setPose(car.getPose().add(-10, 0));
|
car.setPose(car.getPose().add(-1, 0));
|
||||||
}
|
}
|
||||||
if(key == 's'){
|
if(key == 's'){
|
||||||
car.setPose(car.getPose().add(0, 10));
|
car.setPose(car.getPose().add(0, 1));
|
||||||
}
|
}
|
||||||
if(key == 'q'){
|
if(key == 'q'){
|
||||||
car.setAngle(car.getAngle()+1);
|
car.setAngle(car.getAngle()+1);
|
||||||
@@ -58,64 +51,7 @@ public class Processing extends PApplet {
|
|||||||
if(key == 'e'){
|
if(key == 'e'){
|
||||||
car.setAngle(car.getAngle()-1);
|
car.setAngle(car.getAngle()-1);
|
||||||
}
|
}
|
||||||
if(key == DELETE && map.vertexIsSelected()){
|
|
||||||
map.removeVertex(map.getSelectedVertex());
|
|
||||||
}
|
|
||||||
if(key == ' ' && map.vertexIsSelected()){
|
|
||||||
map.deselectVertex();
|
|
||||||
}
|
|
||||||
if(key == ESC){
|
|
||||||
System.out.println("Attempting to save map to file.");
|
|
||||||
try{
|
|
||||||
PointGraphWriter writer = new PointGraphWriter();
|
|
||||||
writer.save("map.txt", map);
|
|
||||||
}
|
|
||||||
catch(IOException e){
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(key == 'l'){
|
|
||||||
System.out.println("Attempting to load a map from file");
|
|
||||||
try{
|
|
||||||
PointGraphWriter writer = new PointGraphWriter();
|
|
||||||
map = writer.loadFile("map.txt");
|
|
||||||
}
|
|
||||||
catch (FileNotFoundException e){
|
|
||||||
System.out.println("File not found");
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
catch (NumberFormatException e){
|
|
||||||
System.out.println("Number format incorrect");
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void mousePressed(){
|
|
||||||
Vector clickPosition = new Vector(mouseX, mouseY);
|
|
||||||
|
|
||||||
if(map.numVertices() == 0){
|
|
||||||
PointVertex v = new PointVertex(clickPosition);
|
|
||||||
map.addVertex(v);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
PointVertex closestVertex = map.getClosestVertex(clickPosition);
|
|
||||||
float distance = closestVertex.getPos().sub(clickPosition).mag();
|
|
||||||
if(distance < 15){
|
|
||||||
if(map.vertexIsSelected()){
|
|
||||||
if(map.getSelectedVertex() == closestVertex){
|
|
||||||
map.deselectVertex();
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
map.addEdge(map.getSelectedVertex(), closestVertex);
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
map.setSelectedVertex(closestVertex);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
PointVertex v = new PointVertex(clickPosition);
|
|
||||||
map.addVertex(v);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
97
src/Ray.java
97
src/Ray.java
@@ -1,97 +0,0 @@
|
|||||||
import Graph.*;
|
|
||||||
import Vector.*;
|
|
||||||
|
|
||||||
import processing.core.PApplet;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
|
|
||||||
import static processing.core.PApplet.*;
|
|
||||||
|
|
||||||
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)
|
|
||||||
Ray(Vector startPosition, float angle){
|
|
||||||
super(startPosition, startPosition.add(new Vector(cos(angle), sin(angle))));
|
|
||||||
direction = direction.mul(maxRayDistance);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void drawRay(PApplet proc){
|
|
||||||
proc.stroke(color[0], color[1], color[2]);
|
|
||||||
proc.line(position.x, position.y, position.x + direction.x, position.y + direction.y);
|
|
||||||
// proc.noFill();
|
|
||||||
// proc.circle(position.x, position.y, 2*direction.mag());
|
|
||||||
// proc.fill(255);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//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(PointGraph map){
|
|
||||||
float shortestWallDistance = maxRayDistance;
|
|
||||||
ArrayList<LineEdge> 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
|
|
||||||
Vector d1 = this.direction.normalize().mul(maxRayDistance);
|
|
||||||
Vector d2 = wall.getDirection();
|
|
||||||
Vector p1 = this.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);
|
|
||||||
float u = -(d1.x*(p2.y-p1.y) + d1.y*(p1.x-p2.x))/(d1.x*d2.y-d2.x*d1.y);
|
|
||||||
|
|
||||||
// the lines will only be intersecting when both t and u are between 0 and 1.
|
|
||||||
if(!(0 <= t && t <= 1 && 0 <= u && u <= 1)){
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// if the distance from the ray to the intersection is shorter than the shortestWallDistance, this is our new closest wall
|
|
||||||
float distance = d1.mul(t).add(p1).sub(this.position).mag();
|
|
||||||
if(distance < shortestWallDistance){
|
|
||||||
shortestWallDistance = distance;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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);
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
this.direction = this.direction.normalize().mul(maxRayDistance);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public Vector getPos(){ return this.position;}
|
|
||||||
|
|
||||||
public float getRayLength(){return this.direction.mag();}
|
|
||||||
|
|
||||||
|
|
||||||
public boolean hasCollided(){
|
|
||||||
return abs(this.direction.mag() - maxRayDistance) > 0.001;
|
|
||||||
}
|
|
||||||
|
|
||||||
//returns the absolute position of the point
|
|
||||||
public Vector getPoint(){
|
|
||||||
if(this.direction.mag() == 0){
|
|
||||||
return this.position;
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.position.add(this.direction);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setPos(Vector newPosition){
|
|
||||||
this.position = newPosition;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setRayLength(int rayLength){this.direction = this.direction.normalize().mul(rayLength);}
|
|
||||||
|
|
||||||
public void setAngle(float angle){
|
|
||||||
float currentAngle = direction.angle();
|
|
||||||
this.direction = direction.rotate2D(angle - currentAngle);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,4 +1,3 @@
|
|||||||
import Vector.*;
|
|
||||||
import processing.core.*;
|
import processing.core.*;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@@ -100,4 +99,10 @@ public class SLAM{
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void drawLines(){
|
||||||
|
for(Line line : lines){
|
||||||
|
line.draw(proc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
74
src/Vector.java
Normal file
74
src/Vector.java
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
import static java.lang.Math.acos;
|
||||||
|
import static java.lang.Math.sqrt;
|
||||||
|
|
||||||
|
public class Vector {
|
||||||
|
public float x = 0;
|
||||||
|
public float y = 0;
|
||||||
|
public float z = 0;
|
||||||
|
|
||||||
|
Vector(){}
|
||||||
|
Vector(float x, float y){
|
||||||
|
this.x = x;
|
||||||
|
this.y = y;
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector(float x, float y, float z){
|
||||||
|
this.x = x;
|
||||||
|
this.y = y;
|
||||||
|
this.z = z;
|
||||||
|
}
|
||||||
|
|
||||||
|
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){
|
||||||
|
return new Vector(this.x + x, this.y + y);
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector add(float x, float y, float z){
|
||||||
|
return new Vector(this.x + x, this.y + y, this.z + z);
|
||||||
|
}
|
||||||
|
|
||||||
|
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){
|
||||||
|
return new Vector(this.x - x, this.y - y);
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector sub(float x, float y, float z){
|
||||||
|
return new Vector(this.x - x, this.y - y, this.z - z);
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector mul(float scalar){
|
||||||
|
return new Vector(this.x * scalar, this.y * scalar, this.z * scalar);
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector div(float scalar){
|
||||||
|
return mul(1/scalar);
|
||||||
|
}
|
||||||
|
|
||||||
|
float mag(){
|
||||||
|
return (float)sqrt(x*x + y*y + z*z);
|
||||||
|
}
|
||||||
|
|
||||||
|
float dot(Vector other){
|
||||||
|
return x * other.x + y * other.y + z * other.z;
|
||||||
|
}
|
||||||
|
|
||||||
|
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(){
|
||||||
|
float mag = this.mag();
|
||||||
|
return new Vector(x / mag, y / mag, z / mag);
|
||||||
|
}
|
||||||
|
|
||||||
|
float angleDiff(Vector other){
|
||||||
|
float dot = this.dot(other);
|
||||||
|
return (float)acos(dot / (this.mag() * other.mag()));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
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);
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,93 +0,0 @@
|
|||||||
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;
|
|
||||||
public float y = 0;
|
|
||||||
public float z = 0;
|
|
||||||
|
|
||||||
Vector(){}
|
|
||||||
public Vector(float x, float y){
|
|
||||||
this.x = x;
|
|
||||||
this.y = y;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Vector(float x, float y, float z){
|
|
||||||
this.x = x;
|
|
||||||
this.y = y;
|
|
||||||
this.z = z;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Vector add(Vector other){
|
|
||||||
return new Vector(this.x + other.x, this.y + other.y, this.z + other.z);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Vector add(float x, float y){
|
|
||||||
return new Vector(this.x + x, this.y + y);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Vector add(float x, float y, float z){
|
|
||||||
return new Vector(this.x + x, this.y + y, this.z + z);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Vector sub(Vector other){
|
|
||||||
return new Vector(this.x - other.x, this.y - other.y, this.z - other.z);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Vector sub(float x, float y){
|
|
||||||
return new Vector(this.x - x, this.y - y);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Vector sub(float x, float y, float z){
|
|
||||||
return new Vector(this.x - x, this.y - y, this.z - z);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Vector mul(float scalar){
|
|
||||||
return new Vector(this.x * scalar, this.y * scalar, this.z * scalar);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Vector div(float scalar){
|
|
||||||
return mul(1/scalar);
|
|
||||||
}
|
|
||||||
|
|
||||||
public float mag(){
|
|
||||||
return (float)sqrt(x*x + y*y + z*z);
|
|
||||||
}
|
|
||||||
|
|
||||||
public float dot(Vector other){
|
|
||||||
return x * other.x + y * other.y + z * other.z;
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Vector normalize(){
|
|
||||||
float mag = this.mag();
|
|
||||||
return new Vector(x / mag, y / mag, z / mag);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param other
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
149
src/View.java
149
src/View.java
@@ -1,9 +1,10 @@
|
|||||||
import Graph.PointGraph;
|
|
||||||
import Vector.Vector;
|
|
||||||
import processing.core.*;
|
import processing.core.*;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
public class View {
|
import static processing.core.PApplet.*;
|
||||||
|
|
||||||
|
public class View{
|
||||||
Vector pose;
|
Vector pose;
|
||||||
float angle = 0;
|
float angle = 0;
|
||||||
float FOV;
|
float FOV;
|
||||||
@@ -11,7 +12,7 @@ public class View {
|
|||||||
private static PApplet proc;
|
private static PApplet proc;
|
||||||
|
|
||||||
//the x,y position of the view, what angle it's looking at and its FOV
|
//the x,y position of the view, what angle it's looking at and its FOV
|
||||||
View(PApplet processing, Vector newPose, int numberOfRays, float FOV) {
|
View(PApplet processing, Vector newPose, int numberOfRays, float FOV){
|
||||||
proc = processing;
|
proc = processing;
|
||||||
this.pose = newPose;
|
this.pose = newPose;
|
||||||
this.FOV = FOV;
|
this.FOV = FOV;
|
||||||
@@ -19,69 +20,58 @@ public class View {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//sets the number of rays and their starting values in the ray list
|
//sets the number of rays and their starting values in the ray list
|
||||||
public void setRayNum(int numberOfRays, float FOV, float angleOffset) {
|
public void setRayNum(int numberOfRays, float FOV, float angleOffset){
|
||||||
float rayStep = FOV / numberOfRays;
|
float rayStep = FOV/numberOfRays;
|
||||||
rays.clear();
|
rays.clear();
|
||||||
float angle = (float) (0.01 - angleOffset); //the 0.01 fixes some bugs
|
float angle = (float)(0.01-angleOffset); //the 0.01 fixes some bugs
|
||||||
for (int i = 0; i < numberOfRays; i++) {
|
for(int i = 0; i < numberOfRays; i++){
|
||||||
Ray ray = new Ray(pose, angle);
|
Ray ray = new Ray(proc, pose, 100000, angle);
|
||||||
angle = angle + rayStep;
|
angle = angle + rayStep;
|
||||||
rays.add(ray);
|
rays.add(ray);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//sees if the ray will collide with the walls in the wall list
|
//sees if the ray will collide with the walls in the wall list
|
||||||
public void look(PointGraph map) {
|
public void look(ArrayList<Wall> walls){
|
||||||
for (Ray ray : rays) {
|
for (Ray ray : rays){
|
||||||
ray.castRay(map);
|
ray.castRay(walls);
|
||||||
if(ray.hasCollided()){
|
ray.drawRay();
|
||||||
ray.drawRay(proc);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//changes the position of the view
|
//changes the position of the view
|
||||||
public void setPos(Vector newPose) {
|
public void setPos(Vector newPose){
|
||||||
pose = newPose;
|
pose = newPose;
|
||||||
for (Ray ray : rays) {
|
for(Ray ray : rays){ray.setPos(pose);}
|
||||||
ray.setPos(pose);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//changes the angle of the view
|
//changes the angle of the view
|
||||||
public void setAngle(float angle) {
|
public void setAngle(float angle){
|
||||||
this.angle = angle;
|
this.angle = angle;
|
||||||
this.setRayNum(rays.size(), this.FOV, angle);
|
this.setRayNum(rays.size(), this.FOV, angle);
|
||||||
}
|
}
|
||||||
|
|
||||||
//changes the field of view of the view
|
//changes the field of view of the view
|
||||||
public void setFOV(float FOV) {
|
public void setFOV(float FOV){
|
||||||
this.FOV = FOV;
|
this.FOV = FOV;
|
||||||
this.setRayNum(this.rays.size(), this.FOV, this.angle);
|
this.setRayNum(this.rays.size(), this.FOV, this.angle);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Vector getPos() {
|
public Vector getPos(){return pose;}
|
||||||
return pose;
|
|
||||||
}
|
|
||||||
|
|
||||||
public float getAngle() {
|
public float getAngle(){return this.angle;}
|
||||||
return this.angle;
|
|
||||||
}
|
|
||||||
|
|
||||||
public float getFOV() {
|
public float getFOV(){return this.FOV;}
|
||||||
return this.FOV;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getRayNum() {
|
public int getRayNum(){return this.rays.size();}
|
||||||
return this.rays.size();
|
|
||||||
}
|
|
||||||
|
|
||||||
//gets the point that each ray has collided with
|
//gets the point that each ray has collided with
|
||||||
public ArrayList<Vector> getPoints() {
|
public ArrayList<Vector> getPoints(){
|
||||||
ArrayList<Vector> points = new ArrayList<>();
|
ArrayList<Vector> points = new ArrayList<>();
|
||||||
|
|
||||||
for (Ray ray : rays) {
|
for(Ray ray : rays){
|
||||||
if (ray.hasCollided()){
|
if(!Objects.equals(ray.getPoint(), new Vector(0, 0) {
|
||||||
|
})){
|
||||||
points.add(ray.getPoint());
|
points.add(ray.getPoint());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -89,3 +79,90 @@ public class View {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class Ray{
|
||||||
|
Vector pose;
|
||||||
|
int rayLength;
|
||||||
|
int defaultRayLength;
|
||||||
|
float angle; // IN RADIANS
|
||||||
|
private static PApplet proc;
|
||||||
|
|
||||||
|
//takes the starting position of the ray, the length of the ray, and it's casting angle (radians)
|
||||||
|
Ray(PApplet processing, Vector position, int defaultRayLength, float angle){
|
||||||
|
proc = processing;
|
||||||
|
this.pose = position;
|
||||||
|
this.defaultRayLength = defaultRayLength;
|
||||||
|
this.rayLength = defaultRayLength;
|
||||||
|
this.angle = angle;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void drawRay(){
|
||||||
|
proc.line(pose.x, pose.y, (pose.x + cos(angle)*rayLength), (pose.y + sin(angle)*rayLength));
|
||||||
|
}
|
||||||
|
|
||||||
|
//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<Wall> objects){
|
||||||
|
this.rayLength = defaultRayLength;
|
||||||
|
ArrayList<Integer> distances = new ArrayList<>();
|
||||||
|
//sees what objects it collides with
|
||||||
|
for(Wall object : objects){
|
||||||
|
float theta1 = angle;
|
||||||
|
float theta2 = radians(object.getAngle());
|
||||||
|
Vector wallPos = object.getPos();
|
||||||
|
|
||||||
|
//finds where along the wall the ray collides
|
||||||
|
float b = (pose.x*sin(theta1) + wallPos.y*cos(theta1) - pose.y*cos(theta1) - wallPos.x*sin(theta1)) / (cos(theta2)*sin(theta1) - sin(theta2)*cos(theta1));
|
||||||
|
|
||||||
|
//if the place along the wall is further away than the wall extends, then it didn't collide
|
||||||
|
if(b < object.getLength() && b > 0){
|
||||||
|
//finds the length of the ray needed to collide with the wall
|
||||||
|
float a = (b*sin(theta2) + wallPos.y-pose.y) / sin(theta1);
|
||||||
|
//add that length to a list
|
||||||
|
if(a > 0){
|
||||||
|
distances.add((int)abs(a));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//finds the shortest distance and sets the length of the ray to that distance
|
||||||
|
if(distances.size() > 0){
|
||||||
|
for(Integer distance : distances){
|
||||||
|
if(distance < rayLength){
|
||||||
|
rayLength = distance;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else this.rayLength = defaultRayLength;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Vector getPos(){ return pose;}
|
||||||
|
|
||||||
|
public int getRayLength(){return this.rayLength;}
|
||||||
|
|
||||||
|
public float getAngle(){return this.angle;}
|
||||||
|
|
||||||
|
public boolean hasCollided(){
|
||||||
|
return this.defaultRayLength != this.rayLength;
|
||||||
|
}
|
||||||
|
|
||||||
|
//returns the absolute position of the point
|
||||||
|
public Vector getPoint(){
|
||||||
|
if(this.rayLength != this.defaultRayLength){
|
||||||
|
return new Vector(rayLength * (int)cos(this.angle) + pose.x, rayLength * (int)sin(this.angle) + pose.y);
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
return new Vector(0,0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPos(Vector newPose){
|
||||||
|
pose = newPose;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRayLength(int rayLength){this.rayLength = rayLength;}
|
||||||
|
|
||||||
|
public void setDefaultRayLength(int defaultRayLength){this.defaultRayLength = defaultRayLength;}
|
||||||
|
|
||||||
|
public void setAngle(float angle){this.angle = angle;}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
41
src/Wall.java
Normal file
41
src/Wall.java
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
import processing.core.*;
|
||||||
|
|
||||||
|
import static processing.core.PApplet.*;
|
||||||
|
|
||||||
|
public class Wall{
|
||||||
|
Vector pos;
|
||||||
|
float angle;
|
||||||
|
int wallLength;
|
||||||
|
private static PApplet proc;
|
||||||
|
int r;
|
||||||
|
int g;
|
||||||
|
int b;
|
||||||
|
|
||||||
|
Wall(PApplet processing, Vector pos, float angle, int wallLength){
|
||||||
|
proc = processing;
|
||||||
|
this.pos = pos;
|
||||||
|
this.angle = angle;
|
||||||
|
this.wallLength = wallLength;
|
||||||
|
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.line(pos.x, pos.y, (pos.x + cos(radians(angle))*wallLength), (pos.y + sin(radians(angle))*wallLength));
|
||||||
|
//ellipse((xPos + cos(radians(angle))*wallLength), (yPos + sin(radians(angle))*wallLength), 20, 20);
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector getPos(){
|
||||||
|
return pos;
|
||||||
|
}
|
||||||
|
|
||||||
|
float getAngle(){
|
||||||
|
return angle;
|
||||||
|
}
|
||||||
|
|
||||||
|
int getLength(){
|
||||||
|
return wallLength;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,9 +1,8 @@
|
|||||||
import Vector.Vector;
|
|
||||||
import Vector.Line;
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import processing.core.PApplet;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Random;
|
import java.util.List;
|
||||||
|
|
||||||
import static java.lang.Math.abs;
|
import static java.lang.Math.abs;
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
@@ -32,34 +31,6 @@ class LineTest{
|
|||||||
// make sure the line is starting in the correct place
|
// make sure the line is starting in the correct place
|
||||||
assertFloatEquals(v1.x, line.getPosition().x);
|
assertFloatEquals(v1.x, line.getPosition().x);
|
||||||
assertFloatEquals(v1.y, line.getPosition().y);
|
assertFloatEquals(v1.y, line.getPosition().y);
|
||||||
|
|
||||||
// make sure the line ends in the correct place
|
|
||||||
assertFloatEquals(v2.x, line.endPoint().x);
|
|
||||||
assertFloatEquals(v2.y, line.endPoint().y);
|
|
||||||
|
|
||||||
Random rand = new Random();
|
|
||||||
// repeat this test with 100 random lines
|
|
||||||
for(int i = 0; i < 100; i++){
|
|
||||||
v1 = new Vector(rand.nextInt(1001), rand.nextInt(1001));
|
|
||||||
v2 = new Vector(rand.nextInt(1001), rand.nextInt(1001));
|
|
||||||
line = new Line(v1, v2);
|
|
||||||
|
|
||||||
// make sure the line is pointing in the right direction
|
|
||||||
lineDirection = v2.sub(v1);
|
|
||||||
float angleDiff = lineDirection.angleDiff(line.getDirection());
|
|
||||||
assertFloatEquals(0, angleDiff, 0.001f);
|
|
||||||
|
|
||||||
// make sure the line is the correct length
|
|
||||||
assertFloatEquals(lineDirection.mag(), line.getLength());
|
|
||||||
|
|
||||||
// make sure the line is starting in the correct place
|
|
||||||
assertFloatEquals(v1.x, line.getPosition().x);
|
|
||||||
assertFloatEquals(v1.y, line.getPosition().y);
|
|
||||||
|
|
||||||
// make sure the line ends in the correct place
|
|
||||||
assertFloatEquals(v2.x, line.endPoint().x);
|
|
||||||
assertFloatEquals(v2.y, line.endPoint().y);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import Vector.Vector;
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import processing.core.PApplet;
|
||||||
|
|
||||||
import static java.lang.Math.*;
|
import static java.lang.Math.*;
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
@@ -28,8 +28,8 @@ class VectorTest{
|
|||||||
assertFloatEquals((float)sqrt(x2*x2 + y2*y2), v2.mag());
|
assertFloatEquals((float)sqrt(x2*x2 + y2*y2), v2.mag());
|
||||||
|
|
||||||
// test dot product
|
// test dot product
|
||||||
assertFloatEquals((x1*x2+y1*y2), v1.dot(v2));
|
assertFloatEquals((float)(x1*x2+y1*y2), v1.dot(v2));
|
||||||
assertFloatEquals((x1*x2+y1*y2), v2.dot(v1));
|
assertFloatEquals((float)(x1*x2+y1*y2), v2.dot(v1));
|
||||||
|
|
||||||
// test addition
|
// test addition
|
||||||
Vector vSum = v1.add(v2);
|
Vector vSum = v1.add(v2);
|
||||||
|
|||||||
Reference in New Issue
Block a user