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.

266 lines
11KB

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