Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ArrayList<Node> nodes;
- Node selected;
- Node moving;
- void setup() {
- size(1200, 1200);
- ellipseMode(CENTER);
- textAlign(CENTER, CENTER);
- textSize(20);
- nodes = new ArrayList<Node>();
- }
- void draw() {
- background(51);
- if (moving != null) {
- moving.pos = new PVector(mouseX, mouseY);
- }
- for (Node n : nodes) {
- n.showLines();
- }
- for (Node n : nodes) {
- n.showOutput();
- }
- for (Node n : nodes) {
- n.showSelf();
- }
- }
- void mousePressed() {
- if (mouseButton == RIGHT) {
- if (selected == null) {
- for (Node n : nodes) {
- if (n.isInside(mouseX, mouseY)) {
- selected = n;
- }
- }
- } else {
- boolean noneFound = true;
- for (Node n : nodes) {
- if (n.isInside(mouseX, mouseY) && selected != n) {
- n.setParent(selected);
- n.setColor();
- selected.setColor();
- noneFound = true;
- }
- }
- if (noneFound) {
- selected = null;
- }
- }
- }
- if (mouseButton == LEFT) {
- for (Node n : nodes) {
- if (n.isInside(mouseX, mouseY)) {
- moving = n;
- }
- }
- }
- }
- void mouseReleased() {
- if (mouseButton == LEFT) {
- moving = null;
- }
- }
- void keyPressed() {
- if (key == ' ') {
- for (Node n : nodes) {
- n.pulse();
- }
- for (Node n : nodes) {
- n.settle();
- }
- }
- if (key == 'i') {
- nodes.add(new Node(100, mouseX, mouseY));
- }
- if (key == 'n') {
- nodes.add(new Node(0, mouseX, mouseY));
- }
- if (key == 'c') {
- nodes.clear();
- }
- if (key == 'a') {
- for (Node n : nodes) {
- n.resetAmount();
- }
- }
- if (key == 'r') {
- if (selected == null) {
- for (Node n : nodes) {
- n.resetConnections();
- }
- } else {
- selected.resetMyConnections();
- }
- }
- if (key == 'd') {
- if (selected != null) {
- selected.resetMyConnections();
- int temp = 0;
- for(int i = 0; i < nodes.size(); i++){
- if(selected == nodes.get(i)){
- temp = i;
- }
- }
- selected = null;
- nodes.remove(temp);
- }
- }
- }
- class Node {
- PVector pos;
- color col;
- Node parent1;
- Node parent2;
- Node child1;
- Node child2;
- float initialAmount;
- float tempAmount;
- float trueAmount;
- Node(float a, float x, float y) {
- trueAmount = a;
- initialAmount = a;
- tempAmount = 0;
- pos = new PVector(x, y);
- col = color(255);
- }
- void pulse() {
- tempAmount = 0;
- if (parent1 != null) {
- if (parent1.child2 == null) {
- tempAmount += parent1.trueAmount;
- } else {
- tempAmount += parent1.trueAmount / 2.0;
- }
- }
- if (parent2 != null) {
- if (parent2.child2 == null) {
- tempAmount += parent2.trueAmount;
- } else {
- tempAmount += parent2.trueAmount / 2.0;
- }
- }
- }
- void settle() {
- if (child1 == null && child2 == null) {
- trueAmount += tempAmount;
- } else {
- trueAmount = tempAmount;
- }
- tempAmount = 0;
- }
- void setParent(Node c) {
- if (parent1 == null) {
- parent1 = c;
- c.setChild(this);
- } else if ( parent2 == null && parent1 != c) {
- parent2 = c;
- c.setChild(this);
- } else if (parent1 != c && parent2 != c) {
- parent1 = parent2;
- parent2 = c;
- c.setChild(this);
- }
- }
- void setChild(Node c) {
- if (child1 == null) {
- child1 = c;
- } else if ( child2 == null) {
- child2 = c;
- } else {
- child1 = child2;
- child2 = c;
- }
- }
- void showSelf() {
- if (selected == this) {
- fill(0, 0, 255);
- } else {
- fill(col);
- }
- stroke(0);
- circle(pos.x, pos.y, 50);
- fill(0);
- text((int)trueAmount, pos.x, pos.y);
- }
- void showLines() {
- if (parent1 != null) {
- strokeWeight(2);
- stroke(255);
- line(parent1.pos.x, parent1.pos.y, pos.x, pos.y);
- }
- if (parent2 != null) {
- strokeWeight(2);
- stroke(255);
- line(parent2.pos.x, parent2.pos.y, pos.x, pos.y);
- }
- }
- void showOutput(){
- if (parent1 != null) {
- PVector edge = PVector.lerp(parent1.pos, pos, 25/pos.dist(parent1.pos));
- strokeWeight(2);
- stroke(255);
- fill(127, 0, 255);
- circle(edge.x, edge.y, 25);
- }
- if (parent2 != null) {
- PVector edge = PVector.lerp(parent2.pos, pos, 25/pos.dist(parent2.pos));
- strokeWeight(2);
- stroke(255);
- fill(127, 0, 255);
- circle(edge.x, edge.y, 25);
- }
- }
- boolean isInside(PVector p) {
- return dist(p.x, p.y, pos.x, pos.y) < 25;
- }
- boolean isInside(float x, float y) {
- return dist(x, y, pos.x, pos.y) < 25;
- }
- void resetAmount() {
- trueAmount = initialAmount;
- tempAmount = 0;
- }
- void resetConnections() {
- parent1 = null;
- parent2 = null;
- child1 = null;
- child2 = null;
- setColor();
- }
- void resetMyConnections() {
- if (parent1 != null) {
- if (parent1.child1 == this) {
- parent1.child1 = parent1.child2;
- }
- parent1.child2 = null;
- parent1.setColor();
- }
- if (parent2 != null) {
- if (parent2.child1 == this) {
- parent2.child1 = parent2.child2;
- }
- parent2.child2 = null;
- parent2.setColor();
- }
- if (child1 != null) {
- if (child1.parent1 == this) {
- child1.parent1 = child1.parent2;
- }
- child1.parent2 = null;
- child1.setColor();
- }
- if (child2 != null) {
- if (child2.parent1 == this) {
- child2.parent1 = child2.parent2;
- }
- child2.parent2 = null;
- child2.setColor();
- }
- parent1 = null;
- parent2 = null;
- child1 = null;
- child2 = null;
- setColor();
- println(parent1);
- println(parent2);
- println(child1);
- println(child2);
- }
- void setColor() {
- if (parent1 == null && parent2 == null && (child1 != null || child2 != null)) {
- col = color(70, 160, 255);
- }
- if ((parent1 != null || parent2 != null) && (child1 != null || child2 != null)) {
- col = color(255, 255, 70);
- }
- if ((parent1 != null || parent2 != null) && child1 == null && child2 == null) {
- col = color(255, 70, 70);
- }
- if (parent1 == null && parent2 == null && child1 == null && child2 == null) {
- col = color(255);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement