topical media & game development

talk show tell print

graphic-processing-algorithm-Ch04-p105-MyGroup.pde / pde



  class MyGroup {
  
    // members of class
    MyShape[] shapes;           // array of shapes
    int numShapes;              //number of shapes
    boolean isSelected = false;     // is this shape selected?
  
    //********** Constructor
    MyGroup(int numInputShapes, MyShape[] inputShapes){
  
      numShapes = numInputShapes;
      shapes = new MyShape[numShapes];
  
      for(int i=0; i<numShapes; i++){
        shapes[i] = inputShapes[i];
        for(int j=0; j<shape[i].numSegments; j++)
        shape[i].segs[j].setColor(int(random(255)),   int(random(255)), int(random(255)));                                                 
      }
    }
  
    //********** Move
    void move(float xoff, float yoff){
  
      for(int i=0; i<numShapes; i++)
        shapes[i].move(xoff, yoff);
    }
    //********** Rotate
    void rotate (float angle, MyPoint ref) {
      for(int i=0; i<numShapes; i++)
        shapes[i].rotate(angle, ref);
    }
  
    //********** Scale
    void scale(float xs, float ys, MyPoint ref){
      for(int i=0; i<numShapes; i++)
        shapes[i].scale(xs, ys, ref);
    }
    void setColor(int r, int g, int b){
      for(int i=0; i<numShapes; i++)
        shapes[i].setColor(r, g, b);
    }
    //*********** centroid
    MyPoint centroid(){
      MyPoint c = new MyPoint(0.,0.);
      for(int i=0; i<numShapes; i++){
        c.x += shapes[i].centroid().x;
        c.y += shapes[i].centroid().y;
      }
      c.x /= numShapes;
      c.y /= numShapes;
      return c;
    }
    //*********** draw
    void draw( ){
  
      for(int i=0; i<numShapes; i++)
        shapes[i].draw();
  
    }
  
    //******** Select
    boolean select(float xpick, float ypick, float tolerance){
      for(int i=0; i<numShapes; i++){
        if(shapes[i].select(xpick, ypick, tolerance)==true){
          isSelected = true; 
          return true;
        }
        else {
          isSelected = false;
        }
      }
      return false;
    }
  
  }
  


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