Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- NHandler nodeHandler;
- Node test;
- void setup() {
- size(1920, 1080);
- nodeHandler = new NHandler();
- }
- void draw() {
- background(51);
- if (frameCount % 30 == 0) nodeHandler.pulseQTYs();
- nodeHandler.render();
- }
- class Node {
- PVector pos;
- color col;
- color ringCol;
- float ringSize;
- int nodeID;
- NHandler handler;
- ArrayList<Integer> parents;
- ArrayList<Integer> children;
- boolean valid;
- int trueQTY;
- int infiniteRate;
- int reserveQTY1;
- int reserveQTY2;
- int pulseCount;
- PVector northPort;
- PVector southPort;
- PVector eastPort;
- PVector westPort;
- Node(NHandler nh, int posx, int posy) {
- handler = nh;
- nodeID = handler.NodeIDsCreated;
- valid = true;
- parents = new ArrayList<Integer>();
- children = new ArrayList<Integer>();
- pos = new PVector(posx, posy);
- northPort = new PVector(posx, posy-handler.nodeSize/2.0);
- southPort = new PVector(posx, posy+handler.nodeSize/2.0);
- eastPort = new PVector(posx+handler.nodeSize/2.0, posy);
- westPort = new PVector(posx-handler.nodeSize/2.0, posy);
- trueQTY = 0;
- reserveQTY1 = 0;
- reserveQTY2 = 0;
- infiniteRate = 15;
- }
- void setValid(boolean b) {
- valid = b;
- }
- void addParent(int parentID) {
- parents.add(parentID);
- }
- void addChild(int childID) {
- children.add(childID);
- }
- void tryRemoveParent(int parentID) {
- for (int i = parents.size()-1; i >= 0; i--) {
- if (parents.get(i) == parentID) {
- parents.remove(i);
- }
- }
- }
- void tryRemoveChild(int childID) {
- for (int i = children.size()-1; i >= 0; i--) {
- if (children.get(i) == childID) {
- children.remove(i);
- }
- }
- }
- void updatePortPositions() {
- northPort = new PVector(pos.x, pos.y-handler.nodeSize/2.0);
- southPort = new PVector(pos.x, pos.y+handler.nodeSize/2.0);
- eastPort = new PVector(pos.x+handler.nodeSize/2.0, pos.y);
- westPort = new PVector(pos.x-handler.nodeSize/2.0, pos.y);
- }
- void show(color f) {
- stroke(0);
- fill(f);
- circle(pos.x, pos.y, handler.nodeSize);
- fill(0);
- stroke(0);
- textSize(handler.textSize);
- if (parents.size()==0) {
- text(infiniteRate, pos.x, pos.y);
- } else {
- text(trueQTY, pos.x, pos.y);
- }
- }
- void pulseToChildren() {
- if (parents.size() == 0) {
- trueQTY = infiniteRate;
- }
- if (children.size()>0) {
- for (int i = trueQTY; i > 0; i--) {
- pulseCount++;
- handler.getNodeByID(children.get(pulseCount % (children.size()))).reserveQTY1++;
- trueQTY--;
- }
- } else {
- trueQTY = 0;
- reserveQTY2 = 0;
- }
- }
- String getSendingPortDirection(Node otherNode) {
- float onXD = otherNode.pos.x - pos.x;
- float cXD1 = 100000;
- float cXD2 = 100000;
- float cXD3 = 100000;
- float onYD = otherNode.pos.y - pos.y;
- float cYD1 = 100000;
- float cYD2 = 100000;
- float cYD3 = 100000;
- float distON = dist(0, 0, onXD, onYD);
- float dist1 = 100000;
- float dist2 = 100000;
- float dist3 = 100000;
- switch(children.size()) {
- case 1:
- return "south";
- case 2:
- cXD1 = handler.getNodeByID(children.get(0)).pos.x - pos.x;
- cXD2 = handler.getNodeByID(children.get(1)).pos.x - pos.x;
- cYD1 = handler.getNodeByID(children.get(0)).pos.y - pos.y;
- cYD2 = handler.getNodeByID(children.get(1)).pos.y - pos.y;
- dist1 = dist(0, 0, cXD1, cYD1);
- dist2 = dist(0, 0, cXD2, cYD2);
- if (cXD1 < 0 && cXD2 < 0) { //if both are on the left, the further left one connects west, the closer left one connects south
- if (onXD == min(cXD1, cXD2)) {
- return "west";
- } else {
- return "south";
- }
- } else if (cXD1 > 0 && cXD2 > 0) { //if both are on the right, the further right one connects east, the closer right one connects south
- if (onXD == min(cXD1, cXD2)) {
- return "south";
- } else {
- return "east";
- }
- } else if (min(abs(cXD1), abs(cXD2)) == 0 && max(abs(cXD1), abs(cXD2)) > 0) { //if one is directly below, connect it south. Otherwise go east or west
- if (onXD == 0) {
- return "south";
- } else {
- if (onXD > 0) {
- return "east";
- } else {
- return "west";
- }
- }
- } else { //otherwise, the closer node connects south, and the remaining node connects either east or west
- if (distON == min(dist1, dist2)) {
- return "south";
- } else {
- if (max(dist1, dist2) == dist1) {
- float sign = cXD1/abs(cXD1);
- if (sign > 0) {
- return "east";
- } else {
- return "west";
- }
- }
- }
- }
- return "east";
- case 3:
- cXD1 = handler.getNodeByID(children.get(0)).pos.x - pos.x;
- cXD2 = handler.getNodeByID(children.get(1)).pos.x - pos.x;
- cXD3 = handler.getNodeByID(children.get(2)).pos.x - pos.x;
- float[] cXD = {cXD1, cXD2, cXD3};
- cXD = sort(cXD);
- cYD1 = handler.getNodeByID(children.get(0)).pos.y - pos.y;
- cYD2 = handler.getNodeByID(children.get(1)).pos.y - pos.y;
- cYD3 = handler.getNodeByID(children.get(2)).pos.y - pos.y;
- float[] cYD = {cYD1, cYD2, cYD3};
- cYD = sort(cYD);
- dist1 = dist(0, 0, cXD1, cYD1);
- dist2 = dist(0, 0, cXD2, cYD2);
- dist3 = dist(0, 0, cXD3, cYD3);
- if (cXD[0] != cXD[1] && cXD[1] != cXD[2] && cXD[2] != cXD[0]) { //if all deltas of X are different, assign west/south/east to min/med/max
- if (onXD == cXD[0]) {
- return "west";
- } else if (onXD == cXD[2]) {
- return "east";
- } else {
- return "south";
- }
- } else if (((cXD[1] == cXD[0]) || (cXD[1] == cXD[2])) && cXD[0] != cXD[2]) { //if 2 are the same, just combine them out of the same port (too complex otherwise), the last one goes out the correct port.
- if (cXD[1] == cXD[0]) {
- if (onXD == cXD[2]) {
- return "east";
- } else {
- return "west";
- }
- } else {
- if (onXD == cXD[0]) {
- return "west";
- } else {
- return "east";
- }
- }
- } else {
- return "south";
- }
- default:
- return "south";
- }
- }
- String getReceivingPortDirection(Node otherNode) {
- float onXD = otherNode.pos.x - pos.x;
- float cXD1 = 100000;
- float cXD2 = 100000;
- float cXD3 = 100000;
- float onYD = otherNode.pos.y - pos.y;
- float cYD1 = 100000;
- float cYD2 = 100000;
- float cYD3 = 100000;
- float distON = dist(0, 0, onXD, onYD);
- float dist1 = 100000;
- float dist2 = 100000;
- float dist3 = 100000;
- switch(parents.size()) {
- case 1:
- return "north";
- case 2:
- cXD1 = handler.getNodeByID(parents.get(0)).pos.x - pos.x;
- cXD2 = handler.getNodeByID(parents.get(1)).pos.x - pos.x;
- cYD1 = handler.getNodeByID(parents.get(0)).pos.y - pos.y;
- cYD2 = handler.getNodeByID(parents.get(1)).pos.y - pos.y;
- dist1 = dist(0, 0, cXD1, cYD1);
- dist2 = dist(0, 0, cXD2, cYD2);
- if (cXD1 < 0 && cXD2 < 0) { //if both are on the left, the further left one connects west, the closer left one connects south
- if (onXD == min(cXD1, cXD2)) {
- return "west";
- } else {
- return "north";
- }
- } else if (cXD1 > 0 && cXD2 > 0) { //if both are on the right, the further right one connects east, the closer right one connects south
- if (onXD == min(cXD1, cXD2)) {
- return "north";
- } else {
- return "east";
- }
- } else if (min(abs(cXD1), abs(cXD2)) == 0 && max(abs(cXD1), abs(cXD2)) > 0) { //if one is directly below, connect it south. Otherwise go east or west
- if (onXD == 0) {
- return "north";
- } else {
- if (onXD > 0) {
- return "east";
- } else {
- return "west";
- }
- }
- } else { //otherwise, the closer node connects south, and the remaining node connects either east or west
- if (distON == min(dist1, dist2)) {
- return "north";
- } else {
- if (max(dist1, dist2) == dist1) {
- float sign = cXD1/abs(cXD1);
- if (sign > 0) {
- return "east";
- } else {
- return "west";
- }
- }
- }
- }
- return "east";
- case 3:
- cXD1 = handler.getNodeByID(parents.get(0)).pos.x - pos.x;
- cXD2 = handler.getNodeByID(parents.get(1)).pos.x - pos.x;
- cXD3 = handler.getNodeByID(parents.get(2)).pos.x - pos.x;
- float[] cXD = {cXD1, cXD2, cXD3};
- cXD = sort(cXD);
- cYD1 = handler.getNodeByID(parents.get(0)).pos.y - pos.y;
- cYD2 = handler.getNodeByID(parents.get(1)).pos.y - pos.y;
- cYD3 = handler.getNodeByID(parents.get(2)).pos.y - pos.y;
- float[] cYD = {cYD1, cYD2, cYD3};
- cYD = sort(cYD);
- dist1 = dist(0, 0, cXD1, cYD1);
- dist2 = dist(0, 0, cXD2, cYD2);
- dist3 = dist(0, 0, cXD3, cYD3);
- if (cXD[0] != cXD[1] && cXD[1] != cXD[2] && cXD[2] != cXD[0]) { //if all deltas of X are different, assign west/south/east to min/med/max
- if (onXD == cXD[0]) {
- return "west";
- } else if (onXD == cXD[2]) {
- return "east";
- } else {
- return "north";
- }
- } else if (((cXD[1] == cXD[0]) || (cXD[1] == cXD[2])) && cXD[0] != cXD[2]) { //if 2 are the same, just combine them out of the same port (too complex otherwise), the last one goes out the correct port.
- if (cXD[1] == cXD[0]) {
- if (onXD == cXD[2]) {
- return "east";
- } else {
- return "west";
- }
- } else {
- if (onXD == cXD[0]) {
- return "west";
- } else {
- return "east";
- }
- }
- } else {
- return "north";
- }
- default:
- return "north";
- }
- }
- PVector getReceivingPortPosition(Node otherNode) {
- float dX = otherNode.pos.x-pos.x;
- float dY = otherNode.pos.y-pos.y;
- if (dY >= 0) {
- if (dX >= 0) {
- return eastPort;
- } else {
- return westPort;
- }
- } else {
- if (abs(dY) > abs(dX)) {
- return northPort;
- } else {
- if (dX >= 0) {
- return eastPort;
- } else {
- return westPort;
- }
- }
- }
- }
- float itemRate() {
- if (parents.size() == 0) {
- return infiniteRate;
- } else {
- if (children.size() == 0) {
- return trueQTY;
- } else {
- return trueQTY / float(children.size());
- }
- }
- }
- }
- class NHandler {
- int NodeIDsCreated;
- ArrayList<Node> nodes;
- ArrayList<PVector> relationships;
- Node selected;
- Node movingNode;
- float nodeSize;
- float textSize;
- boolean deleting;
- NHandler() {
- nodes = new ArrayList<Node>();
- NodeIDsCreated = 0;
- relationships = new ArrayList<PVector>();
- nodeSize = 75.0;
- textSize = nodeSize / 4.0;
- deleting = false;
- }
- void addNode(int numToAdd) {
- for (int i = 0; i < numToAdd; i++) {
- NodeIDsCreated++;
- nodes.add(new Node(this, 0, 0));
- }
- }
- void addNode(int numToAdd, int posx, int posy) {
- for (int i = 0; i < numToAdd; i++) {
- NodeIDsCreated++;
- nodes.add(new Node(this, posx, posy));
- }
- }
- boolean validID(int id) {
- boolean temp = false;
- for (Node n : nodes) {
- if (n.nodeID == id && n.valid) {
- temp = true;
- break;
- }
- }
- return temp;
- }
- void createRelationship(Node parent, Node child) {
- int pID = parent.nodeID;
- int cID = child.nodeID;
- if (pID != cID) {
- parent.children.add(cID);
- child.parents.add(pID);
- relationships.add(new PVector(pID, cID));
- }
- }
- void createOrRemoveRelationship(int parentID, int childID) {
- if (parentID != childID) {
- if (!containsRelationship(parentID, childID)) {
- getNodeByID(parentID).children.add(childID);
- getNodeByID(childID).parents.add(parentID);
- relationships.add(new PVector(parentID, childID));
- } else {
- breakSpecificRelationship(parentID, childID);
- }
- } else {
- println("Same ID (" + parentID + ", " + childID);
- }
- }
- Node getNodeByID(int nID) {
- Node temp = null;
- for (Node n : nodes) {
- if (n.nodeID == nID) return n;
- }
- return temp;
- }
- boolean containsRelationship(int parID, int chiID) {
- for (PVector r : relationships) {
- if (r.x == parID && r.y == chiID) {
- return true;
- }
- }
- return false;
- }
- void breakAllRelationshipsOfNode(Node n) {
- for (int i = relationships.size()-1; i >= 0; i--) {
- if (relationships.get(i).x == n.nodeID || relationships.get(i).y == n.nodeID) {
- int parID = int(relationships.get(i).x);
- int chiID = int(relationships.get(i).y);
- getNodeByID(parID).tryRemoveChild(chiID);
- getNodeByID(chiID).tryRemoveParent(parID);
- relationships.remove(i);
- }
- }
- }
- void breakAllRelationshipsOfNodeID(int nID) {
- for (int i = relationships.size()-1; i >= 0; i--) {
- if (relationships.get(i).x == nID || relationships.get(i).y == nID) {
- int parID = int(relationships.get(i).x);
- int chiID = int(relationships.get(i).y);
- getNodeByID(parID).tryRemoveChild(chiID);
- getNodeByID(chiID).tryRemoveParent(parID);
- relationships.remove(i);
- }
- }
- }
- void breakSpecificRelationship(int pID, int cID) {
- getNodeByID(pID).tryRemoveChild(cID);
- getNodeByID(cID).tryRemoveParent(pID);
- for (int i = relationships.size()-1; i >= 0; i--) {
- if (relationships.get(i).x == pID && relationships.get(i).y == cID) {
- relationships.remove(i);
- }
- }
- }
- void clearAllRelationships() {
- for (Node n : nodes) {
- breakAllRelationshipsOfNode(n);
- }
- }
- Node checkLeftClickingNode() {
- Node temp = null;
- float distance = width*height;
- int nID = 0;
- for (Node n : nodes) {
- if (dist(mouseX, mouseY, n.pos.x, n.pos.y) < distance) {
- distance = dist(mouseX, mouseY, n.pos.x, n.pos.y);
- nID = n.nodeID;
- }
- }
- if (distance <= nodeSize/2.0) {
- temp = getNodeByID(nID);
- }
- return temp;
- }
- void selectNode() {
- float distance = width*height;
- int nID = 0;
- for (Node n : nodes) {
- if (dist(mouseX, mouseY, n.pos.x, n.pos.y) < distance) {
- distance = dist(mouseX, mouseY, n.pos.x, n.pos.y);
- nID = n.nodeID;
- }
- }
- if (distance <= nodeSize/2.0) {
- selected = getNodeByID(nID);
- }
- }
- void processSelection() {
- if (selected == null) {
- selectNode();
- } else {
- tryStartRelationship();
- selected = null;
- }
- }
- void tryStartRelationship() {
- float distance = width*height;
- int nID = 0;
- for (Node n : nodes) {
- if (dist(mouseX, mouseY, n.pos.x, n.pos.y) < distance) {
- distance = dist(mouseX, mouseY, n.pos.x, n.pos.y);
- nID = n.nodeID;
- }
- }
- if (distance <= nodeSize/2.0) {
- createOrRemoveRelationship(selected.nodeID, nID);
- }
- }
- void render() {
- update();
- stroke(255);
- fill(0, 0);
- for (PVector r : relationships) {
- Node parent = getNodeByID(int(r.x));
- Node child = getNodeByID(int(r.y));
- constructCurve(this, parent, child);
- }
- stroke(0);
- for (Node n : nodes) {
- color fColor;
- if (n == selected) {
- fColor = color(0, 0, 255);
- } else {
- fColor = color(255);
- }
- n.show(fColor);
- }
- stroke(0);
- for (PVector r : relationships) {
- PVector p1 = getNodeByID(int(r.x)).pos;
- PVector p2 = getNodeByID(int(r.y)).pos;
- PVector arrow = new PVector(p2.x-p1.x, p2.y-p1.y).normalize().mult(nodeSize/2.0);
- line(p1.x, p1.y, p1.x+arrow.x, p1.y+arrow.y);
- }
- }
- void update() {
- if (deleting) {
- deleteSelected();
- }
- if (movingNode != null) {
- movingNode.pos = new PVector(mouseX - (mouseX % (nodeSize/2.0)), mouseY - (mouseY % (nodeSize/2.0)));
- movingNode.updatePortPositions();
- }
- }
- void pulseQTYs() {
- for (Node n : nodes) {
- n.pulseToChildren();
- }
- for (Node n : nodes) {
- n.trueQTY = n.reserveQTY1 + n.reserveQTY2;
- n.reserveQTY1 = 0;
- n.reserveQTY2 = 0;
- }
- }
- void clearQTYs() {
- for (Node n : nodes) {
- n.trueQTY = 0;
- }
- }
- void deleteSelected() {
- if (selected != null) {
- for (int i = nodes.size()-1; i>=0; i--) {
- if (selected.nodeID == nodes.get(i).nodeID) {
- println("deleting");
- breakAllRelationshipsOfNodeID(selected.nodeID);
- selected = null;
- nodes.remove(i);
- deleting = false;
- break;
- }
- }
- }
- }
- void scheduleDelete() {
- deleting = true;
- }
- }
- void mousePressed() {
- if (mouseButton == LEFT) {
- if (nodeHandler.checkLeftClickingNode() == null) {
- nodeHandler.addNode(1, mouseX, mouseY);
- } else {
- nodeHandler.movingNode = nodeHandler.checkLeftClickingNode();
- }
- }
- if (mouseButton == RIGHT) {
- nodeHandler.processSelection();
- }
- }
- void mouseReleased() {
- if (mouseButton == LEFT) {
- nodeHandler.movingNode = null;
- }
- }
- void keyPressed() {
- if (key == ' ') {
- nodeHandler.pulseQTYs();
- }
- if (key == 'c') {
- nodeHandler.clearQTYs();
- }
- if (key == DELETE) {
- println("Attempting Delete");
- nodeHandler.scheduleDelete();
- }
- }
- PVector intersectionPoint(PVector origin, PVector departure, PVector destination, PVector approach) {
- float x1 = origin.x;
- float y1 = origin.y;
- float x2 = destination.x;
- float y2 = destination.y;
- float dx = departure.x;
- float dy = departure.y;
- float ax = approach.x;
- float ay = approach.y;
- float denominator = dx * ay - dy * ax;
- if (abs(denominator) < 0.00001) {
- return null;
- }
- float t = ((x2 - x1) * ay - (y2 - y1) * ax) / denominator;
- float intersectionX = x1 + t * dx;
- float intersectionY = y1 + t * dy;
- println(intersectionX);
- println(intersectionY);
- return new PVector(intersectionX, intersectionY);
- }
- void constructCurve(NHandler nh, Node p, Node c) {
- PVector parentPos = p.pos;
- PVector childPos = c.pos;
- String parentDirString = p.getSendingPortDirection(c);
- String childDirString = c.getReceivingPortDirection(p);
- PVector parentRectLowerBound = null;
- PVector parentRectUpperBound = null;
- PVector childRectLowerBound = null;
- PVector childRectUpperBound = null;
- PVector parentPort = null;
- PVector childPort = null;
- switch(parentDirString) {
- case "west":
- parentRectUpperBound = new PVector(parentPos.x - nh.nodeSize/2.0, parentPos.y + nh.nodeSize / 2.0);
- parentRectLowerBound = new PVector(parentPos.x - 10000, parentPos.y - nh.nodeSize / 2.0);
- parentPort = new PVector(parentPos.x - nh.nodeSize / 2.0, parentPos.y);
- break;
- case "south":
- parentRectUpperBound = new PVector(parentPos.x + nh.nodeSize / 2.0, parentPos.y + 10000);
- parentRectLowerBound = new PVector(parentPos.x - nh.nodeSize / 2.0, parentPos.y + nh.nodeSize/2.0);
- parentPort = new PVector(parentPos.x, parentPos.y + nh.nodeSize / 2.0);
- break;
- case "east":
- parentRectUpperBound = new PVector(parentPos.x + 10000, parentPos.y + nh.nodeSize / 2.0);
- parentRectLowerBound = new PVector(parentPos.x + nh.nodeSize/2.0, parentPos.y - nh.nodeSize / 2.0);
- parentPort = new PVector(parentPos.x + nh.nodeSize / 2.0, parentPos.y);
- break;
- default:
- parentRectUpperBound = new PVector(parentPos.x + nh.nodeSize / 2.0, parentPos.y + 10000);
- parentRectLowerBound = new PVector(parentPos.x - nh.nodeSize / 2.0, parentPos.y + nh.nodeSize/2.0);
- parentPort = new PVector(parentPos.x, parentPos.y + nh.nodeSize / 2.0);
- break;
- }
- switch(childDirString) {
- case "west":
- childRectUpperBound = new PVector(childPos.x - nh.nodeSize/2.0, childPos.y + nh.nodeSize / 2.0);
- childRectLowerBound = new PVector(childPos.x - 10000, childPos.y - nh.nodeSize / 2.0);
- childPort = new PVector(childPos.x - nh.nodeSize / 2.0, childPos.y);
- break;
- case "north":
- childRectUpperBound = new PVector(childPos.x + nh.nodeSize / 2.0, childPos.y - nh.nodeSize/2.0);
- childRectLowerBound = new PVector(childPos.x - nh.nodeSize / 2.0, childPos.y - 10000);
- childPort = new PVector(childPos.x, childPos.y - nh.nodeSize / 2.0);
- break;
- case "east":
- childRectUpperBound = new PVector(childPos.x + 10000, childPos.y + nh.nodeSize / 2.0);
- childRectLowerBound = new PVector(childPos.x + nh.nodeSize/2.0, childPos.y - nh.nodeSize / 2.0);
- childPort = new PVector(childPos.x + nh.nodeSize / 2.0, childPos.y);
- break;
- default:
- childRectUpperBound = new PVector(childPos.x + nh.nodeSize / 2.0, childPos.y - nh.nodeSize/2.0);
- childRectLowerBound = new PVector(childPos.x - nh.nodeSize / 2.0, childPos.y - 10000);
- childPort = new PVector(childPos.x, childPos.y - nh.nodeSize / 2.0);
- break;
- }
- //rect(parentRectLowerBound.x, parentRectLowerBound.y, parentRectUpperBound.x-parentRectLowerBound.x, parentRectUpperBound.y-parentRectLowerBound.y);
- //rect(childRectLowerBound.x, childRectLowerBound.y, childRectUpperBound.x-childRectLowerBound.x, childRectUpperBound.y-childRectLowerBound.y);
- PVector parentDir = new PVector(parentPort.x - parentPos.x, parentPort.y - parentPos.y).normalize();
- PVector childDir = new PVector(childPort.x - childPos.x, childPort.y - childPos.y).normalize();
- boolean overlapping = !(parentRectUpperBound.x <= childRectLowerBound.x || childRectUpperBound.x <= parentRectLowerBound.x || parentRectUpperBound.y <= childRectLowerBound.y || childRectUpperBound.y <= parentRectLowerBound.y);
- float iRate = p.itemRate();
- if (overlapping) {
- PVector intersect = intersectionPoint(parentPort, parentDir, childPort, childDir);
- PVector parentCP;
- PVector childCP;
- if (intersect == null) {
- parentCP = new PVector(parentPos.x + parentDir.x * nh.nodeSize, parentPos.y + parentDir.y * nh.nodeSize);
- childCP = new PVector(childPos.x + childDir.x * nh.nodeSize, childPos.y + childDir.y * nh.nodeSize);
- } else {
- parentCP = new PVector(intersect.x, intersect.y);
- childCP = new PVector(intersect.x, intersect.y);
- }
- println(iRate);
- float t = ((frameCount+((5000/iRate)))/5000.0*iRate)%1;
- float x1 = bezierPoint(parentPort.x, parentCP.x, childCP.x, childPort.x, t);
- float y1 = bezierPoint(parentPort.y, parentCP.y, childCP.y, childPort.y, t);
- circle(x1, y1, 10);
- bezier(parentPort.x, parentPort.y, parentCP.x, parentCP.y, childCP.x, childCP.y, childPort.x, childPort.y);
- } else {
- PVector midPoint = new PVector(parentPort.x + (childPort.x - parentPort.x)/2.0, parentPort.y + (childPort.y - parentPort.y)/2.0);
- PVector parentTarget = new PVector(constrain(midPoint.x, parentRectLowerBound.x, parentRectUpperBound.x), constrain(midPoint.y, parentRectLowerBound.y, parentRectUpperBound.y));
- PVector childTarget = new PVector(constrain(midPoint.x, childRectLowerBound.x, childRectUpperBound.x), constrain(midPoint.y, childRectLowerBound.y, childRectUpperBound.y));
- midPoint = new PVector(parentTarget.x + (childTarget.x - parentTarget.x)/2.0, parentTarget.y + (childTarget.y - parentTarget.y)/2.0);
- PVector parentCPDir = new PVector(parentTarget.x - midPoint.x, parentTarget.y - midPoint.y).normalize();
- PVector childCPDir = new PVector(childTarget.x - midPoint.x, childTarget.y - midPoint.y).normalize();
- PVector parentIntersect = intersectionPoint(parentPort, parentDir, parentTarget, parentCPDir);
- PVector childIntersect = intersectionPoint(childPort, childDir, childTarget, childCPDir);
- PVector parentCP;
- PVector childCP;
- if (parentIntersect == null || childIntersect == null) {
- parentCP = new PVector(parentPos.x + parentDir.x * nh.nodeSize, parentPos.y + parentDir.y * nh.nodeSize);
- childCP = new PVector(childPos.x + childDir.x * nh.nodeSize, childPos.y + childDir.y * nh.nodeSize);
- } else {
- parentCP = new PVector(parentIntersect.x, parentIntersect.y);
- childCP = new PVector(childIntersect.x, childIntersect.y);
- }
- float t1 = (((frameCount+((5000/iRate)))/5000.0*iRate))%3;
- float t2 = (((frameCount+((5000/iRate)))/5000.0*iRate)+1)%3;
- float t3 = (((frameCount+((5000/iRate)))/5000.0*iRate)+2)%3;
- fill(255, 0);
- bezier(parentPort.x, parentPort.y, parentCP.x, parentCP.y, parentCP.x, parentCP.y, parentTarget.x, parentTarget.y);
- if (0<=t1 && t1<=1) {
- float x1 = bezierPoint(parentPort.x, parentCP.x, parentCP.x, parentTarget.x, t1);
- float y1 = bezierPoint(parentPort.y, parentCP.y, parentCP.y, parentTarget.y, t1);
- circle(x1, y1, 10);
- }
- bezier(childPort.x, childPort.y, childCP.x, childCP.y, childCP.x, childCP.y, childTarget.x, childTarget.y);
- if (0<=t2 && t2<=1) {
- float x2 = bezierPoint(childTarget.x, childCP.x, childCP.x, childPort.x, t2);
- float y2 = bezierPoint(childTarget.y, childCP.y, childCP.y, childPort.y, t2);
- circle(x2, y2, 10);
- }
- bezier(parentTarget.x, parentTarget.y, parentTarget.x + (parentTarget.x - parentCP.x), parentTarget.y + (parentTarget.y - parentCP.y), childTarget.x + (childTarget.x - childCP.x), childTarget.y + (childTarget.y - childCP.y), childTarget.x, childTarget.y);
- if (0<=t3 && t3<=1) {
- float x3 = bezierPoint(parentTarget.x, parentTarget.x + (parentTarget.x - parentCP.x)/2.0, childTarget.x + (childTarget.x - childCP.x)/2.0, childTarget.x, t3);
- float y3 = bezierPoint(parentTarget.y, parentTarget.y + (parentTarget.y - parentCP.y)/2.0, childTarget.y + (childTarget.y - childCP.y)/2.0, childTarget.y, t3);
- circle(x3, y3, 10);
- }
- }
- }
Add Comment
Please, Sign In to add comment