Testlimits – the code

//  test if points or lines are in the limits (+-xRange,+-yRange)
  void showRange(){
    line(xRange,yRange,-xRange,yRange);
    line(xRange,-yRange,-xRange,-yRange);
    line(xRange,yRange,xRange,-yRange);
    line(-xRange,yRange,-xRange,-yRange);
  }
  
  boolean inXRange(float x){
    return (abs(x)<xRange+small);
  }
  
  boolean inYRange(float y){
    return (abs(y)<yRange+small);
  }
  
  boolean inside(Vector v){
    return (inXRange(v.x)&&inYRange(v.y));
  }

  boolean inside(Point p){
    return inside(p.vec);
  }
  
  boolean intersectionWithBorder(Vector a,Vector b){
    if (a.x<-xRange){                  //intersection with border x=-xRange and a.x < xRange, exchange a and b to get other possibility
      if(b.x<-xRange) return false;
      if(inYRange(a.y+(b.y-a.y)*(-xRange-a.x)/(b.x-a.x))) return true;
    }
    if (a.x>xRange){                  //intersection with border x=xRange
      if(b.x>xRange) return false;
      if(inYRange(a.y+(b.y-a.y)*(xRange-a.x)/(b.x-a.x))) return true;
    }
    if (a.y<-yRange){                  //intersection with border y=-yRange
      if(b.y<-yRange) return false;
      if(inXRange(a.x+(b.x-a.x)*(-yRange-a.y)/(b.y-a.y))) return true;
    }
    if (a.y>yRange){                  //intersection with border y=yRange
      if(b.y>yRange) return false;
      if(inXRange(a.x+(b.x-a.x)*(yRange-a.y)/(b.y-a.y))) return true;
    }
    return false;
  }

  //  test if some piece of the line between a and b is inside the range-rectangle
  
  boolean inside(Vector a,Vector b){
    return (inside(a)||inside(b)||intersectionWithBorder(a,b)||intersectionWithBorder(b,a)); 
    }
This entry was posted in programming and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s