var table; var edges = []; var vertices = {}; var numVertices, squareLW = 10, maximum, minimum; function preload() { table = loadTable('698.edges'); } function setup() { minimum = 999999999; maximum = 0; var str, parsed; // calculate number of vertices, and add edges for (var i =0; i < table.getRowCount(); i++){ str = table.getString(i,0); parsed = str.split(" "); // read in vertices and add to map of vertices // also check for min and max if (!(parsed[0] in vertices)){ vertices[parsed[0]] = new Vertice(parsed[0]); if (parseInt(parsed[0]) > maximum){ maximum = parseInt(parsed[0]); } if (parseInt(parsed[0]) < minimum){ minimum = parseInt(parsed[0]); } } if (!(parsed[1] in vertices)){ vertices[parsed[1]] = new Vertice(parsed[1]); if (parseInt(parsed[1]) > maximum){ maximum = parseInt(parsed[1]); } if (parseInt(parsed[1]) < minimum){ minimum = parseInt(parsed[1]); } } // add edges to edges array edges.push(new Edge(parsed[0], parsed[1])); } numVertices = Object.keys(vertices).length; createCanvas(100+(numVertices*squareLW), 100+(numVertices*squareLW)); // assign order to vertices var counter = 0; for (var i = 0; i <= maximum; i++){ if (i in vertices){ vertices[i].setOrder(counter); counter++; } } } function draw(){ clear(); for (i in vertices){ textSize(7.5); text(vertices[i].getID(), 25, 56+(squareLW*vertices[i].getOrder())); push(); rotate(radians(270)); text(vertices[i].getID(), -50, 56+(squareLW*vertices[i].getOrder())); pop(); } push(); stroke(255); fill(245); for (var k = 0; k < numVertices; k++){ for (var j = 0; j < numVertices; j++){ // check for hover here?? rect(50+(squareLW*k), 50+(squareLW*j), squareLW, squareLW); fill(245); } } pop(); var hoverOver = -1, nodeOver = -1; for (var n = 0; n < edges.length; n++){ edges[n].show(); if (edges[n].checkHover() != -1){ hoverOver = n; nodeOver = edges[n].checkHover(); } } if (hoverOver != -1){ edges[hoverOver].getDetails(nodeOver); } } class Vertice { constructor(id){ this.id = id; this.order = -1; } setOrder(order){ this.order = order; } getOrder(){ return this.order; } getID(){ return this.id; } } class Edge { constructor(vertice1, vertice2){ this.vertice1 = vertice1; this.vertice2 = vertice2; } show(){ var v1order = vertices[this.vertice1].getOrder(); var v2order = vertices[this.vertice2].getOrder(); // positions (will be 2 nodes in the matrix) this.x1 = 50+(squareLW*v1order); this.x2 = 50+(squareLW*v2order); this.y1 = 50+(squareLW*v2order); this.y2 = 50+(squareLW*v1order); push(); fill(50); noStroke(); rect(this.x1, this.y1, squareLW, squareLW); rect(this.x2, this.y2, squareLW, squareLW); pop(); } checkHover(){ // node at x1 y1 if (mouseX > this.x1 & mouseX < this.x1+squareLW & mouseY > this.y1 & mouseY < this.y1+squareLW){ return 1; } // node at x2 y2 else if (mouseX > this.x2 & mouseX < this.x2+squareLW & mouseY > this.y2 & mouseY < this.y2+squareLW){ return 2; } else{ return -1; } } getDetails(num){ if (num == 1){ push(); fill('red'); rect(this.x1, this.y1, squareLW, squareLW); var v1order = vertices[this.vertice1].getOrder(); var v2order = vertices[this.vertice2].getOrder(); text(vertices[this.vertice2].getID(), 25, 56+(squareLW*v2order)); // y pop(); push(); fill('red'); rotate(radians(270)); text(vertices[this.vertice1].getID(), -50, 56+(squareLW*v1order)); // x pop(); rect(this.x1-8, this.y1-15, 50, 12); text("("+this.vertice1+", " + this.vertice2 + ")", this.x1-5, this.y1-5); } if (num == 2){ push(); fill('red'); rect(this.x2, this.y2, squareLW, squareLW); var v1order = vertices[this.vertice1].getOrder(); var v2order = vertices[this.vertice2].getOrder(); text(vertices[this.vertice1].getID(), 25, 56+(squareLW*v1order)); // y pop(); push(); fill('red'); rotate(radians(270)); text(vertices[this.vertice2].getID(), -50, 56+(squareLW*v2order)); // x pop(); rect(this.x2-8, this.y2-15, 50, 12); text("("+this.vertice2+", " + this.vertice1 + ")", this.x2-5, this.y2-5); } } getVertice1(){ return this.vertice1; } getVertice2(){ return this.vertice2; } }