You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

236 lines
7.8 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2014 Jean-Pierre Charras, jp.charras at wanadoo.fr
  5. * Copyright (C) 2004-2014 KiCad Developers, see change_log.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. /**
  25. * @file class_eda_rect.h
  26. */
  27. #ifndef CLASS_EDA_RECT_H
  28. #define CLASS_EDA_RECT_H
  29. #include <wx/gdicmn.h>
  30. /**
  31. * Class EDA_RECT
  32. * handles the component boundary box.
  33. * This class is similar to wxRect, but some wxRect functions are very curious,
  34. * and are working only if dimensions are >= 0 (not always the case in KiCad)
  35. * and also KiCad needs some specific method.
  36. * so I prefer this more suitable class
  37. */
  38. class EDA_RECT
  39. {
  40. private:
  41. wxPoint m_Pos; // Rectangle Origin
  42. wxSize m_Size; // Rectangle Size
  43. public:
  44. EDA_RECT() { };
  45. EDA_RECT( const wxPoint& aPos, const wxSize& aSize ) :
  46. m_Pos( aPos ),
  47. m_Size( aSize )
  48. { }
  49. wxPoint Centre() const
  50. {
  51. return wxPoint( m_Pos.x + ( m_Size.x >> 1 ),
  52. m_Pos.y + ( m_Size.y >> 1 ) );
  53. }
  54. /**
  55. * Function Move
  56. * moves the rectangle by the \a aMoveVector.
  57. * @param aMoveVector A wxPoint that is the value to move this rectangle
  58. */
  59. void Move( const wxPoint& aMoveVector );
  60. /**
  61. * Function Normalize
  62. * ensures that the height ant width are positive.
  63. */
  64. void Normalize();
  65. /**
  66. * Function Contains
  67. * @param aPoint = the wxPoint to test
  68. * @return true if aPoint is inside the boundary box. A point on a edge is seen as inside
  69. */
  70. bool Contains( const wxPoint& aPoint ) const;
  71. /**
  72. * Function Contains
  73. * @param x = the x coordinate of the point to test
  74. * @param y = the x coordinate of the point to test
  75. * @return true if point is inside the boundary box. A point on a edge is seen as inside
  76. */
  77. bool Contains( int x, int y ) const { return Contains( wxPoint( x, y ) ); }
  78. /**
  79. * Function Contains
  80. * @param aRect = the EDA_RECT to test
  81. * @return true if aRect is Contained. A common edge is seen as contained
  82. */
  83. bool Contains( const EDA_RECT& aRect ) const;
  84. const wxSize& GetSize() const { return m_Size; }
  85. /**
  86. * @brief GetSizeMax
  87. * @return the max size dimension
  88. */
  89. int GetSizeMax() const { return ( m_Size.x > m_Size.y )?m_Size.x:m_Size.y; }
  90. int GetX() const { return m_Pos.x; }
  91. int GetY() const { return m_Pos.y; }
  92. const wxPoint& GetOrigin() const { return m_Pos; }
  93. const wxPoint& GetPosition() const { return m_Pos; }
  94. const wxPoint GetEnd() const { return wxPoint( m_Pos.x + m_Size.x, m_Pos.y + m_Size.y ); }
  95. int GetWidth() const { return m_Size.x; }
  96. int GetHeight() const { return m_Size.y; }
  97. int GetRight() const { return m_Pos.x + m_Size.x; }
  98. int GetLeft() const { return m_Pos.x; }
  99. int GetTop() const { return m_Pos.y; }
  100. int GetBottom() const { return m_Pos.y + m_Size.y; } // Y axis from top to bottom
  101. void SetOrigin( const wxPoint& pos ) { m_Pos = pos; }
  102. void SetOrigin( int x, int y ) { m_Pos.x = x; m_Pos.y = y; }
  103. void SetSize( const wxSize& size ) { m_Size = size; }
  104. void SetSize( int w, int h ) { m_Size.x = w; m_Size.y = h; }
  105. void Offset( int dx, int dy ) { m_Pos.x += dx; m_Pos.y += dy; }
  106. void Offset( const wxPoint& offset ) { m_Pos.x += offset.x; m_Pos.y +=
  107. offset.y; }
  108. void SetX( int val ) { m_Pos.x = val; }
  109. void SetY( int val ) { m_Pos.y = val; }
  110. void SetWidth( int val ) { m_Size.x = val; }
  111. void SetHeight( int val ) { m_Size.y = val; }
  112. void SetEnd( int x, int y ) { SetEnd( wxPoint( x, y ) ); }
  113. void SetEnd( const wxPoint& pos )
  114. {
  115. m_Size.x = pos.x - m_Pos.x; m_Size.y = pos.y - m_Pos.y;
  116. }
  117. /**
  118. * Function RevertYAxis
  119. * Mirror the rectangle from the X axis (negate Y pos and size)
  120. */
  121. void RevertYAxis()
  122. {
  123. m_Pos.y = -m_Pos.y;
  124. m_Size.y = -m_Size.y;
  125. Normalize();
  126. }
  127. /**
  128. * Function Intersects
  129. * tests for a common area between rectangles.
  130. *
  131. * @param aRect A rectangle to test intersection with.
  132. * @return bool - true if the argument rectangle intersects this rectangle.
  133. * (i.e. if the 2 rectangles have at least a common point)
  134. */
  135. bool Intersects( const EDA_RECT& aRect ) const;
  136. /**
  137. * Function Intersects
  138. * tests for a common area between a segment and this rectangle.
  139. *
  140. * @param aPoint1 First point of the segment to test intersection with.
  141. * @param aPoint2 Second point of the segment to test intersection with.
  142. * @return bool - true if the argument segment intersects this rectangle.
  143. * (i.e. if the segment and rectangle have at least a common point)
  144. */
  145. bool Intersects( const wxPoint& aPoint1, const wxPoint& aPoint2 ) const;
  146. /**
  147. * Function operator(wxRect)
  148. * overloads the cast operator to return a wxRect
  149. * wxRect does not accept negative values for size, so ensure the
  150. * wxRect size is always >= 0
  151. */
  152. operator wxRect() const
  153. {
  154. EDA_RECT rect( m_Pos, m_Size );
  155. rect.Normalize();
  156. return wxRect( rect.m_Pos, rect.m_Size );
  157. }
  158. /**
  159. * Function Inflate
  160. * inflates the rectangle horizontally by \a dx and vertically by \a dy. If \a dx
  161. * and/or \a dy is negative the rectangle is deflated.
  162. */
  163. EDA_RECT& Inflate( wxCoord dx, wxCoord dy );
  164. /**
  165. * Function Inflate
  166. * inflates the rectangle horizontally and vertically by \a aDelta. If \a aDelta
  167. * is negative the rectangle is deflated.
  168. */
  169. EDA_RECT& Inflate( int aDelta );
  170. /**
  171. * Function Merge
  172. * modifies the position and size of the rectangle in order to contain \a aRect. It is
  173. * mainly used to calculate bounding boxes.
  174. * @param aRect The rectangle to merge with this rectangle.
  175. */
  176. void Merge( const EDA_RECT& aRect );
  177. /**
  178. * Function Merge
  179. * modifies the position and size of the rectangle in order to contain the given point.
  180. * @param aPoint The point to merge with the rectangle.
  181. */
  182. void Merge( const wxPoint& aPoint );
  183. /**
  184. * Function GetArea
  185. * returns the area of the rectangle.
  186. * @return The area of the rectangle.
  187. */
  188. double GetArea() const;
  189. /**
  190. * Function Common
  191. * returns the area that is common with another rectangle.
  192. * @param aRect is the rectangle to find the common area with.
  193. * @return The common area rect or 0-sized rectangle if there is no intersection.
  194. */
  195. EDA_RECT Common( const EDA_RECT& aRect ) const;
  196. /**
  197. * Function GetBoundingBoxRotated
  198. * @return the bounding box of this, after rotation
  199. * @param aAngle = the rotation angle in 0.1 deg.
  200. * @param aRotCenter = the rotation point.
  201. * useful to calculate bounding box of rotated items, when
  202. * rotation if not k*90 degrees
  203. */
  204. const EDA_RECT GetBoundingBoxRotated( wxPoint aRotCenter, double aAngle );
  205. };
  206. #endif // CLASS_EDA_RECT_H