difficult...
you could have a list of branches and when you click, you add a branch.
But where?
Either at the last branch (which gives not a tree but a path) or at the nearest (which is not reliable).
You can select one branch to attach the new one to (that is my solution; select with cursor left and right - the selected is white)
Greetings, Chrisir
[Edited: new version]
- use cursor left right to select
- and backspace to delete last one
- // from http://processing.org/learning/topics/arraylistclass.html
- //
- ArrayList <Branch> branches;
- int selectNumber=0;
- // =====================================================================
- void setup() {
- size(500, 500);
- // size(screen.width, screen.height);
- smooth();
- noStroke();
- // Create an empty ArrayList
- branches = new ArrayList();
- // Start by adding one element
- int BranchWidth = 48;
- branches.add(new Branch(width/2, height,
- width/2+13, height-40,
- BranchWidth, color (random(0, 255), random(0, 255), random(0, 255))));
- }
- void draw() {
- // delete
- background(111);
- // With an array, we say branches.length, with an ArrayList, we say branches.size()
- // The length of an ArrayList is dynamic
- // Notice how we are looping through the ArrayList backwards
- for (int i = branches.size()-1; i >= 0; i--) {
- // get from ArrayList
- Branch Branch = branches.get(i);
- Branch.display( i==selectNumber );
- }
- }
- // =====================================================================
- void mousePressed() {
- // A new Branch object is added to the ArrayList (by default to the end)
- //
- newBranch();
- }
- /*
- void mouseDragged() {
- // A new Branch object is added to the ArrayList (by default to the end)
- //
- newBranch();
- }
- */
- // =====================================================================
- //
- void keyPressed () {
- //
- if (key==CODED) {
- if (keyCode == LEFT) {
- selectNumber--;
- if (selectNumber<0) {
- selectNumber=0;
- }
- }
- if (keyCode == RIGHT) {
- selectNumber++;
- if (selectNumber>branches.size()) {
- selectNumber=branches.size();
- }
- }
- }
- else
- {
- if (key==BACKSPACE) {
- branches.remove(branches.size()-1);
- } // if
- } // else
- } // func
- //
- void newBranch() {
- // A new Branch object is added to the ArrayList (by default to the end)
- //
- // different ways to determine parent branch: -------------------------
- // Branch Branch = branches.get( branches.size () -1 );
- // Branch Branch = getNearestBranch();
- Branch Branch = getSelectedBranch();
- branches.add(new Branch(
- Branch.x2, Branch.y2,
- mouseX, mouseY,
- mouseY*.1,
- color (random(0, 255), random(0, 255), random(0, 255))));
- }
- //
- Branch getNearestBranch() {
- float smallestDist = 1000000;
- Branch buffer = null;
- for (int i = 0; i < branches.size(); i++) {
- // get from ArrayList
- Branch Branch = branches.get(i);
- if (dist(mouseX, mouseY, Branch.x2, Branch.y2) < smallestDist ) {
- smallestDist=dist(mouseX, mouseY, Branch.x2, Branch.y2);
- buffer = new Branch(2, 2, 2, 3, 2, 2);
- buffer= Branch;
- }
- }
- return buffer;
- }
- //
- Branch getSelectedBranch() {
- float smallestDist = 1000000;
- Branch buffer = null;
- for (int i = 0; i < branches.size(); i++) {
- // get from ArrayList
- Branch Branch = branches.get(i);
- if (i==selectNumber) {
- buffer = new Branch(2, 2, 2, 3, 2, 2);
- buffer= Branch;
- return buffer;
- }
- }
- return null;
- }
- //
- // =====================================================================
- // Simple Branch class
- class Branch {
- float x;
- float y;
- float x2;
- float y2;
- color myColor;
- //float speed;
- //float gravity;
- //float opacity;
- float w;
- //float life = 255;
- Branch(float tempX, float tempY,
- float tempX2, float tempY2,
- float tempW, color tempmyColor1) {
- x = tempX;
- y = tempY;
- x2 = tempX2;
- y2 = tempY2;
- w = tempW;
- if (w<1) {
- w=1;
- }
- myColor=tempmyColor1;
- }
- //
- void display( boolean isSelected ) {
- // Display
- stroke(myColor);
- if (isSelected) {
- stroke(255);
- }
- strokeWeight(w);
- line(x, y, x2, y2);
- }
- //
- } // class
- // =====================================================================