topical media & game development

talk show tell print

graphic-processing-algorithm-Ch03-p80-MyShape.pde / pde



  class MyShape {
    MySegment[] segs; // members of class
    int numSegments;
    //Constructor
    MyShape(int numInputSegments, MySegment[] inputSegments){
      numSegments = numInputSegments;
      segs = new MySegment[numSegments];
      for(int i=0; i<numSegments; i++)
        segs[i] = inputSegments[i];
    }
    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 * (float)Math.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);
    }
    // Plot
    void plot(){
      for(int i=0; i<numSegments; i++)
        segs[i].plot();
    }
  }
  


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