xxxxxxxxxx
56
let graphString = ""
let vertices = []
let nv = 5
let edges = [[0,1], [0,2], [0,3], [1,3], [3,4]]
let a,b
class vx {
constructor(x, y, label) {
this.pos = createVector(x, y)
this.name = label
this.deg = 0
this.par = -1
}
show() {
push()
strokeWeight(5)
translate(width/2, 20)
point(this.pos.x, this.pos.y)
pop()
}
}
function setup() {
createCanvas(400, 400);
for (let i=0;i<nv;i++) {
vertices.push(new vx(0,i*100,i))
}
for (let i=0;i<edges.length;i++) {
let ct = 0
for (let e of edges) {
ct += e[0]==i
ct += e[1]==i
}
vertices[i].deg = ct
}
}
function draw() {
background(240);
for (let v of vertices) {
v.show()
}
for (let e of edges) {
a = vertices[e[0]]
b = vertices[e[1]]
push()
translate(width/2, 20)
line(a.pos.x, a.pos.y, b.pos.x, b.pos.y)
pop()
}
}