alphachapmtl
Full Member
Offline
Posts: 128
Canada
Pathfinder_Dijkstra
Reply #8 - Sep 16th , 2007, 7:42pm
I made a tiny variation of <Pathfinder_Dijkstra.pde> from the Pathfinder library. It just put some colors on the connectors. //from http://robotacid.com/ //from http://www.robotacid.com/PBeta/AILibrary/Pathfinder/index.html // Demo of Dijkstra algorithm import ai.pathfinder.*; Pathfinder dijk; int w = 10; int h = 10; boolean colored=true; int spacing,s; PFont font; void setup(){ size(400, 400); size(400, 400); spacing = width / w; s = spacing / 2; smooth(); dijk = new Pathfinder(w, h, 1.0); font = loadFont("ArialMT-10.vlw"); textFont(font, 10); } void draw(){ background(230); fill(255); int m = (mouseX/spacing) + (mouseY/spacing) * w; rect((m % w) * spacing, (m / w) * spacing, spacing, spacing); drawConnectors(); drawNodes(); } void mousePressed(){ int m = (mouseX/spacing) + (mouseY/spacing) * w; Node n = (Node)dijk.nodes.get(m); dijk.dijkstra(n); } void drawNodes(){ for(int i = 0; i < dijk.nodes.size(); i++){ Node n = (Node)dijk.nodes.get(i); Node p = null; int pnum = -1; if(n.parent != null){ p = n.parent; pnum = dijk.indexOf(p); } int x = i % w; int y = i / w; int s = spacing / 2; if(p != null){ int px = pnum % w; int py = pnum / w; int xDelta=px-x; int yDelta=py-y; strokeWeight(1.5); //choose connection color if (colored) { int rCol=0,gCol=0,bCol=0; if (xDelta<0) rCol=255; else if (xDelta>0) rCol=0; else rCol=127; if (yDelta<0) bCol=255; else if (yDelta>0) bCol=0; else bCol=127; stroke(rCol, gCol, bCol); } else stroke(10); line(s + x * spacing, s + y * spacing, s + lerp(x, px, .8) * spacing, s + lerp(y, py, .8) * spacing); } strokeWeight(1); stroke(10); fill(255); ellipse(s + x * spacing, s + y * spacing, 10, 10); fill(0); text(nf(n.g, 2, 1), 10 + x * spacing, 10 + y * spacing); } } void drawConnectors(){ stroke(200, 200, 250); for(int i = 0; i < dijk.nodes.size(); i++){ Node n = (Node)dijk.nodes.get(i); int cnum = -1; for(int j = 0; j < n.links.size(); j++){ Connector c = (Connector)n.links.get(j); cnum = dijk.indexOf(c.n); int x = i % w; int y = i / w; int cx = cnum % w; int cy = cnum / w; line(s + x * spacing, s + y * spacing, s + cx * spacing, s + cy * spacing); } } }