Need help with converting java code to javascript code

Here is the code I found and I was trying to convert this into javascript code

    class pathfinder {
      PVector lastLocation;
      PVector location;
      PVector velocity;
      float diameter;
      boolean isFinished;
      pathfinder() { /// <---- this part  
        location = new PVector(0, 540);
            //location = new PVector(0, height/2);
        lastLocation = new PVector(location.x, location.y);
        velocity = new PVector(10, 0);
        diameter = random(10, 20);
        isFinished = false;
      }
      pathfinder(pathfinder parent) {  /// <---- this part 
        location = parent.location.get();
        lastLocation = parent.lastLocation.get();
        velocity = parent.velocity.get();
        diameter = parent.diameter * 0.62;
        isFinished = parent.isFinished;
        parent.diameter = diameter;
      }
      void update() {

        if(location.x > -10 & location.x < width + 10 & location.y > -10 & location.y < height + 10) {
          lastLocation.set(location.x, location.y);
          if (diameter > 0.2) {
            count ++;
            PVector bump = new PVector(random(-1, 1), random(-1, 1));
            velocity.normalize();
            bump.mult(0.2);
            velocity.mult(0.8);
            velocity.add(bump);
            velocity.mult(random(25, 50));
            location.add(velocity);
            if (random(0, 1) < 0.2) { // control length
              paths = (pathfinder[]) append(paths, new pathfinder(this));
            }
          } else {
            if(!isFinished) {
              isFinished = true;
              noStroke();
              fill(255, 255, 51, 100);
              ellipse(location.x, location.y, 10, 10);
              stroke(102, 255, 102, 200);
            }
          }
        }
      }
    } 

My javascript code

    let branchs = [];  

    function setup() {
      createCanvas(600, 400);
      background(0);
      fill(255);
      noStroke();
      let l = createVector(width/2,height);
      branchs.push(new Branch(l,20));
    }

    function draw() {
      for(let i=0;i<branchs.length;i++){ 
        branchs[i].update();
        ellipse(branchs[i].loc.x,branchs[i].loc.y,branchs[i].dia,branchs[i].dia); 
      } 
    }

    function Branch(loc,dia){
      this.loc = loc;
      this.dia = dia;
      this.vel = createVector(0,-1);
    }

    Branch.prototype.add = function(parent){ 
      let area = PI*sq(parent.dia/2);
      let newDia = sqrt(area/2/PI)*2;   
      append(branchs, new Branch(parent.loc,newDia));  
    }

    Branch.prototype.update = function(){
      if(this.dia>5){ 
        this.loc.add(this.vel) ;
        let bp = createVector(random(-1,1),random(-1,1));
        bp.mult(random(-0.5,0.5));
        this.vel.add(bp);
        this.vel.normalize();  
        this.dia-=0.005;
        if(random(1)<0.002){
        for(let i=0;i<random(1,6);i++){
            this.add(this);
          }
        }
      }
    } 

But I can't convert the java's double constructor ---

Answers

Sign In or Register to comment.