topical media & game development

talk show tell print

graphic-processing-algorithm-Ch04-p107-MyPoint.pde / pde



  class MyPoint {
  
    float x, y;  // members of class
    boolean isSelected = false;     // is this point selected?
    color c;
  
    //********** Constructor
    MyPoint(float xin, float yin){
  
      x = xin;
      y = yin;
    }
    //********** Move
    void move(float xoff, float yoff){
      x += xoff;
      y += yoff;
    }
    //********** Rotate
    void rotate (float angle, MyPoint ref) {
      float cosa, sina;
      cosa = cos(PI/180*angle);
      sina = sin(PI/180*angle);
      float newx =  (x-ref.x) * cosa - (y-ref.y) * sina + ref.x;
      float newy =  (y-ref.y) * cosa + (x-ref.x) * sina + ref.y;
      x = newx;
      y = newy;
    }
    //********** Scale
    void scale(float xs, float ys, MyPoint ref){
      x = (x-ref.x)*xs + ref.x;
      y = (y-ref.y)*ys + ref.y;
    }
    void setColor(int r, int g, int b){
      c = color(r,g,b);
    }
    void draw(){
      if(isSelected)
        stroke(255,0,0);  //red
      else
        stroke(c);  //blue
      rect((int)x, (int)y, 1, 1);
  
    }  
    //************* Select
    boolean select(float xpick, float ypick, float tolerance){
  
      if(abs(x - xpick) < tolerance  && 
        abs(y - ypick) < tolerance ) {
        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.