00001 // 00002 // Copyright (C) 2006 Institut fuer Telematik, Universitaet Karlsruhe (TH) 00003 // 00004 // This program is free software; you can redistribute it and/or 00005 // modify it under the terms of the GNU General Public License 00006 // as published by the Free Software Foundation; either version 2 00007 // of the License, or (at your option) any later version. 00008 // 00009 // This program is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with this program; if not, write to the Free Software 00016 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00017 // 00018 00024 #include <BoundingBox2D.h> 00025 00026 BoundingBox2D::BoundingBox2D() {} 00027 00028 BoundingBox2D::BoundingBox2D(Vector2D tl, Vector2D br) 00029 { 00030 this->tl = tl; 00031 this->br = br; 00032 } 00033 00034 BoundingBox2D::BoundingBox2D(double tlx, double tly, double brx, double bry) 00035 { 00036 tl.x = tlx; 00037 tl.y = tly; 00038 br.x = brx; 00039 br.y = bry; 00040 } 00041 00042 BoundingBox2D::BoundingBox2D(Vector2D center, double width) 00043 { 00044 tl.x = center.x - width * 0.5; 00045 tl.y = center.y + width * 0.5; 00046 br.x = center.x + width * 0.5; 00047 br.y = center.y - width * 0.5; 00048 } 00049 00050 bool BoundingBox2D::collide(const BoundingBox2D box) const 00051 { 00052 if(tl.x > box.br.x) 00053 return false; 00054 if(tl.y < box.br.y) 00055 return false; 00056 00057 if(br.x < box.tl.x) 00058 return false; 00059 if(br.y > box.tl.y) 00060 return false; 00061 00062 return true; 00063 } 00064 00065 bool BoundingBox2D::collide(const Vector2D p) const 00066 { 00067 if(p.x > tl.x && p.x < br.x && p.y < tl.y && p.y > br.y) 00068 return true; 00069 else 00070 return false; 00071 } 00072 00073 double BoundingBox2D::top() 00074 { 00075 return tl.y; 00076 } 00077 00078 double BoundingBox2D::bottom() 00079 { 00080 return br.y; 00081 } 00082 00083 double BoundingBox2D::left() 00084 { 00085 return tl.x; 00086 } 00087 00088 double BoundingBox2D::right() 00089 { 00090 return br.x; 00091 } 00092 00093 std::ostream& operator<<(std::ostream& Stream, const BoundingBox2D& box) 00094 { 00095 return Stream << box.tl << " - " << box.br; 00096 }