topical media & game development

talk show tell print

graphic-processing-algorithm-Ch04-p102-p102.pde / pde



  int numShapes = 12;  //num on side of grid of shapes
  MyShape[] shape = new MyShape[numShapes*numShapes];
  MyGroup group;
  MyControl control;
  
  //*****************************************
  void setup(){
  
    size(300,400);
  
    for(int y=0; y<numShapes; y++){
      for(int x=0; x<numShapes; x++){
        shape[y*numShapes+x] = new MyShape(5,9.,30+x*20., 30+y*20.);
      }
    }
  
    group = new MyGroup(numShapes*numShapes, shape);
    group.move(10.,10.);
    control = new MyControl();
  }
  
  //*****************************************
  void draw( ){
    background(255);
    control.coordsDisplay.setText("x= " + mouseX + "   y= " + mouseY);
    group.draw();
  
  }
  
  int xfirst, yfirst;
  //*****************************************
  void mousePressed(){
  
    xfirst = mouseX;  // remember this point
    yfirst = mouseY;
  
    // Pick a shape
    for(int i=0; i<group.numShapes; i++)
      if(group.shapes[i].select((float)mouseX, (float)mouseY, 5.) == true){
        control.input.setText("Selected = " + i);
        break;
      }
  
  }
  
  //*****************************************
  void mouseDragged(){
  
    int xoff = mouseX - xfirst;  // get the offset
    int yoff = mouseY - yfirst;
    MyPoint ref = new MyPoint(0.,0.);
  
    for(int i=0; i<group.numShapes; i++)
      if(group.shapes[i].isSelected){
        ref = group.shapes[i].centroid();
        if(control.status.equals("Move"))
          group.shapes[i].move(( float)xoff, ( float)yoff);
        if(control.status.equals("Rotate"))
          group.shapes[i].rotate(( float)xoff, ref);
          if(control.status.equals("Scale"))
          group.shapes[i].scale((float)mouseX/(float)xfirst, (float)mouseY/(float)yfirst, ref);
      }
  
    xfirst = mouseX;  //remember this point
    yfirst = mouseY;
  
  } 
  
  


(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.