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.

207 lines
7.1 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. /**
  30. * Class EDA_RECT
  31. * handles the component boundary box.
  32. * This class is similar to wxRect, but some wxRect functions are very curious,
  33. * and are working only if dimensions are >= 0 (not always the case in KiCad)
  34. * and also KiCad needs some specific method.
  35. * so I prefer this more suitable class
  36. */
  37. class EDA_RECT
  38. {
  39. private:
  40. wxPoint m_Pos; // Rectangle Origin
  41. wxSize m_Size; // Rectangle Size
  42. public:
  43. EDA_RECT() { };
  44. EDA_RECT( const wxPoint& aPos, const wxSize& aSize ) :
  45. m_Pos( aPos ),
  46. m_Size( aSize )
  47. { }
  48. wxPoint Centre() const
  49. {
  50. return wxPoint( m_Pos.x + ( m_Size.x >> 1 ),
  51. m_Pos.y + ( m_Size.y >> 1 ) );
  52. }
  53. /**
  54. * Function Move
  55. * moves the rectangle by the \a aMoveVector.
  56. * @param aMoveVector A wxPoint that is the value to move this rectangle
  57. */
  58. void Move( const wxPoint& aMoveVector );
  59. /**
  60. * Function Normalize
  61. * ensures that the height ant width are positive.
  62. */
  63. void Normalize();
  64. /**
  65. * Function Contains
  66. * @param aPoint = the wxPoint to test
  67. * @return true if aPoint is inside the boundary box. A point on a edge is seen as inside
  68. */
  69. bool Contains( const wxPoint& aPoint ) const;
  70. /**
  71. * Function Contains
  72. * @param x = the x coordinate of the point to test
  73. * @param y = the x coordinate of the point to test
  74. * @return true if point is inside the boundary box. A point on a edge is seen as inside
  75. */
  76. bool Contains( int x, int y ) const { return Contains( wxPoint( x, y ) ); }
  77. /**
  78. * Function Contains
  79. * @param aRect = the EDA_RECT to test
  80. * @return true if aRect is Contained. A common edge is seen as contained
  81. */
  82. bool Contains( const EDA_RECT& aRect ) const;
  83. const wxSize& GetSize() const { return m_Size; }
  84. int GetX() const { return m_Pos.x; }
  85. int GetY() const { return m_Pos.y; }
  86. const wxPoint& GetOrigin() const { return m_Pos; }
  87. const wxPoint& GetPosition() const { return m_Pos; }
  88. const wxPoint GetEnd() const { return wxPoint( m_Pos.x + m_Size.x, m_Pos.y + m_Size.y ); }
  89. int GetWidth() const { return m_Size.x; }
  90. int GetHeight() const { return m_Size.y; }
  91. int GetRight() const { return m_Pos.x + m_Size.x; }
  92. int GetLeft() const { return m_Pos.x; }
  93. int GetBottom() const { return m_Pos.y + m_Size.y; } // Y axis from top to bottom
  94. void SetOrigin( const wxPoint& pos ) { m_Pos = pos; }
  95. void SetOrigin( int x, int y ) { m_Pos.x = x; m_Pos.y = y; }
  96. void SetSize( const wxSize& size ) { m_Size = size; }
  97. void SetSize( int w, int h ) { m_Size.x = w; m_Size.y = h; }
  98. void Offset( int dx, int dy ) { m_Pos.x += dx; m_Pos.y += dy; }
  99. void Offset( const wxPoint& offset ) { m_Pos.x += offset.x; m_Pos.y +=
  100. offset.y; }
  101. void SetX( int val ) { m_Pos.x = val; }
  102. void SetY( int val ) { m_Pos.y = val; }
  103. void SetWidth( int val ) { m_Size.x = val; }
  104. void SetHeight( int val ) { m_Size.y = val; }
  105. void SetEnd( int x, int y ) { SetEnd( wxPoint( x, y ) ); }
  106. void SetEnd( const wxPoint& pos )
  107. {
  108. m_Size.x = pos.x - m_Pos.x; m_Size.y = pos.y - m_Pos.y;
  109. }
  110. /**
  111. * Function Intersects
  112. * tests for a common area between rectangles.
  113. *
  114. * @param aRect A rectangle to test intersection with.
  115. * @return bool - true if the argument rectangle intersects this rectangle.
  116. * (i.e. if the 2 rectangles have at least a common point)
  117. */
  118. bool Intersects( const EDA_RECT& aRect ) const;
  119. /**
  120. * Function Intersects
  121. * tests for a common area between a segment and this rectangle.
  122. *
  123. * @param aPoint1 First point of the segment to test intersection with.
  124. * @param aPoint2 Second point of the segment to test intersection with.
  125. * @return bool - true if the argument segment intersects this rectangle.
  126. * (i.e. if the segment and rectangle have at least a common point)
  127. */
  128. bool Intersects( const wxPoint& aPoint1, const wxPoint& aPoint2 ) const;
  129. /**
  130. * Function operator(wxRect)
  131. * overloads the cast operator to return a wxRect
  132. * wxRect does not accept negative values for size, so ensure the
  133. * wxRect size is always >= 0
  134. */
  135. operator wxRect() const
  136. {
  137. EDA_RECT rect( m_Pos, m_Size );
  138. rect.Normalize();
  139. return wxRect( rect.m_Pos, rect.m_Size );
  140. }
  141. /**
  142. * Function Inflate
  143. * inflates the rectangle horizontally by \a dx and vertically by \a dy. If \a dx
  144. * and/or \a dy is negative the rectangle is deflated.
  145. */
  146. EDA_RECT& Inflate( wxCoord dx, wxCoord dy );
  147. /**
  148. * Function Inflate
  149. * inflates the rectangle horizontally and vertically by \a aDelta. If \a aDelta
  150. * is negative the rectangle is deflated.
  151. */
  152. EDA_RECT& Inflate( int aDelta );
  153. /**
  154. * Function Merge
  155. * modifies the position and size of the rectangle in order to contain \a aRect. It is
  156. * mainly used to calculate bounding boxes.
  157. * @param aRect The rectangle to merge with this rectangle.
  158. */
  159. void Merge( const EDA_RECT& aRect );
  160. /**
  161. * Function Merge
  162. * modifies the position and size of the rectangle in order to contain the given point.
  163. * @param aPoint The point to merge with the rectangle.
  164. */
  165. void Merge( const wxPoint& aPoint );
  166. /**
  167. * Function GetArea
  168. * returns the area of the rectangle.
  169. * @return The area of the rectangle.
  170. */
  171. double GetArea() const;
  172. /**
  173. * Function GetBoundingBoxRotated
  174. * @return the bounding box of this, after rotation
  175. * @param aAngle = the rotation angle in 0.1 deg.
  176. * @param aRotCenter = the rotation point.
  177. * useful to calculate bounding box of rotated items, when
  178. * rotation if not k*90 degrees
  179. */
  180. const EDA_RECT GetBoundingBoxRotated( wxPoint aRotCenter, double aAngle );
  181. };
  182. #endif // CLASS_EDA_RECT_H