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.

229 lines
7.6 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. int GetX() const { return m_Pos.x; }
  86. int GetY() const { return m_Pos.y; }
  87. const wxPoint& GetOrigin() const { return m_Pos; }
  88. const wxPoint& GetPosition() const { return m_Pos; }
  89. const wxPoint GetEnd() const { return wxPoint( m_Pos.x + m_Size.x, m_Pos.y + m_Size.y ); }
  90. int GetWidth() const { return m_Size.x; }
  91. int GetHeight() const { return m_Size.y; }
  92. int GetRight() const { return m_Pos.x + m_Size.x; }
  93. int GetLeft() const { return m_Pos.x; }
  94. int GetTop() const { return m_Pos.y; }
  95. int GetBottom() const { return m_Pos.y + m_Size.y; } // Y axis from top to bottom
  96. void SetOrigin( const wxPoint& pos ) { m_Pos = pos; }
  97. void SetOrigin( int x, int y ) { m_Pos.x = x; m_Pos.y = y; }
  98. void SetSize( const wxSize& size ) { m_Size = size; }
  99. void SetSize( int w, int h ) { m_Size.x = w; m_Size.y = h; }
  100. void Offset( int dx, int dy ) { m_Pos.x += dx; m_Pos.y += dy; }
  101. void Offset( const wxPoint& offset ) { m_Pos.x += offset.x; m_Pos.y +=
  102. offset.y; }
  103. void SetX( int val ) { m_Pos.x = val; }
  104. void SetY( int val ) { m_Pos.y = val; }
  105. void SetWidth( int val ) { m_Size.x = val; }
  106. void SetHeight( int val ) { m_Size.y = val; }
  107. void SetEnd( int x, int y ) { SetEnd( wxPoint( x, y ) ); }
  108. void SetEnd( const wxPoint& pos )
  109. {
  110. m_Size.x = pos.x - m_Pos.x; m_Size.y = pos.y - m_Pos.y;
  111. }
  112. /**
  113. * Function RevertYAxis
  114. * Mirror the rectangle from the X axis (negate Y pos and size)
  115. */
  116. void RevertYAxis()
  117. {
  118. m_Pos.y = -m_Pos.y;
  119. m_Size.y = -m_Size.y;
  120. Normalize();
  121. }
  122. /**
  123. * Function Intersects
  124. * tests for a common area between rectangles.
  125. *
  126. * @param aRect A rectangle to test intersection with.
  127. * @return bool - true if the argument rectangle intersects this rectangle.
  128. * (i.e. if the 2 rectangles have at least a common point)
  129. */
  130. bool Intersects( const EDA_RECT& aRect ) const;
  131. /**
  132. * Function Intersects
  133. * tests for a common area between a segment and this rectangle.
  134. *
  135. * @param aPoint1 First point of the segment to test intersection with.
  136. * @param aPoint2 Second point of the segment to test intersection with.
  137. * @return bool - true if the argument segment intersects this rectangle.
  138. * (i.e. if the segment and rectangle have at least a common point)
  139. */
  140. bool Intersects( const wxPoint& aPoint1, const wxPoint& aPoint2 ) const;
  141. /**
  142. * Function operator(wxRect)
  143. * overloads the cast operator to return a wxRect
  144. * wxRect does not accept negative values for size, so ensure the
  145. * wxRect size is always >= 0
  146. */
  147. operator wxRect() const
  148. {
  149. EDA_RECT rect( m_Pos, m_Size );
  150. rect.Normalize();
  151. return wxRect( rect.m_Pos, rect.m_Size );
  152. }
  153. /**
  154. * Function Inflate
  155. * inflates the rectangle horizontally by \a dx and vertically by \a dy. If \a dx
  156. * and/or \a dy is negative the rectangle is deflated.
  157. */
  158. EDA_RECT& Inflate( wxCoord dx, wxCoord dy );
  159. /**
  160. * Function Inflate
  161. * inflates the rectangle horizontally and vertically by \a aDelta. If \a aDelta
  162. * is negative the rectangle is deflated.
  163. */
  164. EDA_RECT& Inflate( int aDelta );
  165. /**
  166. * Function Merge
  167. * modifies the position and size of the rectangle in order to contain \a aRect. It is
  168. * mainly used to calculate bounding boxes.
  169. * @param aRect The rectangle to merge with this rectangle.
  170. */
  171. void Merge( const EDA_RECT& aRect );
  172. /**
  173. * Function Merge
  174. * modifies the position and size of the rectangle in order to contain the given point.
  175. * @param aPoint The point to merge with the rectangle.
  176. */
  177. void Merge( const wxPoint& aPoint );
  178. /**
  179. * Function GetArea
  180. * returns the area of the rectangle.
  181. * @return The area of the rectangle.
  182. */
  183. double GetArea() const;
  184. /**
  185. * Function Common
  186. * returns the area that is common with another rectangle.
  187. * @param aRect is the rectangle to find the common area with.
  188. * @return The common area rect or 0-sized rectangle if there is no intersection.
  189. */
  190. EDA_RECT Common( const EDA_RECT& aRect ) const;
  191. /**
  192. * Function GetBoundingBoxRotated
  193. * @return the bounding box of this, after rotation
  194. * @param aAngle = the rotation angle in 0.1 deg.
  195. * @param aRotCenter = the rotation point.
  196. * useful to calculate bounding box of rotated items, when
  197. * rotation if not k*90 degrees
  198. */
  199. const EDA_RECT GetBoundingBoxRotated( wxPoint aRotCenter, double aAngle );
  200. };
  201. #endif // CLASS_EDA_RECT_H