Added interactivity and can now save the graph state to a text file

This commit is contained in:
Quinn
2023-05-02 11:19:39 -05:00
parent 1a4d6e6909
commit 38847abead
5 changed files with 95 additions and 33 deletions

12
map.txt Normal file
View 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

View File

@@ -12,8 +12,7 @@ public class LineEdge extends Edge implements LineInterface{
protected PointVertex vEnd;
protected Line line;
public LineEdge(PointVertex vStart, PointVertex vEnd) {
this.vStart = vStart;
this.vEnd = vEnd;
super(vStart, vEnd);
this.line = new Line(vStart.getPos(), vEnd.getPos());
}

View File

@@ -3,6 +3,10 @@ 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;
@@ -91,7 +95,7 @@ public class PointGraph extends Graph {
* @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){
public PointVertex getClosestVertex(Vector point){
if(super.adjList.size() == 0){
// TODO: choose a better exception name
throw new NullPointerException();
@@ -102,7 +106,7 @@ public class PointGraph extends Graph {
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));
float dist = p2.sub(point).mag();
if(dist < closestDist || closestDist == -1){
closestDist = dist;
closestVertex = v1;
@@ -112,14 +116,24 @@ public class PointGraph extends Graph {
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
*/
/*
public Bundle saveToBundle(){
Bundle bundle = new Bundle();
bundle.putInt("numVerts", super.numVertices());
bundle.putInt("numEdges", super.numEdges());
public void save() throws IOException {
FileWriter file = new FileWriter("map.txt");
file.write("numVerts," + super.numVertices());
file.write("\nnumEdges," + super.numEdges());
// turn the hash map into something linear
ArrayList<PointVertex> verts = new ArrayList<>();
@@ -134,20 +148,9 @@ public class PointGraph extends Graph {
// save the vertexes
int countVerts = 0;
for(PointVertex v : verts){
String countVertsString = "vert"+String.valueOf(countVerts);
String countVertsString = "\nvert"+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);
}
file.write(countVertsString+"Pos,"+v.getPos().x+","+v.getPos().y);
countVerts++;
}
@@ -155,21 +158,21 @@ public class PointGraph extends Graph {
int countEdges = 0;
for(Edge e : edges){
int idx = 0;
String countEdgesString = "edge" + String.valueOf(countEdges);
String countEdgesString = "\nedge" + String.valueOf(countEdges);
for(PointVertex v : verts){
if(e.getStartVertex() == (Vertex)v){
bundle.putInt(countEdgesString+"Start", idx);
file.write(countEdgesString+"Start,"+ idx);
}
else if(e.getEndVertex() == (Vertex)v){
bundle.putInt(countEdgesString+"End", idx);
file.write(countEdgesString+"End," + idx);
}
idx++;
}
countEdges++;
}
return bundle;
file.close();
}
*/
/**
* @ brief add all graph information in ta bundle to the graph
* @param bundle the bundle to add to the graph

View File

@@ -11,10 +11,14 @@ public class PointVertex extends Vertex {
* @param yPos the y posiiton of the vertex
*/
public PointVertex(float xPos, float yPos){
super();
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

View File

@@ -1,6 +1,9 @@
import Graph.*;
import Vector.Vector;
import processing.core.PApplet;
import java.io.IOException;
public class Processing extends PApplet {
@@ -19,11 +22,11 @@ public class Processing extends PApplet {
size(1000, 1000);
car.addView(360,180);
for(int i = 0; i < 10; i++){
PointVertex vStart = new PointVertex(random(50, 950), random(50, 950));
PointVertex vEnd = new PointVertex(random(50, 950), random(50, 950));
map.addEdge(vStart, vEnd);
}
// for(int i = 0; i < 10; i++){
// PointVertex vStart = new PointVertex(random(50, 950), random(50, 950));
// PointVertex vEnd = new PointVertex(random(50, 950), random(50, 950));
// map.addEdge(vStart, vEnd);
// }
}
public void draw(){
@@ -54,7 +57,48 @@ public class Processing extends PApplet {
if(key == 'e'){
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);
}