00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "CeylanBipoint.h"
00028
00029
00030 #include "CeylanLogPlug.h"
00031 #include "CeylanOperators.h"
00032 #include "CeylanTextDisplayable.h"
00033
00034 #include "CeylanVector2.h"
00035 #include "CeylanMatrix2.h"
00036
00037 #ifdef CEYLAN_USES_CONFIG_H
00038 #include "CeylanConfig.h"
00039 #endif // CEYLAN_USES_CONFIG_H
00040
00041
00042 #include <iostream>
00043 using std::endl ;
00044
00045 #include <sstream>
00046 using std::ostringstream ;
00047
00048 #include <iomanip>
00049
00050
00051 using std::string ;
00052
00053
00054 using namespace Ceylan ;
00055 using namespace Ceylan::Maths::Linear ;
00056
00057
00058 using Ceylan::Maths::Real ;
00059
00060
00061
00062 Bipoint::Bipoint( Real x0, Real x1 )
00063 {
00064
00065 _coordinates[0] = x0 ;
00066 _coordinates[1] = x1 ;
00067
00068 }
00069
00070
00071
00072 Bipoint::~Bipoint() throw()
00073 {
00074
00075 }
00076
00077
00078
00079 void Bipoint::setTo( Real x0, Real x1 )
00080 {
00081
00082 _coordinates[0] = x0 ;
00083 _coordinates[1] = x1 ;
00084
00085 }
00086
00087
00088
00089 void Bipoint::setAllElementsTo( Real commonValue )
00090 {
00091
00092 for ( MatrixIndex i = 0 ; i < Dimensions ; i++ )
00093 {
00094 _coordinates[i] = 0 ;
00095 }
00096
00097 }
00098
00099
00100
00101
00102 Real Bipoint::getX() const
00103 {
00104 return _coordinates[ 0 ] ;
00105 }
00106
00107
00108
00109 Real Bipoint::getY() const
00110 {
00111 return _coordinates[ 1 ] ;
00112 }
00113
00114
00115
00116
00117 Real Bipoint::getElementAt( MatrixIndex index ) const
00118 {
00119
00120 #if CEYLAN_DEBUG
00121
00122 if ( index >= Dimensions )
00123 throw MathsException( "Bipoint::getElementAt: index out of bounds." ) ;
00124
00125 #endif // CEYLAN_DEBUG
00126
00127 return _coordinates[ index ] ;
00128
00129 }
00130
00131
00132
00133 void Bipoint::setElementAt( MatrixIndex index, Real newValue )
00134 {
00135
00136 #if CEYLAN_DEBUG
00137
00138 if ( index >= Dimensions )
00139 throw MathsException( "Bipoint::setElementAt: index out of bounds." ) ;
00140
00141 #endif // CEYLAN_DEBUG
00142
00143 _coordinates[ index ] = newValue ;
00144
00145 }
00146
00147
00148
00149 const string Bipoint::toString( VerbosityLevels level ) const
00150 {
00151
00152 string res ;
00153
00154 if ( TextDisplayable::GetOutputFormat() == TextDisplayable::html )
00155 {
00156
00157 res = "<table border=1>" ;
00158
00159 res += " <tr>\n" ;
00160
00161 for ( MatrixIndex i = 0; i < Dimensions; i++ )
00162 {
00163 res += " <tr>\n" ;
00164 res += " <td>" + Ceylan::toString( _coordinates[i] ) + "</td>" ;
00165 res += " </tr>\n" ;
00166 }
00167
00168 res += "</table>" ;
00169
00170
00171 return res ;
00172
00173 }
00174
00175
00176
00177 if ( level == high )
00178 {
00179
00180 ostringstream oss ;
00181
00182 oss.precision( Ceylan::DigitOutputPrecision ) ;
00183
00184 oss << endl ;
00185
00186 for ( MatrixIndex i = 0; i < Dimensions; i++ )
00187 {
00188 oss << "[ " << std::setw(5) << _coordinates[i] <<" ]" << endl ;
00189
00190 }
00191
00192 oss << endl ;
00193
00194 res = oss.str() ;
00195
00196 #if CEYLAN_DEBUG
00197
00198 if ( oss.fail() )
00199 {
00200 string message = "Bipoint::toString: conversion error." ;
00201 Log::LogPlug::error( message ) ;
00202 return message ;
00203 }
00204
00205 #endif // CEYLAN_DEBUG
00206
00207 return res ;
00208 }
00209 else
00210 {
00211
00212 res = "Bipoint: [ " ;
00213
00214 for ( MatrixIndex i = 0; i < Dimensions; i++ )
00215 {
00216 res += Ceylan::toString( _coordinates[i] )
00217 + ( ( i == Dimensions-1 ) ? " ]": " ; " ) ;
00218 }
00219
00220 return res ;
00221
00222 }
00223
00224
00225 return res ;
00226
00227 }
00228
00229
00230
00231 Real Bipoint::Distance( const Bipoint & first, const Bipoint & second )
00232 {
00233
00234 return Sqrt( Pow2( second.getX() - first.getX() )
00235 + Pow2( second.getY() - first.getY() ) ) ;
00236
00237 }
00238
00239
00240
00241 Real Bipoint::DistancePow2( const Bipoint & first, const Bipoint & second )
00242 {
00243
00244 return Pow2( second.getX() - first.getX() )
00245 + Pow2( second.getY() - first.getY() ) ;
00246
00247 }
00248
00249
00250
00251 bool Ceylan::Maths::Linear::operator == (
00252 const Bipoint & t1, const Bipoint & t2 )
00253 {
00254
00255 for ( MatrixIndex i = 0; i < Bipoint::Dimensions; i++ )
00256 {
00257
00258
00259
00260
00261
00262
00263 if ( ! AreRelativelyEqual<Real>( t1._coordinates[i],
00264 t2._coordinates[i] ) )
00265 return false ;
00266 }
00267
00268 return true ;
00269
00270 }
00271
00272
00273
00274 bool Ceylan::Maths::Linear::operator != ( const Bipoint & t1,
00275 const Bipoint & t2 )
00276 {
00277
00278 return ( ! ( t1 == t2 ) ) ;
00279
00280 }
00281
00282
00283
00284 Bipoint Ceylan::Maths::Linear::operator + ( const Bipoint & t ,
00285 const Vector2 & v )
00286 {
00287
00288 Bipoint result ;
00289
00290 for ( MatrixIndex i = 0 ; i < Vector2::Dimensions ; i++ )
00291 {
00292 result._coordinates[i] = t._coordinates[i] + v._vec[i] ;
00293 }
00294
00295 return result ;
00296
00297 }
00298
00299
00300
00301 Bipoint Ceylan::Maths::Linear::operator - ( const Bipoint & t,
00302 const Vector2 & v )
00303 {
00304
00305 Bipoint result ;
00306
00307 for ( MatrixIndex i = 0 ; i < Vector2::Dimensions ; i++ )
00308 {
00309 result._coordinates[i] = t._coordinates[i] - v._vec[i] ;
00310 }
00311
00312 return result ;
00313
00314 }
00315
00316
00317
00318 Vector2 Ceylan::Maths::Linear::vectorize( const Bipoint & t )
00319 {
00320
00321 Vector2 result ;
00322
00323 for ( MatrixIndex i = 0; i < Vector2::Dimensions; i++ )
00324 {
00325 result._vec[i] = t._coordinates[i] ;
00326 }
00327
00328 return result ;
00329
00330 }
00331
00332
00333
00334 Vector2 Ceylan::Maths::Linear::vectorize( const Bipoint & t1,
00335 const Bipoint & t2 )
00336 {
00337
00338 Vector2 result ;
00339
00340 for ( MatrixIndex i = 0; i < Vector2::Dimensions; i++ )
00341 {
00342 result._vec[i] = t2._coordinates[i] - t1._coordinates[i] ;
00343 }
00344
00345 return result ;
00346
00347 }
00348