Parametric Design with Processing

ModelBuilder2

While PVector is built into processing, UVertex is from Modelbuilder2, which is an external library.

An Introduction to ModelBuilderMK2

  1. Import the library by clicking on Sketch>Import Library.
    import the library
    import unlekker.mb2.geo.*;
    import unlekker.mb2.util.*;
    import ec.util.*;
    

  2. Add the following instance variables:
    UVertex v, vDelta;
    float angleDelta;
    

  3. Create a setup() method. Inside the method:
    1. Set the size


    2. Set v to be a new UVertex at the center of the sketch.
       v=new UVertex(width/2, height/2);



    3. Set the vDelta variable to be a random UVertex point. To move at a higher speed, provide larger values:
        vDelta=new UVertex (random(1,3), 0);
      

    4. Rotate the point 360°:
      vDelta.rot(random(TWO_PI));

    5. Set angleDelta:
      angleDelta=radians(random(0.5, 1.5));

  4. Create a draw method:
    void draw(){
      
      
    }
    

  5. import unlekker.mb2.geo.*;
    import unlekker.mb2.util.*;
    import ec.util.*;
    
    UVertex v, vDelta;
    float angleDelta;
    
    void setup() {
      size(600, 600);
      v=new UVertex(width/2, height/2);
      //to move at higher speed, provide larger values
      vDelta=new UVertex (random(1, 3), 0);
      vDelta.rot(random(TWO_PI));
    
      angleDelta=radians(random(0.5, 1.5));
      if(random(100)<50){
        angleDelta=- angleDelta;
      }
    }
    void draw() {
      //add vDelta to v
      v.add(vDelta);
      vDelta.rot(angleDelta);
      ellipse(v.x,v.y,50,50);
      
    }
    




Complete program

import unlekker.mb2.geo.*;
import unlekker.mb2.util.*;
import ec.util.*;

UVertex v, vDelta;
float angleDelta;

void setup() {
  size(600, 600);
  v=new UVertex(width/2, height/2);
  //to move at higher speed, provide larger values
  vDelta=new UVertex (random(1, 3), 0);
  vDelta.rot(random(TWO_PI));

  angleDelta=radians(random(0.5, 1.5));
  if(random(100)<50){
    angleDelta=- angleDelta;
  }
}
void draw() {
  //add vDelta to v
  v.add(vDelta);
  vDelta.rot(angleDelta);
  ellipse(v.x,v.y,50,50);
  stroke(255,0,0);
  line(v.x,v.y, v.x+vDelta.x*20, v.y+vDelta.y*20);
  if(frameCount%100==0){
    newRotation();
  }
  if(v.x<0 ){
    v.x=width;
  }
    if(v.x>width ){
    v.x=0;
  }
   if(v.y<0 ){
    v.y=height;
  }
    if(v.y>height ){
    v.y=0;
  }
}
void newRotation(){
  angleDelta=radians(random(0.5, 1.5));
  if(random(100)<50){
    angleDelta=- angleDelta;
  }
  vDelta.norm(random(1,3));
}