src/engine/eRectangle.cpp

Go to the documentation of this file.
00001 /*
00002 
00003 *************************************************************************
00004 
00005 ArmageTron -- Just another Tron Lightcycle Game in 3D.
00006 Copyright (C) 2005  by Manuel Moos (z-man@users.sf.net)
00007 and the AA DevTeam (see the file AUTHORS(.txt) in the main source directory)
00008 
00009 **************************************************************************
00010 
00011 This program is free software; you can redistribute it and/or
00012 modify it under the terms of the GNU General Public License
00013 as published by the Free Software Foundation; either version 2
00014 of the License, or (at your option) any later version.
00015 
00016 This program is distributed in the hope that it will be useful,
00017 but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 GNU General Public License for more details.
00020 
00021 You should have received a copy of the GNU General Public License
00022 along with this program; if not, write to the Free Software
00023 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00024   
00025 ***************************************************************************
00026 
00027 */
00028 
00029 #include "eRectangle.h"
00030 #include "tError.h"
00031 
00032 // ****************************************************************************
00033 // *
00034 // *   eRectangle
00035 // *
00036 // ****************************************************************************
00039 // ****************************************************************************
00040 
00041 eRectangle::eRectangle( void )
00042         : low_(1E+30, 1E+30), high_(-1E+30, -1E+30)
00043 {
00044 }
00045 
00046 // ****************************************************************************
00047 // *
00048 // *   eRectangle
00049 // *
00050 // ****************************************************************************
00055 // ****************************************************************************
00056 
00057 eRectangle::eRectangle( eCoord const & low, eCoord const & high )
00058         : low_(low), high_(high)
00059 {
00060 }
00061 
00062 // ****************************************************************************
00063 // *
00064 // *   Clear
00065 // *
00066 // ****************************************************************************
00069 // ****************************************************************************
00070 
00071 void eRectangle::Clear( void )
00072 {
00073     low_ = eCoord(1E+30, 1E+30);
00074     high_ = eCoord(-1E+30, -1E+30);
00075 }
00076 
00077 // ****************************************************************************
00078 // *
00079 // *   Include
00080 // *
00081 // ****************************************************************************
00086 // ****************************************************************************
00087 
00088 eRectangle & eRectangle::Include( eCoord const & point )
00089 {
00090     if (low_.x > point.x)
00091         low_.x = point.x;
00092     if (low_.y > point.y)
00093         low_.y = point.y;
00094     if (high_.x < point.x)
00095         high_.x = point.x;
00096     if (high_.y < point.y)
00097         high_.y = point.y;
00098 
00099     return *this;
00100 }
00101 
00102 // ****************************************************************************
00103 // *
00104 // *    Contains
00105 // *
00106 // ****************************************************************************
00111 // ****************************************************************************
00112 
00113 bool eRectangle::Contains( eCoord const & point ) const
00114 {
00115     return low_.x  <= point.x && low_.y  <= point.y &&
00116            high_.x >= point.x && high_.y >= point.y;
00117 }
00118 
00119 // clamp toClamp to reference in direction. Store the maximal move in max.
00120 static inline void se_Clamp( REAL& toClamp, REAL reference, REAL direction, REAL& max )
00121 {
00122     REAL move = (toClamp - reference)*direction;
00123     if (move > max)
00124         max = move;
00125     if (move > 0)
00126         toClamp = reference;
00127 }
00128 
00129 // ****************************************************************************
00130 // *
00131 // *    Clamp
00132 // *
00133 // ****************************************************************************
00138 // ****************************************************************************
00139 
00140 REAL eRectangle::Clamp( eCoord & point ) const
00141 {
00142     REAL ret = -1E+30;
00143     se_Clamp( point.x,  low_.x, -1, ret);
00144     se_Clamp( point.y,  low_.y, -1, ret);
00145     se_Clamp( point.x, high_.x,  1, ret);
00146     se_Clamp( point.y, high_.y,  1, ret);
00147 
00148     return ret;
00149 }
00150 
00151 // helper function clipping a line at one clipping line
00152 static inline REAL se_Clip( eCoord const & start, eCoord & stop,
00153                             eCoord const & reference, eCoord const & normal )
00154 {
00155     // calculate how far out on the other side of the reference point the stop point is
00156     REAL out = eCoord::F( normal, stop-reference );
00157     if ( out <= 0 )
00158         return 1; // nothing to clamp
00159 
00160     // calculate how far inside the startpoint lies
00161     REAL in = eCoord::F( normal, reference-start );
00162     if ( in < 0 )
00163         in = 0;
00164 
00165     // clip
00166     if ( out+in > 0 )
00167     {
00168         stop = (start * out + stop * in)*(1/(in+out));
00169         return in/(in+out);
00170     }
00171 
00172     return 1;
00173 }
00174 
00175 // ****************************************************************************
00176 // *
00177 // *    Clip
00178 // *
00179 // ****************************************************************************
00185 // ****************************************************************************
00186 
00187 REAL eRectangle::Clip( eCoord const & start, eCoord & stop ) const
00188 {
00189     // clip in x-direction
00190     REAL rx = se_Clip( start, stop, low_, eCoord(-1,0) );
00191     if ( rx >= 1 )
00192     {
00193         rx = se_Clip( start, stop, high_, eCoord(1,0) );
00194     }
00195 
00196     // clip in y-direction
00197     REAL ry = se_Clip( start, stop, low_, eCoord(0,-1) );
00198     if ( ry >= 1 )
00199     {
00200         ry = se_Clip( start, stop, high_, eCoord(0,1) );
00201     }
00202 
00203     return rx * ry;
00204 }
00205 
00206 // ****************************************************************************
00207 // *
00208 // *    GetPoint
00209 // *
00210 // ****************************************************************************
00215 // ****************************************************************************
00216 
00217 eCoord eRectangle::GetPoint( eCoord const & in ) const
00218 {
00219     eCoord diff = high_ - low_;
00220     return low_ + eCoord( diff.x * in.x, diff.y * in.y );
00221 }
00222 

Generated on Sat Mar 15 22:55:47 2008 for Armagetron Advanced by  doxygen 1.5.4