00001 /* 00002 * Copyright (c) 2001 The AUTHORS 00003 * Romeu Andre' Pieritz, Ph.D. - romeu_pieritz@hotmail.com 00004 * Rafael Mendes, Eng. – mendes_rafael@yahoo.com 00005 * Rodrigo Ferraz de Andrade, Eng. – rferraz@iname.com 00006 * All rights reserved. 00007 * 00008 * Permission to use, copy and distribute this software and its 00009 * documentation for educational and personal use, without fee is hereby granted, 00010 * provided that the above copyright notice and the following 00011 * two paragraphs appear in all copies of this software. 00012 * 00013 * IN NO EVENT SHALL THE AUTHORS BE LIABLE TO ANY PARTY FOR 00014 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT 00015 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE AUTHORS 00016 * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00017 * 00018 * THE AUTHORS SPECIFICALLY DISCLAIMS ANY WARRANTIES, 00019 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 00020 * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HERE UNDER IS 00021 * ON AN "AS IS" BASIS, AND THE AUTHORS HAVE NO OBLIGATION TO 00022 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. 00023 * 00024 * SINMEC Lab. - CFD Sinflow Project - http://www.sinmec.ufsc.br/cfd 00025 */ 00026 00027 00028 // File Define 00029 #ifndef __CSFL_SYS_GEOM_DLINE_H__ 00030 #define __CSFL_SYS_GEOM_DLINE_H__ 00031 00032 // Include 00033 #include <csfl/sys/geom/dpoint.h> 00034 00035 00036 // Namespace 00037 namespace csfl { 00038 00039 00040 class IDLine 00044 { 00045 public: 00046 00047 static const IDLine unit; 00048 00052 IDLine( const IDPoint _p1 = IDPoint::zero, const IDPoint _p2 = IDPoint::zero) 00053 : p1(_p1), p2(_p2) {} 00054 ~IDLine(){} 00055 00056 00058 bool isPointOver( const IDPoint &_p , const double & = 10e-10) const; 00059 00061 double CalculateX( const double &_y ) const; 00062 00064 double CalculateY( const double &_x ) const; 00065 00067 inline double Xmin() const 00068 { return ( p1.x < p2.x ) ? p1.x : p2.x;} 00069 00071 inline double Xmax() const 00072 { return ( p1.x > p2.x ) ? p1.x : p2.x;} 00073 00075 inline double Ymin() const 00076 { return ( p1.y < p2.y ) ? p1.y : p2.y;} 00077 00079 inline double Ymax() const 00080 { return ( p1.y > p2.y ) ? p1.y : p2.y;} 00081 00087 bool isLineVertical( const double & ToleranceAngle = 0.5) const; 00088 00094 bool isLineHorizontal( const double & ToleranceAngle = 0.5 ) const; 00095 00100 double Angle() const; 00101 00103 inline double LineLenght() 00104 { return sqrt( ( p1.x - p2.x )*( p1.x - p2.x ) + 00105 ( p1.y - p2.y )*( p1.y - p2.y ) ); } 00106 00108 inline IDPoint LineCenter( const IDPoint & _p1, const IDPoint &_p2 ) 00109 { return IDPoint( ( ( p1.x + p2.x ) / 2.0 ), 00110 ( ( p1.y + p2.y ) / 2.0) ); } 00111 00113 double DistanceToObject( const IDPoint & p ) const; 00114 00115 00116 public: 00117 IDPoint p1, p2; 00118 00119 }; 00120 //============================================================================== 00121 00122 00123 00124 00125 inline bool AreParallel( const IDLine & _l1, const IDLine & _l2 ) 00126 { 00127 00128 double val1, val2; 00129 00130 if( _l1.isLineHorizontal() != _l2.isLineHorizontal() ) 00131 return false; 00132 00133 if( _l1.isLineVertical() != _l2.isLineVertical() ) 00134 return false; 00135 00136 00137 val1 = (_l1.p2.y - _l1.p1.y )/( _l1.p2.x - _l1.p1.x ); 00138 val2 = (_l2.p2.y - _l2.p1.y )/( _l2.p2.x - _l2.p1.x ); 00139 00140 if( val1 == val2 ) 00141 return true; 00142 else 00143 return false; 00144 00145 } 00146 00147 //verify if the two line segments are parallel and have a intersection region 00148 inline bool AreCoincidental( const IDLine & _l1, const IDLine & _l2 ) 00149 { 00150 00151 if( !AreParallel( _l1, _l2 ) ) 00152 return false; 00153 00154 if( _l1.isPointOver( _l2.p1 ) || _l1.isPointOver( _l2.p2 )) 00155 return true; 00156 else 00157 if( _l2.isPointOver( _l1.p1 ) || _l2.isPointOver( _l1.p2 )) 00158 return true; 00159 else 00160 return false; 00161 00162 } 00163 00164 00165 // Verify if the two line segments have a commun point 00166 inline bool Intersection( const IDLine & _l1, const IDLine & _l2 ) 00167 { 00168 00169 if( AreCoincidental( _l1, _l2 )) 00170 return true; 00171 00172 if( AreParallel( _l1, _l2 )) 00173 return false; 00174 00175 00176 double val1, val2, val3, x, y1, y2; 00177 00178 if( !_l2.isLineVertical() ){ 00179 00180 val1 = _l1.p2.y - _l1.p1.y; 00181 val2 = ( _l1.p2.x - _l1.p1.x ) / ( _l2.p2.x - _l2.p1.x ); 00182 val3 = (_l2.p2.y - _l2.p1.y ) * val2; 00183 00184 x = ( _l1.p1.x * _l1.p2.y ) - ( _l1.p2.x * _l1.p1.y ) - 00185 ( _l2.p1.x * _l2.p2.y )* val2 + ( _l2.p2.x * _l2.p1.y )*val2; 00186 00187 x /= (val1 - val3 ); 00188 00189 y2 = _l2.CalculateY( x ); 00190 00191 if ( _l1.isLineVertical() ) 00192 y1 = y2; 00193 else 00194 y1 = _l1.CalculateY( x ); 00195 00196 return ( _l1.isPointOver( IDPoint(x, y1 )) && 00197 _l2.isPointOver( IDPoint(x, y2) )) ? true : false; 00198 00199 } 00200 00201 else{ 00202 val1 = _l2.p2.y - _l2.p1.y; 00203 val2 = ( _l2.p2.x - _l2.p1.x ) / ( _l1.p2.x - _l1.p1.x ); 00204 val3 = (_l1.p2.y - _l1.p1.y ) * val2; 00205 00206 x = ( _l2.p1.x * _l2.p2.y ) - ( _l2.p2.x * _l2.p1.y ) - 00207 ( _l1.p1.x * _l1.p2.y )* val2 + ( _l1.p2.x * _l1.p1.y )*val2; 00208 00209 x /= (val1 - val3 ); 00210 00211 y1 = y2 = _l1.CalculateY( x ); 00212 00213 return ( _l1.isPointOver( IDPoint(x, y1 )) && 00214 _l2.isPointOver( IDPoint(x, y2) )) ? true : false; 00215 00216 } 00217 00218 } 00219 00220 } // csfl namespace 00221 00222 #endif // __CSFL_SYS_GEOM_DLINE_H__
CFD Project
|
Documents |
Downloads |
Contact us |
Use Terms |
|
|||
© SINMEC/EMC/UFSC,
2001. |
All rights
reserved. |
Last Update: Jan. 18, 2002 |
|