topical media & game development

talk show tell print

graphic-processing-algorithm-Ch07-p162-p162.pde / pde



  float [] px = new float[0]; //temp array
  float [] py = new float[0];
  float [] gx = {
    -10,10,20,30,40}; //generator data
  float [] gy = {
    0, 0,-10, 0, 0};
  float [] bx = {
    0,100,200,300,400}; //base data
  float [] by = {
    200, 200,100,200, 200};
  void setup(){
    size(400,400);
  }
  void draw(){
    background(200);
    for(int i=1; i<bx.length; i++)
      if(bx[i]!=999 && bx[i-1]!=999) //skip
        line(bx[i-1],by[i-1],bx[i],by[i]);
  }
  void mousePressed(){
    px = expand (px,0); //empty px
    py = expand (py,0);
    for(int j=0; j<bx.length-1; j++){ //for all base lines
      if(bx[j]!=999 && bx[j+1]!=999) //skip if marked
        for(int i=0; i<gx.length; i++){ //for all generator lines
          float db = dist(bx[j],by[j],bx[j+1],by[j+1]); //get dist of each base segment
          float dg = dist(0,0,gx[gx.length-1],gy[gy.length-1]); //get the distance of the generator
          float x = gx[i] * db/dg; //divide to get the scale factor
          float y = gy[i] * db/dg;
          float angle = atan2(by[j+1]-by[j],bx[j+1]-bx[j]); //angle between origin and each point
          float tempx = x * cos(angle) - y * sin(angle); //rotate
          y = y * cos(angle) + x * sin(angle);
          x = tempx;
          x += bx[j]; //translate
          y += by[j];
          px = append(px,x); //add the newly transformed point
          py = append(py,y);
        }
      px = append(px,999); //mark the end of a polyline sequence with 999
      py = append(py,999);
    }
    //copy p to the base array
    bx = expand (bx,0);
    by = expand (by,0);
    for(int i=0; i<px.length; i++){
      bx = append(bx,px[i]);
      by = append(by,py[i]);
    }
  }
  


(C) Æliens 04/09/2009

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.