geometry.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/geometry.h
  3. // Purpose: Common Geometry Classes
  4. // Author: Stefan Csomor
  5. // Modified by:
  6. // Created: 08/05/99
  7. // Copyright: (c) 1999 Stefan Csomor
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_GEOMETRY_H_
  11. #define _WX_GEOMETRY_H_
  12. #include "wx/defs.h"
  13. #if wxUSE_GEOMETRY
  14. #include "wx/utils.h"
  15. #include "wx/gdicmn.h"
  16. #include "wx/math.h"
  17. class WXDLLIMPEXP_FWD_BASE wxDataInputStream;
  18. class WXDLLIMPEXP_FWD_BASE wxDataOutputStream;
  19. // clipping from Cohen-Sutherland
  20. enum wxOutCode
  21. {
  22. wxInside = 0x00 ,
  23. wxOutLeft = 0x01 ,
  24. wxOutRight = 0x02 ,
  25. wxOutTop = 0x08 ,
  26. wxOutBottom = 0x04
  27. };
  28. class WXDLLIMPEXP_CORE wxPoint2DInt
  29. {
  30. public :
  31. inline wxPoint2DInt();
  32. inline wxPoint2DInt( wxInt32 x , wxInt32 y );
  33. inline wxPoint2DInt( const wxPoint2DInt &pt );
  34. inline wxPoint2DInt( const wxPoint &pt );
  35. // noops for this class, just return the coords
  36. inline void GetFloor( wxInt32 *x , wxInt32 *y ) const;
  37. inline void GetRounded( wxInt32 *x , wxInt32 *y ) const;
  38. inline wxDouble GetVectorLength() const;
  39. wxDouble GetVectorAngle() const;
  40. inline void SetVectorLength( wxDouble length );
  41. void SetVectorAngle( wxDouble degrees );
  42. void SetPolarCoordinates( wxInt32 angle , wxInt32 length );
  43. // set the vector length to 1.0, preserving the angle
  44. inline void Normalize();
  45. inline wxDouble GetDistance( const wxPoint2DInt &pt ) const;
  46. inline wxDouble GetDistanceSquare( const wxPoint2DInt &pt ) const;
  47. inline wxInt32 GetDotProduct( const wxPoint2DInt &vec ) const;
  48. inline wxInt32 GetCrossProduct( const wxPoint2DInt &vec ) const;
  49. // the reflection of this point
  50. inline wxPoint2DInt operator-();
  51. inline wxPoint2DInt& operator=(const wxPoint2DInt& pt);
  52. inline wxPoint2DInt& operator+=(const wxPoint2DInt& pt);
  53. inline wxPoint2DInt& operator-=(const wxPoint2DInt& pt);
  54. inline wxPoint2DInt& operator*=(const wxPoint2DInt& pt);
  55. inline wxPoint2DInt& operator*=(wxDouble n);
  56. inline wxPoint2DInt& operator*=(wxInt32 n);
  57. inline wxPoint2DInt& operator/=(const wxPoint2DInt& pt);
  58. inline wxPoint2DInt& operator/=(wxDouble n);
  59. inline wxPoint2DInt& operator/=(wxInt32 n);
  60. inline operator wxPoint() const;
  61. inline bool operator==(const wxPoint2DInt& pt) const;
  62. inline bool operator!=(const wxPoint2DInt& pt) const;
  63. #if wxUSE_STREAMS
  64. void WriteTo( wxDataOutputStream &stream ) const;
  65. void ReadFrom( wxDataInputStream &stream );
  66. #endif // wxUSE_STREAMS
  67. wxInt32 m_x;
  68. wxInt32 m_y;
  69. };
  70. inline wxPoint2DInt operator+(const wxPoint2DInt& pt1 , const wxPoint2DInt& pt2);
  71. inline wxPoint2DInt operator-(const wxPoint2DInt& pt1 , const wxPoint2DInt& pt2);
  72. inline wxPoint2DInt operator*(const wxPoint2DInt& pt1 , const wxPoint2DInt& pt2);
  73. inline wxPoint2DInt operator*(wxInt32 n , const wxPoint2DInt& pt);
  74. inline wxPoint2DInt operator*(wxDouble n , const wxPoint2DInt& pt);
  75. inline wxPoint2DInt operator*(const wxPoint2DInt& pt , wxInt32 n);
  76. inline wxPoint2DInt operator*(const wxPoint2DInt& pt , wxDouble n);
  77. inline wxPoint2DInt operator/(const wxPoint2DInt& pt1 , const wxPoint2DInt& pt2);
  78. inline wxPoint2DInt operator/(const wxPoint2DInt& pt , wxInt32 n);
  79. inline wxPoint2DInt operator/(const wxPoint2DInt& pt , wxDouble n);
  80. inline wxPoint2DInt::wxPoint2DInt()
  81. {
  82. m_x = 0;
  83. m_y = 0;
  84. }
  85. inline wxPoint2DInt::wxPoint2DInt( wxInt32 x , wxInt32 y )
  86. {
  87. m_x = x;
  88. m_y = y;
  89. }
  90. inline wxPoint2DInt::wxPoint2DInt( const wxPoint2DInt &pt )
  91. {
  92. m_x = pt.m_x;
  93. m_y = pt.m_y;
  94. }
  95. inline wxPoint2DInt::wxPoint2DInt( const wxPoint &pt )
  96. {
  97. m_x = pt.x;
  98. m_y = pt.y;
  99. }
  100. inline void wxPoint2DInt::GetFloor( wxInt32 *x , wxInt32 *y ) const
  101. {
  102. if ( x )
  103. *x = m_x;
  104. if ( y )
  105. *y = m_y;
  106. }
  107. inline void wxPoint2DInt::GetRounded( wxInt32 *x , wxInt32 *y ) const
  108. {
  109. GetFloor(x, y);
  110. }
  111. inline wxDouble wxPoint2DInt::GetVectorLength() const
  112. {
  113. // cast needed MIPSpro compiler under SGI
  114. return sqrt( (double)(m_x)*(m_x) + (m_y)*(m_y) );
  115. }
  116. inline void wxPoint2DInt::SetVectorLength( wxDouble length )
  117. {
  118. wxDouble before = GetVectorLength();
  119. m_x = (wxInt32)(m_x * length / before);
  120. m_y = (wxInt32)(m_y * length / before);
  121. }
  122. inline void wxPoint2DInt::Normalize()
  123. {
  124. SetVectorLength( 1 );
  125. }
  126. inline wxDouble wxPoint2DInt::GetDistance( const wxPoint2DInt &pt ) const
  127. {
  128. return sqrt( GetDistanceSquare( pt ) );
  129. }
  130. inline wxDouble wxPoint2DInt::GetDistanceSquare( const wxPoint2DInt &pt ) const
  131. {
  132. return ( (pt.m_x-m_x)*(pt.m_x-m_x) + (pt.m_y-m_y)*(pt.m_y-m_y) );
  133. }
  134. inline wxInt32 wxPoint2DInt::GetDotProduct( const wxPoint2DInt &vec ) const
  135. {
  136. return ( m_x * vec.m_x + m_y * vec.m_y );
  137. }
  138. inline wxInt32 wxPoint2DInt::GetCrossProduct( const wxPoint2DInt &vec ) const
  139. {
  140. return ( m_x * vec.m_y - vec.m_x * m_y );
  141. }
  142. inline wxPoint2DInt::operator wxPoint() const
  143. {
  144. return wxPoint( m_x, m_y);
  145. }
  146. inline wxPoint2DInt wxPoint2DInt::operator-()
  147. {
  148. return wxPoint2DInt( -m_x, -m_y);
  149. }
  150. inline wxPoint2DInt& wxPoint2DInt::operator=(const wxPoint2DInt& pt)
  151. {
  152. if (this != &pt)
  153. {
  154. m_x = pt.m_x;
  155. m_y = pt.m_y;
  156. }
  157. return *this;
  158. }
  159. inline wxPoint2DInt& wxPoint2DInt::operator+=(const wxPoint2DInt& pt)
  160. {
  161. m_x = m_x + pt.m_x;
  162. m_y = m_y + pt.m_y;
  163. return *this;
  164. }
  165. inline wxPoint2DInt& wxPoint2DInt::operator-=(const wxPoint2DInt& pt)
  166. {
  167. m_x = m_x - pt.m_x;
  168. m_y = m_y - pt.m_y;
  169. return *this;
  170. }
  171. inline wxPoint2DInt& wxPoint2DInt::operator*=(const wxPoint2DInt& pt)
  172. {
  173. m_x = m_x * pt.m_x;
  174. m_y = m_y * pt.m_y;
  175. return *this;
  176. }
  177. inline wxPoint2DInt& wxPoint2DInt::operator/=(const wxPoint2DInt& pt)
  178. {
  179. m_x = m_x / pt.m_x;
  180. m_y = m_y / pt.m_y;
  181. return *this;
  182. }
  183. inline bool wxPoint2DInt::operator==(const wxPoint2DInt& pt) const
  184. {
  185. return m_x == pt.m_x && m_y == pt.m_y;
  186. }
  187. inline bool wxPoint2DInt::operator!=(const wxPoint2DInt& pt) const
  188. {
  189. return m_x != pt.m_x || m_y != pt.m_y;
  190. }
  191. inline wxPoint2DInt operator+(const wxPoint2DInt& pt1 , const wxPoint2DInt& pt2)
  192. {
  193. return wxPoint2DInt( pt1.m_x + pt2.m_x , pt1.m_y + pt2.m_y );
  194. }
  195. inline wxPoint2DInt operator-(const wxPoint2DInt& pt1 , const wxPoint2DInt& pt2)
  196. {
  197. return wxPoint2DInt( pt1.m_x - pt2.m_x , pt1.m_y - pt2.m_y );
  198. }
  199. inline wxPoint2DInt operator*(const wxPoint2DInt& pt1 , const wxPoint2DInt& pt2)
  200. {
  201. return wxPoint2DInt( pt1.m_x * pt2.m_x , pt1.m_y * pt2.m_y );
  202. }
  203. inline wxPoint2DInt operator*(wxInt32 n , const wxPoint2DInt& pt)
  204. {
  205. return wxPoint2DInt( pt.m_x * n , pt.m_y * n );
  206. }
  207. inline wxPoint2DInt operator*(wxDouble n , const wxPoint2DInt& pt)
  208. {
  209. return wxPoint2DInt( static_cast<wxInt32>(pt.m_x * n) ,
  210. static_cast<wxInt32>(pt.m_y * n) );
  211. }
  212. inline wxPoint2DInt operator*(const wxPoint2DInt& pt , wxInt32 n)
  213. {
  214. return wxPoint2DInt( pt.m_x * n , pt.m_y * n );
  215. }
  216. inline wxPoint2DInt operator*(const wxPoint2DInt& pt , wxDouble n)
  217. {
  218. return wxPoint2DInt( static_cast<wxInt32>(pt.m_x * n) ,
  219. static_cast<wxInt32>(pt.m_y * n) );
  220. }
  221. inline wxPoint2DInt operator/(const wxPoint2DInt& pt1 , const wxPoint2DInt& pt2)
  222. {
  223. return wxPoint2DInt( pt1.m_x / pt2.m_x , pt1.m_y / pt2.m_y );
  224. }
  225. inline wxPoint2DInt operator/(const wxPoint2DInt& pt , wxInt32 n)
  226. {
  227. return wxPoint2DInt( pt.m_x / n , pt.m_y / n );
  228. }
  229. inline wxPoint2DInt operator/(const wxPoint2DInt& pt , wxDouble n)
  230. {
  231. return wxPoint2DInt( static_cast<wxInt32>(pt.m_x / n) ,
  232. static_cast<wxInt32>(pt.m_y / n) );
  233. }
  234. // wxPoint2Ds represent a point or a vector in a 2d coordinate system
  235. class WXDLLIMPEXP_CORE wxPoint2DDouble
  236. {
  237. public :
  238. inline wxPoint2DDouble();
  239. inline wxPoint2DDouble( wxDouble x , wxDouble y );
  240. inline wxPoint2DDouble( const wxPoint2DDouble &pt );
  241. wxPoint2DDouble( const wxPoint2DInt &pt )
  242. { m_x = (wxDouble) pt.m_x ; m_y = (wxDouble) pt.m_y ; }
  243. wxPoint2DDouble( const wxPoint &pt )
  244. { m_x = (wxDouble) pt.x ; m_y = (wxDouble) pt.y ; }
  245. // two different conversions to integers, floor and rounding
  246. inline void GetFloor( wxInt32 *x , wxInt32 *y ) const;
  247. inline void GetRounded( wxInt32 *x , wxInt32 *y ) const;
  248. inline wxDouble GetVectorLength() const;
  249. wxDouble GetVectorAngle() const ;
  250. void SetVectorLength( wxDouble length );
  251. void SetVectorAngle( wxDouble degrees );
  252. void SetPolarCoordinates( wxDouble angle , wxDouble length );
  253. // set the vector length to 1.0, preserving the angle
  254. void Normalize();
  255. inline wxDouble GetDistance( const wxPoint2DDouble &pt ) const;
  256. inline wxDouble GetDistanceSquare( const wxPoint2DDouble &pt ) const;
  257. inline wxDouble GetDotProduct( const wxPoint2DDouble &vec ) const;
  258. inline wxDouble GetCrossProduct( const wxPoint2DDouble &vec ) const;
  259. // the reflection of this point
  260. inline wxPoint2DDouble operator-();
  261. inline wxPoint2DDouble& operator=(const wxPoint2DDouble& pt);
  262. inline wxPoint2DDouble& operator+=(const wxPoint2DDouble& pt);
  263. inline wxPoint2DDouble& operator-=(const wxPoint2DDouble& pt);
  264. inline wxPoint2DDouble& operator*=(const wxPoint2DDouble& pt);
  265. inline wxPoint2DDouble& operator*=(wxDouble n);
  266. inline wxPoint2DDouble& operator*=(wxInt32 n);
  267. inline wxPoint2DDouble& operator/=(const wxPoint2DDouble& pt);
  268. inline wxPoint2DDouble& operator/=(wxDouble n);
  269. inline wxPoint2DDouble& operator/=(wxInt32 n);
  270. inline bool operator==(const wxPoint2DDouble& pt) const;
  271. inline bool operator!=(const wxPoint2DDouble& pt) const;
  272. wxDouble m_x;
  273. wxDouble m_y;
  274. };
  275. inline wxPoint2DDouble operator+(const wxPoint2DDouble& pt1 , const wxPoint2DDouble& pt2);
  276. inline wxPoint2DDouble operator-(const wxPoint2DDouble& pt1 , const wxPoint2DDouble& pt2);
  277. inline wxPoint2DDouble operator*(const wxPoint2DDouble& pt1 , const wxPoint2DDouble& pt2);
  278. inline wxPoint2DDouble operator*(wxDouble n , const wxPoint2DDouble& pt);
  279. inline wxPoint2DDouble operator*(wxInt32 n , const wxPoint2DDouble& pt);
  280. inline wxPoint2DDouble operator*(const wxPoint2DDouble& pt , wxDouble n);
  281. inline wxPoint2DDouble operator*(const wxPoint2DDouble& pt , wxInt32 n);
  282. inline wxPoint2DDouble operator/(const wxPoint2DDouble& pt1 , const wxPoint2DDouble& pt2);
  283. inline wxPoint2DDouble operator/(const wxPoint2DDouble& pt , wxDouble n);
  284. inline wxPoint2DDouble operator/(const wxPoint2DDouble& pt , wxInt32 n);
  285. inline wxPoint2DDouble::wxPoint2DDouble()
  286. {
  287. m_x = 0.0;
  288. m_y = 0.0;
  289. }
  290. inline wxPoint2DDouble::wxPoint2DDouble( wxDouble x , wxDouble y )
  291. {
  292. m_x = x;
  293. m_y = y;
  294. }
  295. inline wxPoint2DDouble::wxPoint2DDouble( const wxPoint2DDouble &pt )
  296. {
  297. m_x = pt.m_x;
  298. m_y = pt.m_y;
  299. }
  300. inline void wxPoint2DDouble::GetFloor( wxInt32 *x , wxInt32 *y ) const
  301. {
  302. *x = (wxInt32) floor( m_x );
  303. *y = (wxInt32) floor( m_y );
  304. }
  305. inline void wxPoint2DDouble::GetRounded( wxInt32 *x , wxInt32 *y ) const
  306. {
  307. *x = (wxInt32) floor( m_x + 0.5 );
  308. *y = (wxInt32) floor( m_y + 0.5);
  309. }
  310. inline wxDouble wxPoint2DDouble::GetVectorLength() const
  311. {
  312. return sqrt( (m_x)*(m_x) + (m_y)*(m_y) ) ;
  313. }
  314. inline void wxPoint2DDouble::SetVectorLength( wxDouble length )
  315. {
  316. wxDouble before = GetVectorLength() ;
  317. m_x = (m_x * length / before) ;
  318. m_y = (m_y * length / before) ;
  319. }
  320. inline void wxPoint2DDouble::Normalize()
  321. {
  322. SetVectorLength( 1 );
  323. }
  324. inline wxDouble wxPoint2DDouble::GetDistance( const wxPoint2DDouble &pt ) const
  325. {
  326. return sqrt( GetDistanceSquare( pt ) );
  327. }
  328. inline wxDouble wxPoint2DDouble::GetDistanceSquare( const wxPoint2DDouble &pt ) const
  329. {
  330. return ( (pt.m_x-m_x)*(pt.m_x-m_x) + (pt.m_y-m_y)*(pt.m_y-m_y) );
  331. }
  332. inline wxDouble wxPoint2DDouble::GetDotProduct( const wxPoint2DDouble &vec ) const
  333. {
  334. return ( m_x * vec.m_x + m_y * vec.m_y );
  335. }
  336. inline wxDouble wxPoint2DDouble::GetCrossProduct( const wxPoint2DDouble &vec ) const
  337. {
  338. return ( m_x * vec.m_y - vec.m_x * m_y );
  339. }
  340. inline wxPoint2DDouble wxPoint2DDouble::operator-()
  341. {
  342. return wxPoint2DDouble( -m_x, -m_y);
  343. }
  344. inline wxPoint2DDouble& wxPoint2DDouble::operator=(const wxPoint2DDouble& pt)
  345. {
  346. if (this != &pt)
  347. {
  348. m_x = pt.m_x;
  349. m_y = pt.m_y;
  350. }
  351. return *this;
  352. }
  353. inline wxPoint2DDouble& wxPoint2DDouble::operator+=(const wxPoint2DDouble& pt)
  354. {
  355. m_x = m_x + pt.m_x;
  356. m_y = m_y + pt.m_y;
  357. return *this;
  358. }
  359. inline wxPoint2DDouble& wxPoint2DDouble::operator-=(const wxPoint2DDouble& pt)
  360. {
  361. m_x = m_x - pt.m_x;
  362. m_y = m_y - pt.m_y;
  363. return *this;
  364. }
  365. inline wxPoint2DDouble& wxPoint2DDouble::operator*=(const wxPoint2DDouble& pt)
  366. {
  367. m_x = m_x * pt.m_x;
  368. m_y = m_y * pt.m_y;
  369. return *this;
  370. }
  371. inline wxPoint2DDouble& wxPoint2DDouble::operator/=(const wxPoint2DDouble& pt)
  372. {
  373. m_x = m_x / pt.m_x;
  374. m_y = m_y / pt.m_y;
  375. return *this;
  376. }
  377. inline bool wxPoint2DDouble::operator==(const wxPoint2DDouble& pt) const
  378. {
  379. return wxIsSameDouble(m_x, pt.m_x) && wxIsSameDouble(m_y, pt.m_y);
  380. }
  381. inline bool wxPoint2DDouble::operator!=(const wxPoint2DDouble& pt) const
  382. {
  383. return !(*this == pt);
  384. }
  385. inline wxPoint2DDouble operator+(const wxPoint2DDouble& pt1 , const wxPoint2DDouble& pt2)
  386. {
  387. return wxPoint2DDouble( pt1.m_x + pt2.m_x , pt1.m_y + pt2.m_y );
  388. }
  389. inline wxPoint2DDouble operator-(const wxPoint2DDouble& pt1 , const wxPoint2DDouble& pt2)
  390. {
  391. return wxPoint2DDouble( pt1.m_x - pt2.m_x , pt1.m_y - pt2.m_y );
  392. }
  393. inline wxPoint2DDouble operator*(const wxPoint2DDouble& pt1 , const wxPoint2DDouble& pt2)
  394. {
  395. return wxPoint2DDouble( pt1.m_x * pt2.m_x , pt1.m_y * pt2.m_y );
  396. }
  397. inline wxPoint2DDouble operator*(wxDouble n , const wxPoint2DDouble& pt)
  398. {
  399. return wxPoint2DDouble( pt.m_x * n , pt.m_y * n );
  400. }
  401. inline wxPoint2DDouble operator*(wxInt32 n , const wxPoint2DDouble& pt)
  402. {
  403. return wxPoint2DDouble( pt.m_x * n , pt.m_y * n );
  404. }
  405. inline wxPoint2DDouble operator*(const wxPoint2DDouble& pt , wxDouble n)
  406. {
  407. return wxPoint2DDouble( pt.m_x * n , pt.m_y * n );
  408. }
  409. inline wxPoint2DDouble operator*(const wxPoint2DDouble& pt , wxInt32 n)
  410. {
  411. return wxPoint2DDouble( pt.m_x * n , pt.m_y * n );
  412. }
  413. inline wxPoint2DDouble operator/(const wxPoint2DDouble& pt1 , const wxPoint2DDouble& pt2)
  414. {
  415. return wxPoint2DDouble( pt1.m_x / pt2.m_x , pt1.m_y / pt2.m_y );
  416. }
  417. inline wxPoint2DDouble operator/(const wxPoint2DDouble& pt , wxDouble n)
  418. {
  419. return wxPoint2DDouble( pt.m_x / n , pt.m_y / n );
  420. }
  421. inline wxPoint2DDouble operator/(const wxPoint2DDouble& pt , wxInt32 n)
  422. {
  423. return wxPoint2DDouble( pt.m_x / n , pt.m_y / n );
  424. }
  425. // wxRect2Ds are a axis-aligned rectangles, each side of the rect is parallel to the x- or m_y- axis. The rectangle is either defined by the
  426. // top left and bottom right corner, or by the top left corner and size. A point is contained within the rectangle if
  427. // left <= x < right and top <= m_y < bottom , thus it is a half open interval.
  428. class WXDLLIMPEXP_CORE wxRect2DDouble
  429. {
  430. public:
  431. wxRect2DDouble()
  432. { m_x = m_y = m_width = m_height = 0; }
  433. wxRect2DDouble(wxDouble x, wxDouble y, wxDouble w, wxDouble h)
  434. { m_x = x; m_y = y; m_width = w; m_height = h; }
  435. /*
  436. wxRect2DDouble(const wxPoint2DDouble& topLeft, const wxPoint2DDouble& bottomRight);
  437. wxRect2DDouble(const wxPoint2DDouble& pos, const wxSize& size);
  438. wxRect2DDouble(const wxRect2DDouble& rect);
  439. */
  440. // single attribute accessors
  441. wxPoint2DDouble GetPosition() const
  442. { return wxPoint2DDouble(m_x, m_y); }
  443. wxSize GetSize() const
  444. { return wxSize((int) m_width, (int) m_height); }
  445. // for the edge and corner accessors there are two setters counterparts, the Set.. functions keep the other corners at their
  446. // position whenever sensible, the Move.. functions keep the size of the rect and move the other corners appropriately
  447. inline wxDouble GetLeft() const { return m_x; }
  448. inline void SetLeft( wxDouble n ) { m_width += m_x - n; m_x = n; }
  449. inline void MoveLeftTo( wxDouble n ) { m_x = n; }
  450. inline wxDouble GetTop() const { return m_y; }
  451. inline void SetTop( wxDouble n ) { m_height += m_y - n; m_y = n; }
  452. inline void MoveTopTo( wxDouble n ) { m_y = n; }
  453. inline wxDouble GetBottom() const { return m_y + m_height; }
  454. inline void SetBottom( wxDouble n ) { m_height += n - (m_y+m_height);}
  455. inline void MoveBottomTo( wxDouble n ) { m_y = n - m_height; }
  456. inline wxDouble GetRight() const { return m_x + m_width; }
  457. inline void SetRight( wxDouble n ) { m_width += n - (m_x+m_width) ; }
  458. inline void MoveRightTo( wxDouble n ) { m_x = n - m_width; }
  459. inline wxPoint2DDouble GetLeftTop() const
  460. { return wxPoint2DDouble( m_x , m_y ); }
  461. inline void SetLeftTop( const wxPoint2DDouble &pt )
  462. { m_width += m_x - pt.m_x; m_height += m_y - pt.m_y; m_x = pt.m_x; m_y = pt.m_y; }
  463. inline void MoveLeftTopTo( const wxPoint2DDouble &pt )
  464. { m_x = pt.m_x; m_y = pt.m_y; }
  465. inline wxPoint2DDouble GetLeftBottom() const
  466. { return wxPoint2DDouble( m_x , m_y + m_height ); }
  467. inline void SetLeftBottom( const wxPoint2DDouble &pt )
  468. { m_width += m_x - pt.m_x; m_height += pt.m_y - (m_y+m_height) ; m_x = pt.m_x; }
  469. inline void MoveLeftBottomTo( const wxPoint2DDouble &pt )
  470. { m_x = pt.m_x; m_y = pt.m_y - m_height; }
  471. inline wxPoint2DDouble GetRightTop() const
  472. { return wxPoint2DDouble( m_x+m_width , m_y ); }
  473. inline void SetRightTop( const wxPoint2DDouble &pt )
  474. { m_width += pt.m_x - ( m_x + m_width ); m_height += m_y - pt.m_y; m_y = pt.m_y; }
  475. inline void MoveRightTopTo( const wxPoint2DDouble &pt )
  476. { m_x = pt.m_x - m_width; m_y = pt.m_y; }
  477. inline wxPoint2DDouble GetRightBottom() const
  478. { return wxPoint2DDouble( m_x+m_width , m_y + m_height ); }
  479. inline void SetRightBottom( const wxPoint2DDouble &pt )
  480. { m_width += pt.m_x - ( m_x + m_width ); m_height += pt.m_y - (m_y+m_height);}
  481. inline void MoveRightBottomTo( const wxPoint2DDouble &pt )
  482. { m_x = pt.m_x - m_width; m_y = pt.m_y - m_height; }
  483. inline wxPoint2DDouble GetCentre() const
  484. { return wxPoint2DDouble( m_x+m_width/2 , m_y+m_height/2 ); }
  485. inline void SetCentre( const wxPoint2DDouble &pt )
  486. { MoveCentreTo( pt ); } // since this is impossible without moving...
  487. inline void MoveCentreTo( const wxPoint2DDouble &pt )
  488. { m_x += pt.m_x - (m_x+m_width/2) , m_y += pt.m_y -(m_y+m_height/2); }
  489. inline wxOutCode GetOutCode( const wxPoint2DDouble &pt ) const
  490. { return (wxOutCode) (( ( pt.m_x < m_x ) ? wxOutLeft : 0 ) +
  491. ( ( pt.m_x > m_x + m_width ) ? wxOutRight : 0 ) +
  492. ( ( pt.m_y < m_y ) ? wxOutTop : 0 ) +
  493. ( ( pt.m_y > m_y + m_height ) ? wxOutBottom : 0 )); }
  494. inline wxOutCode GetOutcode(const wxPoint2DDouble &pt) const
  495. { return GetOutCode(pt) ; }
  496. inline bool Contains( const wxPoint2DDouble &pt ) const
  497. { return GetOutCode( pt ) == wxInside; }
  498. inline bool Contains( const wxRect2DDouble &rect ) const
  499. { return ( ( ( m_x <= rect.m_x ) && ( rect.m_x + rect.m_width <= m_x + m_width ) ) &&
  500. ( ( m_y <= rect.m_y ) && ( rect.m_y + rect.m_height <= m_y + m_height ) ) ); }
  501. inline bool IsEmpty() const
  502. { return m_width <= 0 || m_height <= 0; }
  503. inline bool HaveEqualSize( const wxRect2DDouble &rect ) const
  504. { return wxIsSameDouble(rect.m_width, m_width) && wxIsSameDouble(rect.m_height, m_height); }
  505. inline void Inset( wxDouble x , wxDouble y )
  506. { m_x += x; m_y += y; m_width -= 2 * x; m_height -= 2 * y; }
  507. inline void Inset( wxDouble left , wxDouble top ,wxDouble right , wxDouble bottom )
  508. { m_x += left; m_y += top; m_width -= left + right; m_height -= top + bottom;}
  509. inline void Offset( const wxPoint2DDouble &pt )
  510. { m_x += pt.m_x; m_y += pt.m_y; }
  511. void ConstrainTo( const wxRect2DDouble &rect );
  512. inline wxPoint2DDouble Interpolate( wxInt32 widthfactor , wxInt32 heightfactor )
  513. { return wxPoint2DDouble( m_x + m_width * widthfactor , m_y + m_height * heightfactor ); }
  514. static void Intersect( const wxRect2DDouble &src1 , const wxRect2DDouble &src2 , wxRect2DDouble *dest );
  515. inline void Intersect( const wxRect2DDouble &otherRect )
  516. { Intersect( *this , otherRect , this ); }
  517. inline wxRect2DDouble CreateIntersection( const wxRect2DDouble &otherRect ) const
  518. { wxRect2DDouble result; Intersect( *this , otherRect , &result); return result; }
  519. bool Intersects( const wxRect2DDouble &rect ) const;
  520. static void Union( const wxRect2DDouble &src1 , const wxRect2DDouble &src2 , wxRect2DDouble *dest );
  521. void Union( const wxRect2DDouble &otherRect )
  522. { Union( *this , otherRect , this ); }
  523. void Union( const wxPoint2DDouble &pt );
  524. inline wxRect2DDouble CreateUnion( const wxRect2DDouble &otherRect ) const
  525. { wxRect2DDouble result; Union( *this , otherRect , &result); return result; }
  526. inline void Scale( wxDouble f )
  527. { m_x *= f; m_y *= f; m_width *= f; m_height *= f;}
  528. inline void Scale( wxInt32 num , wxInt32 denum )
  529. { m_x *= ((wxDouble)num)/((wxDouble)denum); m_y *= ((wxDouble)num)/((wxDouble)denum);
  530. m_width *= ((wxDouble)num)/((wxDouble)denum); m_height *= ((wxDouble)num)/((wxDouble)denum);}
  531. wxRect2DDouble& operator = (const wxRect2DDouble& rect);
  532. inline bool operator == (const wxRect2DDouble& rect) const
  533. { return wxIsSameDouble(m_x, rect.m_x) && wxIsSameDouble(m_y, rect.m_y) && HaveEqualSize(rect); }
  534. inline bool operator != (const wxRect2DDouble& rect) const
  535. { return !(*this == rect); }
  536. wxDouble m_x;
  537. wxDouble m_y;
  538. wxDouble m_width;
  539. wxDouble m_height;
  540. };
  541. // wxRect2Ds are a axis-aligned rectangles, each side of the rect is parallel to the x- or m_y- axis. The rectangle is either defined by the
  542. // top left and bottom right corner, or by the top left corner and size. A point is contained within the rectangle if
  543. // left <= x < right and top <= m_y < bottom , thus it is a half open interval.
  544. class WXDLLIMPEXP_CORE wxRect2DInt
  545. {
  546. public:
  547. wxRect2DInt() { m_x = m_y = m_width = m_height = 0; }
  548. wxRect2DInt( const wxRect& r ) { m_x = r.x ; m_y = r.y ; m_width = r.width ; m_height = r.height ; }
  549. wxRect2DInt(wxInt32 x, wxInt32 y, wxInt32 w, wxInt32 h) { m_x = x; m_y = y; m_width = w; m_height = h; }
  550. wxRect2DInt(const wxPoint2DInt& topLeft, const wxPoint2DInt& bottomRight);
  551. inline wxRect2DInt(const wxPoint2DInt& pos, const wxSize& size);
  552. inline wxRect2DInt(const wxRect2DInt& rect);
  553. // single attribute accessors
  554. wxPoint2DInt GetPosition() const { return wxPoint2DInt(m_x, m_y); }
  555. wxSize GetSize() const { return wxSize(m_width, m_height); }
  556. // for the edge and corner accessors there are two setters counterparts, the Set.. functions keep the other corners at their
  557. // position whenever sensible, the Move.. functions keep the size of the rect and move the other corners appropriately
  558. inline wxInt32 GetLeft() const { return m_x; }
  559. inline void SetLeft( wxInt32 n ) { m_width += m_x - n; m_x = n; }
  560. inline void MoveLeftTo( wxInt32 n ) { m_x = n; }
  561. inline wxInt32 GetTop() const { return m_y; }
  562. inline void SetTop( wxInt32 n ) { m_height += m_y - n; m_y = n; }
  563. inline void MoveTopTo( wxInt32 n ) { m_y = n; }
  564. inline wxInt32 GetBottom() const { return m_y + m_height; }
  565. inline void SetBottom( wxInt32 n ) { m_height += n - (m_y+m_height);}
  566. inline void MoveBottomTo( wxInt32 n ) { m_y = n - m_height; }
  567. inline wxInt32 GetRight() const { return m_x + m_width; }
  568. inline void SetRight( wxInt32 n ) { m_width += n - (m_x+m_width) ; }
  569. inline void MoveRightTo( wxInt32 n ) { m_x = n - m_width; }
  570. inline wxPoint2DInt GetLeftTop() const { return wxPoint2DInt( m_x , m_y ); }
  571. inline void SetLeftTop( const wxPoint2DInt &pt ) { m_width += m_x - pt.m_x; m_height += m_y - pt.m_y; m_x = pt.m_x; m_y = pt.m_y; }
  572. inline void MoveLeftTopTo( const wxPoint2DInt &pt ) { m_x = pt.m_x; m_y = pt.m_y; }
  573. inline wxPoint2DInt GetLeftBottom() const { return wxPoint2DInt( m_x , m_y + m_height ); }
  574. inline void SetLeftBottom( const wxPoint2DInt &pt ) { m_width += m_x - pt.m_x; m_height += pt.m_y - (m_y+m_height) ; m_x = pt.m_x; }
  575. inline void MoveLeftBottomTo( const wxPoint2DInt &pt ) { m_x = pt.m_x; m_y = pt.m_y - m_height; }
  576. inline wxPoint2DInt GetRightTop() const { return wxPoint2DInt( m_x+m_width , m_y ); }
  577. inline void SetRightTop( const wxPoint2DInt &pt ) { m_width += pt.m_x - ( m_x + m_width ); m_height += m_y - pt.m_y; m_y = pt.m_y; }
  578. inline void MoveRightTopTo( const wxPoint2DInt &pt ) { m_x = pt.m_x - m_width; m_y = pt.m_y; }
  579. inline wxPoint2DInt GetRightBottom() const { return wxPoint2DInt( m_x+m_width , m_y + m_height ); }
  580. inline void SetRightBottom( const wxPoint2DInt &pt ) { m_width += pt.m_x - ( m_x + m_width ); m_height += pt.m_y - (m_y+m_height);}
  581. inline void MoveRightBottomTo( const wxPoint2DInt &pt ) { m_x = pt.m_x - m_width; m_y = pt.m_y - m_height; }
  582. inline wxPoint2DInt GetCentre() const { return wxPoint2DInt( m_x+m_width/2 , m_y+m_height/2 ); }
  583. inline void SetCentre( const wxPoint2DInt &pt ) { MoveCentreTo( pt ); } // since this is impossible without moving...
  584. inline void MoveCentreTo( const wxPoint2DInt &pt ) { m_x += pt.m_x - (m_x+m_width/2) , m_y += pt.m_y -(m_y+m_height/2); }
  585. inline wxOutCode GetOutCode( const wxPoint2DInt &pt ) const
  586. { return (wxOutCode) (( ( pt.m_x < m_x ) ? wxOutLeft : 0 ) +
  587. ( ( pt.m_x >= m_x + m_width ) ? wxOutRight : 0 ) +
  588. ( ( pt.m_y < m_y ) ? wxOutTop : 0 ) +
  589. ( ( pt.m_y >= m_y + m_height ) ? wxOutBottom : 0 )); }
  590. inline wxOutCode GetOutcode( const wxPoint2DInt &pt ) const
  591. { return GetOutCode( pt ) ; }
  592. inline bool Contains( const wxPoint2DInt &pt ) const
  593. { return GetOutCode( pt ) == wxInside; }
  594. inline bool Contains( const wxRect2DInt &rect ) const
  595. { return ( ( ( m_x <= rect.m_x ) && ( rect.m_x + rect.m_width <= m_x + m_width ) ) &&
  596. ( ( m_y <= rect.m_y ) && ( rect.m_y + rect.m_height <= m_y + m_height ) ) ); }
  597. inline bool IsEmpty() const
  598. { return ( m_width <= 0 || m_height <= 0 ); }
  599. inline bool HaveEqualSize( const wxRect2DInt &rect ) const
  600. { return ( rect.m_width == m_width && rect.m_height == m_height ); }
  601. inline void Inset( wxInt32 x , wxInt32 y ) { m_x += x; m_y += y; m_width -= 2 * x; m_height -= 2 * y; }
  602. inline void Inset( wxInt32 left , wxInt32 top ,wxInt32 right , wxInt32 bottom )
  603. { m_x += left; m_y += top; m_width -= left + right; m_height -= top + bottom;}
  604. inline void Offset( const wxPoint2DInt &pt ) { m_x += pt.m_x; m_y += pt.m_y; }
  605. void ConstrainTo( const wxRect2DInt &rect );
  606. inline wxPoint2DInt Interpolate( wxInt32 widthfactor , wxInt32 heightfactor ) { return wxPoint2DInt( m_x + m_width * widthfactor , m_y + m_height * heightfactor ); }
  607. static void Intersect( const wxRect2DInt &src1 , const wxRect2DInt &src2 , wxRect2DInt *dest );
  608. inline void Intersect( const wxRect2DInt &otherRect ) { Intersect( *this , otherRect , this ); }
  609. inline wxRect2DInt CreateIntersection( const wxRect2DInt &otherRect ) const { wxRect2DInt result; Intersect( *this , otherRect , &result); return result; }
  610. bool Intersects( const wxRect2DInt &rect ) const;
  611. static void Union( const wxRect2DInt &src1 , const wxRect2DInt &src2 , wxRect2DInt *dest );
  612. void Union( const wxRect2DInt &otherRect ) { Union( *this , otherRect , this ); }
  613. void Union( const wxPoint2DInt &pt );
  614. inline wxRect2DInt CreateUnion( const wxRect2DInt &otherRect ) const { wxRect2DInt result; Union( *this , otherRect , &result); return result; }
  615. inline void Scale( wxInt32 f ) { m_x *= f; m_y *= f; m_width *= f; m_height *= f;}
  616. inline void Scale( wxInt32 num , wxInt32 denum )
  617. { m_x *= ((wxInt32)num)/((wxInt32)denum); m_y *= ((wxInt32)num)/((wxInt32)denum);
  618. m_width *= ((wxInt32)num)/((wxInt32)denum); m_height *= ((wxInt32)num)/((wxInt32)denum);}
  619. wxRect2DInt& operator = (const wxRect2DInt& rect);
  620. bool operator == (const wxRect2DInt& rect) const;
  621. bool operator != (const wxRect2DInt& rect) const;
  622. #if wxUSE_STREAMS
  623. void WriteTo( wxDataOutputStream &stream ) const;
  624. void ReadFrom( wxDataInputStream &stream );
  625. #endif // wxUSE_STREAMS
  626. wxInt32 m_x;
  627. wxInt32 m_y;
  628. wxInt32 m_width;
  629. wxInt32 m_height;
  630. };
  631. inline wxRect2DInt::wxRect2DInt( const wxRect2DInt &r )
  632. {
  633. m_x = r.m_x;
  634. m_y = r.m_y;
  635. m_width = r.m_width;
  636. m_height = r.m_height;
  637. }
  638. inline wxRect2DInt::wxRect2DInt( const wxPoint2DInt &a , const wxPoint2DInt &b)
  639. {
  640. m_x = wxMin( a.m_x , b.m_x );
  641. m_y = wxMin( a.m_y , b.m_y );
  642. m_width = abs( a.m_x - b.m_x );
  643. m_height = abs( a.m_y - b.m_y );
  644. }
  645. inline wxRect2DInt::wxRect2DInt( const wxPoint2DInt& pos, const wxSize& size)
  646. {
  647. m_x = pos.m_x;
  648. m_y = pos.m_y;
  649. m_width = size.x;
  650. m_height = size.y;
  651. }
  652. inline bool wxRect2DInt::operator == (const wxRect2DInt& rect) const
  653. {
  654. return (m_x==rect.m_x && m_y==rect.m_y &&
  655. m_width==rect.m_width && m_height==rect.m_height);
  656. }
  657. inline bool wxRect2DInt::operator != (const wxRect2DInt& rect) const
  658. {
  659. return !(*this == rect);
  660. }
  661. class WXDLLIMPEXP_CORE wxTransform2D
  662. {
  663. public :
  664. virtual ~wxTransform2D() { }
  665. virtual void Transform( wxPoint2DInt* pt )const = 0;
  666. virtual void Transform( wxRect2DInt* r ) const;
  667. virtual wxPoint2DInt Transform( const wxPoint2DInt &pt ) const;
  668. virtual wxRect2DInt Transform( const wxRect2DInt &r ) const ;
  669. virtual void InverseTransform( wxPoint2DInt* pt ) const = 0;
  670. virtual void InverseTransform( wxRect2DInt* r ) const ;
  671. virtual wxPoint2DInt InverseTransform( const wxPoint2DInt &pt ) const ;
  672. virtual wxRect2DInt InverseTransform( const wxRect2DInt &r ) const ;
  673. };
  674. #endif // wxUSE_GEOMETRY
  675. #endif // _WX_GEOMETRY_H_