topical media & game development

talk show tell print

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



  class MyShape {
  
    MySegment[] segs;  // members of class
    int numSegments;
    boolean isSelected = false;     // is this shape selected?
  
    //********** Constructor 1
    MyShape(int numInputSegments, MySegment[] inputSegments){
  
      numSegments = numInputSegments;
      segs = new MySegment[numSegments];
  
      for(int i=0; i<numSegments; i++)
        segs[i] = inputSegments[i];
    }
  
    //********** Constructor 1
    MyShape(int numSides, float radius, float xoff, float yoff){
  
      numSegments = numSides;
      segs = new MySegment[numSegments];
  
      //divide the full circle in nsides sections
      float angle = 2 * PI / numSegments;
  
      // create two points to store the segment points
      MyPoint p = new MyPoint(0.,0.);
      MyPoint pnext = new MyPoint(0.,0.);
  
      // loop to assign values to the points
      for(int i =0; i<numSegments; i++){
        p.x     = xoff + radius * sin(angle*i);
        p.y     = yoff + radius * cos(angle*i);
        pnext.x = xoff + radius * sin(angle*(i+1));
        pnext.y = yoff + radius * cos(angle*(i+1));
        segs[i] = new MySegment(p, pnext);
      }
  
    }
  
    //********** Move
    void move(float xoff, float yoff){
  
      for(int i=0; i<numSegments; i++)
        segs[i].move(xoff, yoff);
    }
  
    //********** Rotate
    void rotate (float angle, MyPoint ref) {
      for(int i=0; i<numSegments; i++)
        segs[i].rotate(angle, ref);
    }
  
    //********** Scale
    void scale(float xs, float ys, MyPoint ref){
      for(int i=0; i<numSegments; i++)
        segs[i].scale(xs, ys, ref);
    }
  
    //*********** centroid
    MyPoint centroid(){
      MyPoint c = new MyPoint(0.,0.);
      for(int i=0; i<numSegments; i++){
          c.x += segs[i].centroid().x;
          c.y += segs[i].centroid().y;
      }
      c.x /= numSegments;
      c.y /= numSegments;
      return c;
    }
    //*********** draw
    void draw(){
  
      for(int i=0; i<numSegments; i++)
        segs[i].draw();
    }
    //******** Select
    boolean select(float xpick, float ypick, float tolerance){
      for(int i=0; i<numSegments; i++){
        if(segs[i].select(xpick, ypick, tolerance)==true){
          isSelected = true;
          for(int j=0; j<numSegments; j++)                             
            segs[j].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.