topical media & game development

talk show tell print

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



  class MySegment {
  
    MyPoint start;  // members of class
    MyPoint end;
    color c;                 // default color
    color red = color(255,0,0);                 // selected color
    boolean isSelected = false;             // is this segment selected?
  
    //********** Constructor
    MySegment(MyPoint p1, MyPoint p2){
  
      start = new MyPoint(p1.x, p1.y);
      end   = new MyPoint(p2.x, p2.y);
     
    }
  
    //********** Move
    void move(float xoff, float yoff){
  
      start.move(xoff, yoff);
      end.move(xoff, yoff);
    }
  
    //********** Rotate
    void rotate (float angle, MyPoint ref) {
      start.rotate(angle, ref);
      end.rotate(angle, ref);
    }
  
    //********** Scale
    void scale(float xs, float ys, MyPoint ref){
      start.scale(xs, ys, ref);
      end.scale(xs, ys, ref);
    }
    
    //*********** centroid
    MyPoint centroid(){
      MyPoint c = new MyPoint(0.,0.);
      c.x = (start.x+end.x)/2.;
      c.y = (start.y+end.y)/2.;
      return c;
    }
    void setColor(int r, int g, int b){
      c = color(r,g,b);
    }
    //*********** draw
    void draw(){
  
      if(isSelected)
        stroke(red);
      else
        stroke(c);
      line((int)start.x, (int)start.y, (int)end.x, (int)end.y);
  
    }
  
    //******** Select
    boolean select(float xpick, float ypick, float tolerance){
  
      if(start.select(xpick, ypick, tolerance)==true  ||
        end.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.