The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

257 lines
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-9 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_LINE_JUCEHEADER__
  19. #define __JUCE_LINE_JUCEHEADER__
  20. #include "juce_Path.h"
  21. #include "juce_Point.h"
  22. //==============================================================================
  23. /**
  24. Represents a line, using 32-bit float co-ordinates.
  25. This class contains a bunch of useful methods for various geometric
  26. tasks.
  27. @see Point, Rectangle, Path, Graphics::drawLine
  28. */
  29. class JUCE_API Line
  30. {
  31. public:
  32. //==============================================================================
  33. /** Creates a line, using (0, 0) as its start and end points. */
  34. Line() throw();
  35. /** Creates a copy of another line. */
  36. Line (const Line& other) throw();
  37. /** Creates a line based on the co-ordinates of its start and end points. */
  38. Line (float startX,
  39. float startY,
  40. float endX,
  41. float endY) throw();
  42. /** Creates a line from its start and end points. */
  43. Line (const Point<float>& start,
  44. const Point<float>& end) throw();
  45. /** Copies a line from another one. */
  46. Line& operator= (const Line& other) throw();
  47. /** Destructor. */
  48. ~Line() throw();
  49. //==============================================================================
  50. /** Returns the x co-ordinate of the line's start point. */
  51. inline float getStartX() const throw() { return startX; }
  52. /** Returns the y co-ordinate of the line's start point. */
  53. inline float getStartY() const throw() { return startY; }
  54. /** Returns the x co-ordinate of the line's end point. */
  55. inline float getEndX() const throw() { return endX; }
  56. /** Returns the y co-ordinate of the line's end point. */
  57. inline float getEndY() const throw() { return endY; }
  58. /** Returns the line's start point. */
  59. const Point<float> getStart() const throw();
  60. /** Returns the line's end point. */
  61. const Point<float> getEnd() const throw();
  62. /** Changes this line's start point */
  63. void setStart (float newStartX,
  64. float newStartY) throw();
  65. /** Changes this line's end point */
  66. void setEnd (float newEndX,
  67. float newEndY) throw();
  68. /** Changes this line's start point */
  69. void setStart (const Point<float>& newStart) throw();
  70. /** Changes this line's end point */
  71. void setEnd (const Point<float>& newEnd) throw();
  72. /** Applies an affine transform to the line's start and end points. */
  73. void applyTransform (const AffineTransform& transform) throw();
  74. //==============================================================================
  75. /** Returns the length of the line. */
  76. float getLength() const throw();
  77. /** Returns true if the line's start and end x co-ordinates are the same. */
  78. bool isVertical() const throw();
  79. /** Returns true if the line's start and end y co-ordinates are the same. */
  80. bool isHorizontal() const throw();
  81. /** Returns the line's angle.
  82. This value is the number of radians clockwise from the 3 o'clock direction,
  83. where the line's start point is considered to be at the centre.
  84. */
  85. float getAngle() const throw();
  86. //==============================================================================
  87. /** Compares two lines. */
  88. bool operator== (const Line& other) const throw();
  89. /** Compares two lines. */
  90. bool operator!= (const Line& other) const throw();
  91. //==============================================================================
  92. /** Finds the intersection between two lines.
  93. @param line the other line
  94. @param intersectionX the x co-ordinate of the point where the lines meet (or
  95. where they would meet if they were infinitely long)
  96. the intersection (if the lines intersect). If the lines
  97. are parallel, this will just be set to the position
  98. of one of the line's endpoints.
  99. @param intersectionY the y co-ordinate of the point where the lines meet
  100. @returns true if the line segments intersect; false if they dont. Even if they
  101. don't intersect, the intersection co-ordinates returned will still
  102. be valid
  103. */
  104. bool intersects (const Line& line,
  105. float& intersectionX,
  106. float& intersectionY) const throw();
  107. //==============================================================================
  108. /** Returns the location of the point which is a given distance along this line.
  109. @param distanceFromStart the distance to move along the line from its
  110. start point. This value can be negative or longer
  111. than the line itself
  112. @see getPointAlongLineProportionally
  113. */
  114. const Point<float> getPointAlongLine (float distanceFromStart) const throw();
  115. /** Returns a point which is a certain distance along and to the side of this line.
  116. This effectively moves a given distance along the line, then another distance
  117. perpendicularly to this, and returns the resulting position.
  118. @param distanceFromStart the distance to move along the line from its
  119. start point. This value can be negative or longer
  120. than the line itself
  121. @param perpendicularDistance how far to move sideways from the line. If you're
  122. looking along the line from its start towards its
  123. end, then a positive value here will move to the
  124. right, negative value move to the left.
  125. */
  126. const Point<float> getPointAlongLine (float distanceFromStart,
  127. float perpendicularDistance) const throw();
  128. /** Returns the location of the point which is a given distance along this line
  129. proportional to the line's length.
  130. @param proportionOfLength the distance to move along the line from its
  131. start point, in multiples of the line's length.
  132. So a value of 0.0 will return the line's start point
  133. and a value of 1.0 will return its end point. (This value
  134. can be negative or greater than 1.0).
  135. @see getPointAlongLine
  136. */
  137. const Point<float> getPointAlongLineProportionally (float proportionOfLength) const throw();
  138. /** Returns the smallest distance between this line segment and a given point.
  139. So if the point is close to the line, this will return the perpendicular
  140. distance from the line; if the point is a long way beyond one of the line's
  141. end-point's, it'll return the straight-line distance to the nearest end-point.
  142. @param x x position of the point to test
  143. @param y y position of the point to test
  144. @returns the point's distance from the line
  145. @see getPositionAlongLineOfNearestPoint
  146. */
  147. float getDistanceFromLine (float x, float y) const throw();
  148. /** Finds the point on this line which is nearest to a given point, and
  149. returns its position as a proportional position along the line.
  150. @param x x position of the point to test
  151. @param y y position of the point to test
  152. @returns a value 0 to 1.0 which is the distance along this line from the
  153. line's start to the point which is nearest to the point passed-in. To
  154. turn this number into a position, use getPointAlongLineProportionally().
  155. @see getDistanceFromLine, getPointAlongLineProportionally
  156. */
  157. float findNearestPointTo (float x, float y) const throw();
  158. /** Returns true if the given point lies above this line.
  159. The return value is true if the point's y coordinate is less than the y
  160. coordinate of this line at the given x (assuming the line extends infinitely
  161. in both directions).
  162. */
  163. bool isPointAbove (float x, float y) const throw();
  164. //==============================================================================
  165. /** Returns a shortened copy of this line.
  166. This will chop off part of the start of this line by a certain amount, (leaving the
  167. end-point the same), and return the new line.
  168. */
  169. const Line withShortenedStart (float distanceToShortenBy) const throw();
  170. /** Returns a shortened copy of this line.
  171. This will chop off part of the end of this line by a certain amount, (leaving the
  172. start-point the same), and return the new line.
  173. */
  174. const Line withShortenedEnd (float distanceToShortenBy) const throw();
  175. /** Cuts off parts of this line to keep the parts that are either inside or
  176. outside a path.
  177. Note that this isn't smart enough to cope with situations where the
  178. line would need to be cut into multiple pieces to correctly clip against
  179. a re-entrant shape.
  180. @param path the path to clip against
  181. @param keepSectionOutsidePath if true, it's the section outside the path
  182. that will be kept; if false its the section inside
  183. the path
  184. @returns true if the line was changed.
  185. */
  186. bool clipToPath (const Path& path, bool keepSectionOutsidePath) throw();
  187. //==============================================================================
  188. juce_UseDebuggingNewOperator
  189. private:
  190. float startX, startY, endX, endY;
  191. };
  192. #endif // __JUCE_LINE_JUCEHEADER__