Added interactivity and can now save the graph state to a text file
This commit is contained in:
12
map.txt
Normal file
12
map.txt
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
numVerts,4
|
||||||
|
numEdges,3
|
||||||
|
vert0Pos,797.0,539.0
|
||||||
|
vert1Pos,149.0,673.0
|
||||||
|
vert2Pos,609.0,247.0
|
||||||
|
vert3Pos,724.0,857.0
|
||||||
|
edge0End,0
|
||||||
|
edge0Start,3
|
||||||
|
edge1End,2
|
||||||
|
edge1Start,3
|
||||||
|
edge2End,1
|
||||||
|
edge2Start,3
|
||||||
@@ -12,8 +12,7 @@ public class LineEdge extends Edge implements LineInterface{
|
|||||||
protected PointVertex vEnd;
|
protected PointVertex vEnd;
|
||||||
protected Line line;
|
protected Line line;
|
||||||
public LineEdge(PointVertex vStart, PointVertex vEnd) {
|
public LineEdge(PointVertex vStart, PointVertex vEnd) {
|
||||||
this.vStart = vStart;
|
super(vStart, vEnd);
|
||||||
this.vEnd = vEnd;
|
|
||||||
this.line = new Line(vStart.getPos(), vEnd.getPos());
|
this.line = new Line(vStart.getPos(), vEnd.getPos());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,10 @@ package Graph;
|
|||||||
import Vector.Vector;
|
import Vector.Vector;
|
||||||
import processing.core.PApplet;
|
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.pow;
|
||||||
import static java.lang.Math.sqrt;
|
import static java.lang.Math.sqrt;
|
||||||
|
|
||||||
@@ -91,7 +95,7 @@ public class PointGraph extends Graph {
|
|||||||
* @param y the y coordinate to search by
|
* @param y the y coordinate to search by
|
||||||
* @return the pointvertex closest to the given x, y coordinates
|
* @return the pointvertex closest to the given x, y coordinates
|
||||||
*/
|
*/
|
||||||
public PointVertex getClosestVertex(float x, float y){
|
public PointVertex getClosestVertex(Vector point){
|
||||||
if(super.adjList.size() == 0){
|
if(super.adjList.size() == 0){
|
||||||
// TODO: choose a better exception name
|
// TODO: choose a better exception name
|
||||||
throw new NullPointerException();
|
throw new NullPointerException();
|
||||||
@@ -102,7 +106,7 @@ public class PointGraph extends Graph {
|
|||||||
for(Vertex v : super.adjList.keySet()){
|
for(Vertex v : super.adjList.keySet()){
|
||||||
PointVertex v1 = (PointVertex) v;
|
PointVertex v1 = (PointVertex) v;
|
||||||
Vector p2 = v1.getPos();
|
Vector p2 = v1.getPos();
|
||||||
float dist = (float)sqrt(pow(x-p2.x, 2) + pow(y-p2.y, 2));
|
float dist = p2.sub(point).mag();
|
||||||
if(dist < closestDist || closestDist == -1){
|
if(dist < closestDist || closestDist == -1){
|
||||||
closestDist = dist;
|
closestDist = dist;
|
||||||
closestVertex = v1;
|
closestVertex = v1;
|
||||||
@@ -112,14 +116,24 @@ public class PointGraph extends Graph {
|
|||||||
return closestVertex;
|
return closestVertex;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void removeVertex(PointVertex v){
|
||||||
|
if(selectedVertex == v){
|
||||||
|
selectedVertex = null;
|
||||||
|
}
|
||||||
|
super.removeVertex(v);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return a bundle with all of the graphs vertex and edge information saved into it
|
* @return a bundle with all of the graphs vertex and edge information saved into it
|
||||||
*/
|
*/
|
||||||
/*
|
|
||||||
public Bundle saveToBundle(){
|
public void save() throws IOException {
|
||||||
Bundle bundle = new Bundle();
|
FileWriter file = new FileWriter("map.txt");
|
||||||
bundle.putInt("numVerts", super.numVertices());
|
|
||||||
bundle.putInt("numEdges", super.numEdges());
|
|
||||||
|
|
||||||
|
file.write("numVerts," + super.numVertices());
|
||||||
|
file.write("\nnumEdges," + super.numEdges());
|
||||||
|
|
||||||
// turn the hash map into something linear
|
// turn the hash map into something linear
|
||||||
ArrayList<PointVertex> verts = new ArrayList<>();
|
ArrayList<PointVertex> verts = new ArrayList<>();
|
||||||
@@ -134,20 +148,9 @@ public class PointGraph extends Graph {
|
|||||||
// save the vertexes
|
// save the vertexes
|
||||||
int countVerts = 0;
|
int countVerts = 0;
|
||||||
for(PointVertex v : verts){
|
for(PointVertex v : verts){
|
||||||
String countVertsString = "vert"+String.valueOf(countVerts);
|
String countVertsString = "\nvert"+String.valueOf(countVerts);
|
||||||
// save the vertex position
|
// save the vertex position
|
||||||
bundle.putFloatArray(countVertsString+"Pos", v.getPos());
|
file.write(countVertsString+"Pos,"+v.getPos().x+","+v.getPos().y);
|
||||||
// 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++;
|
countVerts++;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -155,21 +158,21 @@ public class PointGraph extends Graph {
|
|||||||
int countEdges = 0;
|
int countEdges = 0;
|
||||||
for(Edge e : edges){
|
for(Edge e : edges){
|
||||||
int idx = 0;
|
int idx = 0;
|
||||||
String countEdgesString = "edge" + String.valueOf(countEdges);
|
String countEdgesString = "\nedge" + String.valueOf(countEdges);
|
||||||
for(PointVertex v : verts){
|
for(PointVertex v : verts){
|
||||||
if(e.getStartVertex() == (Vertex)v){
|
if(e.getStartVertex() == (Vertex)v){
|
||||||
bundle.putInt(countEdgesString+"Start", idx);
|
file.write(countEdgesString+"Start,"+ idx);
|
||||||
}
|
}
|
||||||
else if(e.getEndVertex() == (Vertex)v){
|
else if(e.getEndVertex() == (Vertex)v){
|
||||||
bundle.putInt(countEdgesString+"End", idx);
|
file.write(countEdgesString+"End," + idx);
|
||||||
}
|
}
|
||||||
idx++;
|
idx++;
|
||||||
}
|
}
|
||||||
countEdges++;
|
countEdges++;
|
||||||
}
|
}
|
||||||
return bundle;
|
file.close();
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
/**
|
/**
|
||||||
* @ brief add all graph information in ta bundle to the graph
|
* @ brief add all graph information in ta bundle to the graph
|
||||||
* @param bundle the bundle to add to the graph
|
* @param bundle the bundle to add to the graph
|
||||||
|
|||||||
@@ -11,10 +11,14 @@ public class PointVertex extends Vertex {
|
|||||||
* @param yPos the y posiiton of the vertex
|
* @param yPos the y posiiton of the vertex
|
||||||
*/
|
*/
|
||||||
public PointVertex(float xPos, float yPos){
|
public PointVertex(float xPos, float yPos){
|
||||||
super();
|
|
||||||
this.position = new Vector(xPos, yPos);
|
this.position = new Vector(xPos, yPos);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public PointVertex(Vector position){
|
||||||
|
super();
|
||||||
|
this.position = position;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param xPos the x position of the vertex
|
* @param xPos the x position of the vertex
|
||||||
* @param yPos the y posiiton of the vertex
|
* @param yPos the y posiiton of the vertex
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
import Graph.*;
|
import Graph.*;
|
||||||
|
import Vector.Vector;
|
||||||
import processing.core.PApplet;
|
import processing.core.PApplet;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
|
||||||
public class Processing extends PApplet {
|
public class Processing extends PApplet {
|
||||||
|
|
||||||
@@ -19,11 +22,11 @@ public class Processing extends PApplet {
|
|||||||
size(1000, 1000);
|
size(1000, 1000);
|
||||||
car.addView(360,180);
|
car.addView(360,180);
|
||||||
|
|
||||||
for(int i = 0; i < 10; i++){
|
// for(int i = 0; i < 10; i++){
|
||||||
PointVertex vStart = new PointVertex(random(50, 950), random(50, 950));
|
// PointVertex vStart = new PointVertex(random(50, 950), random(50, 950));
|
||||||
PointVertex vEnd = new PointVertex(random(50, 950), random(50, 950));
|
// PointVertex vEnd = new PointVertex(random(50, 950), random(50, 950));
|
||||||
map.addEdge(vStart, vEnd);
|
// map.addEdge(vStart, vEnd);
|
||||||
}
|
// }
|
||||||
|
|
||||||
}
|
}
|
||||||
public void draw(){
|
public void draw(){
|
||||||
@@ -54,7 +57,48 @@ 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{
|
||||||
|
map.save();
|
||||||
|
}
|
||||||
|
catch(IOException e){
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user