float d = 10; float r = d/2; float r2 = sq(r); color c_bg = color(0); color c_node_on = color(100, 100, 255); color c_node_off = color(80); color c_link_on = color(100, 100, 255); color c_link_off = color(80); Node[] nodes; Link[] links; XMLElement xml; void screenSetup(){ size(850, 500); smooth(); strokeWeight(3); frameRate(30); } void screenLoadXML(){ ArrayList load_nodes = new ArrayList(); ArrayList load_links = new ArrayList(); ArrayList network_pairs = new ArrayList(); float x_min = 0; float x_max = 0; float y_min = 0; float y_max = 0; xml = new XMLElement(this, "map_vectors_1_clean.svg"); int numChildren = xml.getChildCount(); for (int i = 0; i < numChildren; i++) { XMLElement kid = xml.getChild(i); println(kid); String kid_name = kid.getContent(); String kid_string = kid.toString(); if(kid_string.substring(1, 6).equals( "path ")){ String d_string = kid.getStringAttribute("D"); // println(d_string); boolean absoluteCurveTo = false; if(d_string.indexOf('C') != -1) absoluteCurveTo = true; println("d = "+d_string); // add commas where there are minus signs String[] d_minus_fix = d_string.split("-"); d_string = d_minus_fix[0]; if(d_minus_fix.length > 1){ for(int j = 1; j < d_minus_fix.length; j++){ d_string += ",-" + d_minus_fix[j]; } } println("d = "+d_string); // get rid of the cC String[] c_strings = d_string.split("[cC]"); d_string = c_strings[0]; if(c_strings.length > 1){ for(int j = 1; j < c_strings.length; j++){ if(c_strings[j].substring(0,1).equals(",")) d_string += c_strings[j]; else d_string += "," + c_strings[j]; } } println("d = "+d_string); // drop the M and split by commas String[] pt_strings = (d_string.substring(1)).split(","); float[] xs = new float[4]; float[] ys = new float[4]; xs[0] = (Float.valueOf(pt_strings[0])).floatValue(); ys[0] = (Float.valueOf(pt_strings[1])).floatValue(); int i_n0 = screenNodeFoundIn(xs[0], ys[0], load_nodes); Node n_0 = null; if(i_n0 != -1){ println("found a repeat"); n_0 = (Node)(load_nodes.get(i_n0)); } else { n_0 = new Node(xs[0], ys[0]); load_nodes.add(n_0); i_n0 = load_nodes.size() -1; } // handle the end point xs[3] = (Float.valueOf(pt_strings[6])).floatValue(); ys[3] = (Float.valueOf(pt_strings[7])).floatValue(); if(!absoluteCurveTo){ xs[3] += xs[0]; ys[3] += ys[0]; } int i_n3 = screenNodeFoundIn(xs[3], ys[3], load_nodes); Node n_3 = null; if(i_n3 != -1){ println("found a repeat"); n_3 = (Node)(load_nodes.get(i_n3)); } else { n_3 = new Node(xs[3], ys[3]); load_nodes.add(n_3); i_n3 = load_nodes.size() -1; } xs[1] = (Float.valueOf(pt_strings[2])).floatValue(); ys[1] = (Float.valueOf(pt_strings[3])).floatValue(); xs[2] = (Float.valueOf(pt_strings[4])).floatValue(); ys[2] = (Float.valueOf(pt_strings[5])).floatValue(); if(!absoluteCurveTo){ xs[1] += xs[0]; ys[1] += ys[0]; xs[2] += xs[0]; ys[2] += ys[0]; } Node n_1 = new Node(xs[1], ys[1]); Node n_2 = new Node(xs[2], ys[2]); load_links.add(new Link(n_0.p, n_1.p, n_2.p, n_3.p)); int i_l = load_links.size() - 1; // remember the network pairs //int[] pair0 = {i_l, i_n0}; //int[] pair3 = {i_l, i_n3}; //network_pairs.add(pair0); //network_pairs.add(pair3); int[] pair_nodes = {i_n0, i_n3}; network_pairs.add(pair_nodes); //check mins and maxes for(int j = 0; j < xs.length; j++){ if(xs[j] < x_min) {x_min = xs[j];} if(xs[j] > x_max) {x_max = xs[j];} if(ys[j] < y_min) {y_min = ys[j];} if(ys[j] > y_max) {y_max = ys[j];} } } } //convert the nodes and links to arrays and store them. nodes = (Node[])(load_nodes.toArray(new Node[0])); links = (Link[])(load_links.toArray(new Link[0])); println("nodes.length = "+nodes.length); float cx = (x_max + x_min)/2.0; float cy = (y_max + y_min)/2.0; float dx = width/2.0 - cx; float dy = height/2.0 - cy; for(int n = 0; n < nodes.length; n++){ nodes[n].p.x += dx; nodes[n].p.y += dy; } for(int l = 0; l < links.length; l++){ //links[l].p1.x += dx; //links[l].p1.y += dy; links[l].p1a.x += dx; links[l].p1a.y += dy; //links[l].p2.x += dx; //links[l].p2.y += dy; links[l].p2a.x += dx; links[l].p2a.y += dy; } on = new boolean[nodes.length]; visited = new boolean[nodes.length]; on_next = new boolean[nodes.length]; visited_next = new boolean[nodes.length]; network = new boolean[nodes.length][nodes.length]; Iterator it_net = network_pairs.iterator(); int[] pair; while(it_net.hasNext()){ pair = (int[])(it_net.next()); network[pair[0]][pair[1]] = true; network[pair[1]][pair[0]] = true; } // generate code for hard-wired device int lngth = nodes.length; String on_string = "boolean on["+lngth+"] = { "; String on_next_string = "boolean on_next["+lngth+"] = { "; String visited_string = "boolean visited["+lngth+"] = { "; String visited_next_string = "boolean visited_next["+lngth+"] = { "; for(int i = 0; i < lngth-1; i++){ on_string += on[i]+", "; on_next_string += on_next[i]+", "; visited_string += on[i]+", "; visited_next_string += on[i]+", "; } on_string += on[lngth-1]+" };"; on_next_string += on_next[lngth-1]+" };"; visited_string += visited[lngth-1]+" };"; visited_next_string += visited_next[lngth-1]+" };"; String network_string = "boolean network["+lngth+"]["+lngth+"] = { "; for(int i = 0; i < lngth-1; i++){ String network_i_string = "{ "; for(int j = 0; j < lngth-1; j++){ network_i_string += network[i][j]+", "; } network_i_string += network[i][lngth-1]+"} ,"; network_string += network_i_string; } String network_i_string = "{ "; for(int j = 0; j < lngth-1; j++){ network_i_string += network[lngth-1][j]+", "; } network_i_string += network[lngth-1][lngth-1]+"} "; network_string += network_i_string+" };"; String[] out = {on_string, on_next_string, visited_string, visited_next_string, network_string}; //saveStrings("vars.txt", out); } int screenNodeFoundIn(float x, float y, ArrayList load_nodes){ int i_match = -1; int i = 0; Iterator it = load_nodes.iterator(); while(it.hasNext()){ Node n = (Node)(it.next()); float dx = x - n.p.x; float dy = y - n.p.y; float d2 = sq(dx) + sq(dy); if(d2 <= r2){ i_match = i; break; } i++; } return i_match; } void screenDraw(){ background(c_bg); noFill(); for(int i = 0; i < links.length; i++){ links[i].draw(); } noStroke(); for(int i = 0; i < on.length; i++){ nodes[i].draw(); if(on[i]){ fill(c_node_on); } else { fill(c_node_off); } ellipse(nodes[i].p.x, nodes[i].p.y, d, d); } } void screenReset(){ for(int i = 0; i < nodes.length; i++){ nodes[i].reset(); } for(int i = 0; i < links.length; i++){ links[i].reset(); } }