topical media & game development

talk show tell print

graphic-processing-algorithm-Ch09-p246-MyGroup.pde / pde



  
  class MyGroup  {
    MySolid [] solids;
    int nsolids;
  
    MyGroup (){
      solids = new MySolid[0];
    }
    /*
    MyGroup(MySolid[] inSolids){
     numSolids = inSolids.length;
     solids = new MySolid[numSolids];
     for(int i=0; i<numSolids; i++)
     solids[i] = new MySolid(inSolids[i].faces);  
     
     }
     */
    void addSolids(MyPoint[] points, float hite){
      nsolids++;
      solids = (MySolid[])append(solids, new MySolid(points,hite));
    }
  
    //******************
    void draw(){
      sort();
      for(int i=0; i<nsolids; i++)
        solids[i].draw();
  
    }
    void sort(){
      float[] zc;
      zc = new float[nsolids];
      int knt;
      float centerz;
  
      // Calculate the centroids of each solid
      for(int i=0; i<nsolids; i++){
        centerz = 0;
        knt = 0;
        for(int j=0; j<solids[i].nfaces; j++){
          for(int k=0; k<solids[i].faces[j].npoints; k++){
            centerz += solids[i].faces[j].points[k].z;
            knt++;
          }
        }
        zc[i] = centerz/knt;
      }
  
      // Sorting the objects
      for(int i=0; i<nsolids; i++)
        for(int j=0; j<nsolids; j++)
          if(zc[i] > zc[j]){
            MySolid tobj = solids[i];
            solids[i] = solids[j];
            solids[j] = tobj;
            float temp = zc[i];
            zc[i] = zc[j];
            zc[j] = temp;
          }
    }
  
    //*************
    void rotatex (float angle, MyPoint ref) {
      for(int i=0; i<nsolids; i++)
        solids[i].rotatex(angle, ref);
    }
  
    //*************
    void rotatey (float angle, MyPoint ref) {
      for(int i=0; i<nsolids; i++)
        solids[i].rotatey(angle, ref);
    }
  
    //*************
    void rotatez (float angle, MyPoint ref) {
      for(int i=0; i<nsolids; i++)
        solids[i].rotatez(angle, ref);
    }
  
    //*************
    void scale(float xs, float ys, float zs, MyPoint ref) {
      for(int i=0; i<nsolids; i++)
        solids[i].scale(xs, ys, zs, ref);
    }
  
    //*************
    void move(float xoff, float yoff, float zoff){
      for(int i=0; i<nsolids; i++)
        solids[i].move(xoff, yoff, zoff);
    }
  
    void setSelected(boolean what){
      for(int i=0; i<nsolids; i++)
        solids[i].setSelected(what);
    }
     boolean pick(int xmouse, int ymouse){
      for(int i=0; i< nsolids; i++)
        if(solids[i].pick(xmouse,ymouse))
           return true;
      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.