3
*************************************************************************
5
ArmageTron -- Just another Tron Lightcycle Game in 3D.
6
Copyright (C) 2005 by Manuel Moos (z-man@users.sf.net)
7
and the AA DevTeam (see the file AUTHORS(.txt) in the main source directory)
9
**************************************************************************
11
This program is free software; you can redistribute it and/or
12
modify it under the terms of the GNU General Public License
13
as published by the Free Software Foundation; either version 2
14
of the License, or (at your option) any later version.
16
This program is distributed in the hope that it will be useful,
17
but WITHOUT ANY WARRANTY; without even the implied warranty of
18
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
GNU General Public License for more details.
21
You should have received a copy of the GNU General Public License
22
along with this program; if not, write to the Free Software
23
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25
***************************************************************************
29
#include "eRectangle.h"
32
// ****************************************************************************
36
// ****************************************************************************
39
// ****************************************************************************
41
eRectangle::eRectangle( void )
42
: low_(1E+30, 1E+30), high_(-1E+30, -1E+30)
46
// ****************************************************************************
50
// ****************************************************************************
52
//! @param low lowest part of the coordinates of the rectangle
53
//! @param high highest part of the coordinates of the rectangle
55
// ****************************************************************************
57
eRectangle::eRectangle( eCoord const & low, eCoord const & high )
58
: low_(low), high_(high)
62
// ****************************************************************************
66
// ****************************************************************************
69
// ****************************************************************************
71
void eRectangle::Clear( void )
73
low_ = eCoord(1E+30, 1E+30);
74
high_ = eCoord(-1E+30, -1E+30);
77
// ****************************************************************************
81
// ****************************************************************************
83
//! @param point the point to include
84
//! @return reference to this for chaining
86
// ****************************************************************************
88
eRectangle & eRectangle::Include( eCoord const & point )
94
if (high_.x < point.x)
96
if (high_.y < point.y)
102
// ****************************************************************************
106
// ****************************************************************************
108
//! @param point the point to test
109
//! @return true if the point lies inside
111
// ****************************************************************************
113
bool eRectangle::Contains( eCoord const & point ) const
115
return low_.x <= point.x && low_.y <= point.y &&
116
high_.x >= point.x && high_.y >= point.y;
119
// clamp toClamp to reference in direction. Store the maximal move in max.
120
static inline void se_Clamp( REAL& toClamp, REAL reference, REAL direction, REAL& max )
122
REAL move = (toClamp - reference)*direction;
129
// ****************************************************************************
133
// ****************************************************************************
135
//! @param point the point to clamp
136
//! @return distance the point needed to be moved
138
// ****************************************************************************
140
REAL eRectangle::Clamp( eCoord & point ) const
143
se_Clamp( point.x, low_.x, -1, ret);
144
se_Clamp( point.y, low_.y, -1, ret);
145
se_Clamp( point.x, high_.x, 1, ret);
146
se_Clamp( point.y, high_.y, 1, ret);
151
// helper function clipping a line at one clipping line
152
static inline REAL se_Clip( eCoord const & start, eCoord & stop,
153
eCoord const & reference, eCoord const & normal )
155
// calculate how far out on the other side of the reference point the stop point is
156
REAL out = eCoord::F( normal, stop-reference );
158
return 1; // nothing to clamp
160
// calculate how far inside the startpoint lies
161
REAL in = eCoord::F( normal, reference-start );
168
stop = (start * out + stop * in)*(1/(in+out));
175
// ****************************************************************************
179
// ****************************************************************************
181
//! @param start point (asserted to lie inside the rectangle) to serve as starting point
182
//! @param stop point to clip
183
//! @return amount left of the start-stop line segment (1 for no clipping)
185
// ****************************************************************************
187
REAL eRectangle::Clip( eCoord const & start, eCoord & stop ) const
189
// clip in x-direction
190
REAL rx = se_Clip( start, stop, low_, eCoord(-1,0) );
193
rx = se_Clip( start, stop, high_, eCoord(1,0) );
196
// clip in y-direction
197
REAL ry = se_Clip( start, stop, low_, eCoord(0,-1) );
200
ry = se_Clip( start, stop, high_, eCoord(0,1) );
206
// ****************************************************************************
210
// ****************************************************************************
212
//! @param in coordinates in the range of 0..1 of the point to get
213
//! @return point inside the rectangle
215
// ****************************************************************************
217
eCoord eRectangle::GetPoint( eCoord const & in ) const
219
eCoord diff = high_ - low_;
220
return low_ + eCoord( diff.x * in.x, diff.y * in.y );