Commit 897969a2 authored by Timothy  Borunov's avatar Timothy Borunov
Browse files

Now we can make an adjacency matrix using a file

parent 5f1e865f
Loading
Loading
Loading
Loading
+32 −0
Original line number Diff line number Diff line
package Graph;
// Adjacency Matrix representation in Java

public class AdjacencyMatrix {
    private boolean adjMatrix[][];
    private int vertexCount;

    // Initialize the matrix
    public AdjacencyMatrix(int vertexCount) {
        this.vertexCount = vertexCount;
        adjMatrix = new boolean[vertexCount][vertexCount];
    }

    // Add edges
    public void addEdge(int i, int j) {
        adjMatrix[i][j] = true;
        adjMatrix[j][i] = true;
    }

    // Print the matrix
    public String toString() {
        StringBuilder s = new StringBuilder();
        for (int i = 0; i < vertexCount; i++) {
            s.append(i + ": ");
            for (boolean j : adjMatrix[i]) {
                s.append((j ? 1 : 0) + " ");
            }
            s.append("\n");
        }
        return s.toString();
    }
}
 No newline at end of file
+30 −0
Original line number Diff line number Diff line
package Graph;
import java.util.Arrays;
public class VertexKey {

    private final float[] coords;

    public VertexKey(float[] coordinates) {
        this.coords = coordinates;
    }

    @Override
    public boolean equals(Object anotherVertex) {
        if (anotherVertex == this) {
            return true;
        }
        if (anotherVertex == null) {
            return false;
        }
        if (anotherVertex.getClass() != this.getClass()) {
            return false;
        }
        VertexKey vertexKey = (VertexKey) anotherVertex;
        return Arrays.equals(this.coords, vertexKey.coords);
    }

    @Override
    public int hashCode() {
        return Arrays.hashCode(this.coords);
    }
}
 No newline at end of file
+107 −0
Original line number Diff line number Diff line
package Graph;

import java.io.File;
import java.util.Scanner;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Objects;
import java.util.HashMap;
import java.util.Map;


public class readFile {
    public static String[] path = new String[3];
    private static double[] speed = new double[3];
    private static double[][] direction = new double[3][3];
    private static int numPath = 0, numSpeed = 0, numDir = 0, index = 0, vertexCount = 0;
    public static Map<VertexKey, ArrayList<float[]>> vertexList = new HashMap<>();
    public static Map<VertexKey,Integer> vertexToIndex = new HashMap<>();
    public static Map<Integer, float[]> indexToVertex = new HashMap<>();

    public static AdjacencyMatrix adjMatrix;
    public static void parseCommand(String[] args) {
        // parses command of up to 3 shapes, each with own path, init speed, and init dir
        // will just match these attributes up in the order that they appear


        for (String arg : args) {
            if (Objects.equals(arg, "--image")) {
                path[numPath] = args[index + 1];
                numPath++;
            }
            if (Objects.equals(arg, "--speed")) {
                speed[numSpeed] = Double.parseDouble(args[index + 1]);
                numSpeed++;
            }
            if (Objects.equals(arg, "--dir")) {
                for (int ii = 0; ii < 3; ii++) {

                    direction[numDir][ii] = Double.parseDouble(args[index + ii + 1].replace(",",""));
                }
                numDir++;
            }
            index++;
        }

        if ((numPath != numDir) || (numSpeed != numDir) || (numPath == 0)) {
            // die
            return;
        }
    }

    public static void makeVertexHash(String currPath) {
        try
        {
            File fileObj = new File(currPath);
            Scanner myReader = new Scanner(fileObj);
            while (myReader.hasNextLine()) {
                String data = myReader.nextLine();
                String[] coords = data.split(" ");
                ArrayList<float[]> triangle = new ArrayList<>();
                for (String coord:coords) {
                    coord = coord.replaceAll("[\\[\\]]", "");
                    //coord = coord.replace(".", " ");
                    String[] vals = coord.split(",");
                    int valIndex = 0;
                    float[] vertex = new float[3];
                    for (String val:vals) {
                        vertex[valIndex] = Float.parseFloat(val);
                        valIndex++;
                    }
                    triangle.add(vertex);
                }
                for (int i = 0; i<3; i++) {
                    float[] vertex = triangle.get(i);
                    VertexKey vertexKey = new VertexKey(vertex);
                    if (!vertexList.containsKey(vertexKey)) {
                        ArrayList<float[]> adjList = new ArrayList<>();
                        vertexList.put(vertexKey, adjList);
                        vertexToIndex.put(vertexKey, vertexCount);
                        indexToVertex.put(vertexCount, vertex);
                        vertexCount++;
                    }
                    for (int j = 1; j < 3; j++) {
                        if (!vertexList.get(vertexKey).contains(triangle.get((i+j) % 3)))
                            vertexList.get(vertexKey).add(triangle.get((i+j) % 3));
                    }
                }
            }

            myReader.close();
        }
        catch (FileNotFoundException exception)
        {

        }
    }

    public static void makeMatrix() {
        adjMatrix = new AdjacencyMatrix(vertexCount);
        for (int i = 0; i < vertexCount; i++) {
            VertexKey currVertexKey = new VertexKey(indexToVertex.get(i));
            for (float[] adjVertex : vertexList.get(currVertexKey)) {
                adjMatrix.addEdge(i,vertexToIndex.get(new VertexKey(adjVertex)));
            }
        }
    }
}
 No newline at end of file

src/Main.java

0 → 100644
+10 −0
Original line number Diff line number Diff line
import Graph.readFile;
public class Main {
    public static void main(String[] args) {
        readFile processInput = new readFile();
        processInput.parseCommand(args);
        processInput.makeVertexHash(processInput.path[0]);
        processInput.makeMatrix();
        System.out.print(processInput.adjMatrix.toString());
    }
}

src/testfile.txt

0 → 100644
+6 −0
Original line number Diff line number Diff line
[0,-105,0] [122.5,105,62.5] [-122.5,105,62.5]
[0,-105,0] [122.5,105,62.5] [122.5,105,-62.5]
[0,-105,0] [-122.5,105,-62.5] [-122.5,105,62.5]
[0,-105,0] [-122.5,105,-62.5] [122.5,105,-62.5]
[122.5,105,62.5] [-122.5,105,-62.5] [-122.5,105,62.5]
[122.5,105,62.5] [-122.5,105,-62.5] [122.5,105,-62.5]
 No newline at end of file