22 #ifndef TLP_RECTANGLE_H 
   23 #define TLP_RECTANGLE_H 
   24 #include <tulip/Vector.h> 
   25 #include <tulip/BoundingBox.h> 
   37 template<
typename Obj, 
typename OTYPE = 
double>
 
   38 struct Rectangle : 
public Array<Vector<Obj,2, OTYPE>,2> {
 
   52   Rectangle(
const Obj xmin, 
const Obj ymin, 
const Obj xmax, 
const Obj ymax) {
 
   64     (*this)[0][0] = b[0][0];
 
   65     (*this)[0][1] = b[0][1];
 
   66     (*this)[1][0] = b[1][0];
 
   67     (*this)[1][1] = b[1][1];
 
   75   Rectangle(
const Vector<Obj,2, OTYPE> &min, 
const Vector<Obj,2, OTYPE> &max) {
 
   84   bool intersect(
const Rectangle &r)
 const {
 
   85     assert(this->isValid());
 
   88     if ((*
this)[0][0] > r[1][0])  
return false;
 
   90     if ((*
this)[1][0] < r[0][0])  
return false;
 
   92     if ((*
this)[0][1] > r[1][1])  
return false;
 
   94     if ((*
this)[1][1] < r[0][1])  
return false;
 
  102   bool intersect(
const Rectangle &r, Rectangle &intersection)
 const {
 
  103     assert(this->isValid());
 
  106     if (!this->intersect(r)) 
return false;
 
  108     intersection[0][0] = std::max((*
this)[0][0] , r[0][0]);
 
  109     intersection[1][0] = std::min((*
this)[1][0] , r[1][0]);
 
  110     intersection[0][1] = std::max((*
this)[0][1] , r[0][1]);
 
  111     intersection[1][1] = std::min((*
this)[1][1] , r[1][1]);
 
  118   bool isValid()
 const {
 
  119     return (*
this)[0][0] <= (*this)[1][0] && (*this)[0][1] <= (*this)[1][1];
 
  125   bool isInside(
const Vector<Obj, 2, OTYPE> &p)
 const {
 
  128     if (p[0] > (*
this)[1][0]) 
return false;
 
  130     if (p[0] < (*
this)[0][0]) 
return false;
 
  132     if (p[1] > (*
this)[1][1]) 
return false;
 
  134     if (p[1] < (*
this)[0][1]) 
return false;
 
  142   bool isInside(
const Rectangle &r)
 const {
 
  146     if ((*
this)[0] == r[0] && (*this)[1] == r[1]) 
return true;
 
  148     if (this->isInside(r[0]) && this->isInside(r[1])) 
return true;
 
  157   void translate(
const tlp::Vector<Obj,2, OTYPE> &v) {
 
  168     return (*
this)[1][0] - (*this)[0][0];
 
  176     return (*
this)[1][1] - (*this)[0][1];
 
  182   Obj surface()
 const {
 
  184     return height() * width();
 
  191   Obj aspectRatio()
 const {
 
  194     if (std::max(height(), width()) < std::numeric_limits<Obj>::epsilon())
 
  197     return std::min(height(), width()) / std::max(height(), width());
 
  203   Vector<Obj, 2, OTYPE> center()
 const {
 
  205     return ((*
this)[0] + (*
this)[1]) / Obj(2);
 
  210 typedef Rectangle<double, long double> Rectd;
 
  211 typedef Rectangle<float, double> Rectf;
 
  212 typedef Rectangle<int, double> Recti;
 
  213 typedef Rectangle<unsigned int, double> Rectui;