BoundingSquare.hpp

Go to the documentation of this file.
00001 /*
00002 SagaEngine library
00003 Copyright (c) 2002-2006 Skalden Studio AS
00004 
00005 This software is provided 'as-is', without any express or implied 
00006 warranty. In no event will the authors be held liable for any 
00007 damages arising from the use of this software.
00008 
00009 Permission is granted to distribute the library under the terms of the 
00010 Q Public License version 1.0. Be sure to read and understand the license
00011 before using the library. It should be included here, or you may read it
00012 at http://www.trolltech.com/products/qt/licenses/licensing/qpl
00013 
00014 The original version of this library can be located at:
00015 http://www.sagaengine.com/
00016 
00017 Rune Myrland
00018 rune@skalden.com
00019 */
00020 
00021 
00022 #ifndef base_bounds_BoundingSquare_hpp
00023 #define base_bounds_BoundingSquare_hpp
00024 
00025 #include "util/type/util_type.hpp"
00026 #include "util/math/CoorT.hpp"
00027 
00028 namespace se_core {
00029     class _SeCoreExport BoundingSquare {
00030     public:
00034         BoundingSquare() {
00035         }
00036 
00037 
00041         BoundingSquare(coor_t x, coor_t y, coor_t radius)
00042             : minX_(x - radius), minY_(y - radius), maxX_(x + radius), maxY_(y + radius) {
00043         }
00044 
00045 
00049         inline void setNull() {
00050             minX_ = 0; maxX_ = 0;
00051             minY_ = 0; maxY_ = 0;
00052         }
00053 
00054 
00058         bool isNull() {
00059             return (minX_ == maxX_ && minY_ == maxY_);
00060         }
00061 
00062 
00063         inline void setMin(coor_t x, coor_t y) {
00064             minX_ = x;
00065             minY_ = y;
00066         }
00067 
00068 
00069         inline void setMax(coor_t x, coor_t y) {
00070             maxX_ = x;
00071             maxY_ = y;
00072         }
00073 
00074 
00075         void merge(BoundingSquare &b) {
00076             if(isNull()) {
00077                 *this = b;
00078                 return;
00079             }
00080 
00081             if(b.minX_ < minX_) minX_ = b.minX_;
00082             if(b.minY_ < minY_) minY_ = b.minY_;
00083 
00084             if(b.maxX_ > maxX_) maxX_ = b.maxX_;
00085             if(b.maxY_ > maxY_) maxY_ = b.maxY_;
00086         }
00087 
00088 
00089         bool hasInside(coor_t x, coor_t y) {
00090             return (x >= minX_ && x < maxX_ && y >= minY_ && y < maxY_);
00091         }
00092 
00093 
00094         bool isTouching(BoundingSquare& b) {
00095             bool xInside = ((maxX_ < b.minX_) && (minX_ < b.maxX_))
00096                 || ((b.maxX_ < minX_) && (b.minX_ < minX_));
00097             bool yInside = ((maxY_ < b.minY_) && (minY_ < b.maxY_))
00098                 || ((b.maxY_ < minY_) && (b.minY_ < minY_));
00099             return (xInside && yInside);
00100         }
00101 
00102 
00103         coor_t radius() {
00104             return CoorT::half(maxY_ - minY_);
00105         }
00106 
00107 
00108         coor_t centerX() {
00109             return CoorT::half(minX_ + maxX_);
00110         }
00111 
00112 
00113         coor_t centerY() {
00114             return CoorT::half(minY_ + maxY_);
00115         }
00116 
00117 
00118     private:
00119         coor_t minX_, minY_;
00120         coor_t maxX_, maxY_;
00121     };
00122 }
00123 
00124 #endif

Home Page | SagaEngine trunk (updated nightly) reference generated Sun Dec 2 20:06:12 2007 by Doxygen version 1.3.9.1.

SourceForge.net Logo