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.

407 lines
17KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 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_Point.h"
  21. //==============================================================================
  22. /**
  23. Represents a line.
  24. This class contains a bunch of useful methods for various geometric
  25. tasks.
  26. The ValueType template parameter should be a primitive type - float or double
  27. are what it's designed for. Integer types will work in a basic way, but some methods
  28. that perform mathematical operations may not compile, or they may not produce
  29. sensible results.
  30. @see Point, Rectangle, Path, Graphics::drawLine
  31. */
  32. template <typename ValueType>
  33. class Line
  34. {
  35. public:
  36. //==============================================================================
  37. /** Creates a line, using (0, 0) as its start and end points. */
  38. Line() noexcept {}
  39. /** Creates a copy of another line. */
  40. Line (const Line& other) noexcept
  41. : start (other.start),
  42. end (other.end)
  43. {
  44. }
  45. /** Creates a line based on the co-ordinates of its start and end points. */
  46. Line (ValueType startX, ValueType startY, ValueType endX, ValueType endY) noexcept
  47. : start (startX, startY),
  48. end (endX, endY)
  49. {
  50. }
  51. /** Creates a line from its start and end points. */
  52. Line (const Point<ValueType>& startPoint,
  53. const Point<ValueType>& endPoint) noexcept
  54. : start (startPoint),
  55. end (endPoint)
  56. {
  57. }
  58. /** Copies a line from another one. */
  59. Line& operator= (const Line& other) noexcept
  60. {
  61. start = other.start;
  62. end = other.end;
  63. return *this;
  64. }
  65. /** Destructor. */
  66. ~Line() noexcept {}
  67. //==============================================================================
  68. /** Returns the x co-ordinate of the line's start point. */
  69. inline ValueType getStartX() const noexcept { return start.x; }
  70. /** Returns the y co-ordinate of the line's start point. */
  71. inline ValueType getStartY() const noexcept { return start.y; }
  72. /** Returns the x co-ordinate of the line's end point. */
  73. inline ValueType getEndX() const noexcept { return end.x; }
  74. /** Returns the y co-ordinate of the line's end point. */
  75. inline ValueType getEndY() const noexcept { return end.y; }
  76. /** Returns the line's start point. */
  77. inline const Point<ValueType>& getStart() const noexcept { return start; }
  78. /** Returns the line's end point. */
  79. inline const Point<ValueType>& getEnd() const noexcept { return end; }
  80. /** Changes this line's start point */
  81. void setStart (ValueType newStartX, ValueType newStartY) noexcept { start.setXY (newStartX, newStartY); }
  82. /** Changes this line's end point */
  83. void setEnd (ValueType newEndX, ValueType newEndY) noexcept { end.setXY (newEndX, newEndY); }
  84. /** Changes this line's start point */
  85. void setStart (const Point<ValueType>& newStart) noexcept { start = newStart; }
  86. /** Changes this line's end point */
  87. void setEnd (const Point<ValueType>& newEnd) noexcept { end = newEnd; }
  88. /** Returns a line that is the same as this one, but with the start and end reversed, */
  89. const Line reversed() const noexcept { return Line (end, start); }
  90. /** Applies an affine transform to the line's start and end points. */
  91. void applyTransform (const AffineTransform& transform) noexcept
  92. {
  93. start.applyTransform (transform);
  94. end.applyTransform (transform);
  95. }
  96. //==============================================================================
  97. /** Returns the length of the line. */
  98. ValueType getLength() const noexcept { return start.getDistanceFrom (end); }
  99. /** Returns true if the line's start and end x co-ordinates are the same. */
  100. bool isVertical() const noexcept { return start.x == end.x; }
  101. /** Returns true if the line's start and end y co-ordinates are the same. */
  102. bool isHorizontal() const noexcept { return start.y == end.y; }
  103. /** Returns the line's angle.
  104. This value is the number of radians clockwise from the 12 o'clock direction,
  105. where the line's start point is considered to be at the centre.
  106. */
  107. typename Point<ValueType>::FloatType getAngle() const noexcept { return start.getAngleToPoint (end); }
  108. //==============================================================================
  109. /** Compares two lines. */
  110. bool operator== (const Line& other) const noexcept { return start == other.start && end == other.end; }
  111. /** Compares two lines. */
  112. bool operator!= (const Line& other) const noexcept { return start != other.start || end != other.end; }
  113. //==============================================================================
  114. /** Finds the intersection between two lines.
  115. @param line the other line
  116. @param intersection the position of the point where the lines meet (or
  117. where they would meet if they were infinitely long)
  118. the intersection (if the lines intersect). If the lines
  119. are parallel, this will just be set to the position
  120. of one of the line's endpoints.
  121. @returns true if the line segments intersect; false if they dont. Even if they
  122. don't intersect, the intersection co-ordinates returned will still
  123. be valid
  124. */
  125. bool intersects (const Line& line, Point<ValueType>& intersection) const noexcept
  126. {
  127. return findIntersection (start, end, line.start, line.end, intersection);
  128. }
  129. /** Finds the intersection between two lines.
  130. @param line the line to intersect with
  131. @returns the point at which the lines intersect, even if this lies beyond the end of the lines
  132. */
  133. Point<ValueType> getIntersection (const Line& line) const noexcept
  134. {
  135. Point<ValueType> p;
  136. findIntersection (start, end, line.start, line.end, p);
  137. return p;
  138. }
  139. //==============================================================================
  140. /** Returns the location of the point which is a given distance along this line.
  141. @param distanceFromStart the distance to move along the line from its
  142. start point. This value can be negative or longer
  143. than the line itself
  144. @see getPointAlongLineProportionally
  145. */
  146. Point<ValueType> getPointAlongLine (ValueType distanceFromStart) const noexcept
  147. {
  148. return start + (end - start) * (distanceFromStart / getLength());
  149. }
  150. /** Returns a point which is a certain distance along and to the side of this line.
  151. This effectively moves a given distance along the line, then another distance
  152. perpendicularly to this, and returns the resulting position.
  153. @param distanceFromStart the distance to move along the line from its
  154. start point. This value can be negative or longer
  155. than the line itself
  156. @param perpendicularDistance how far to move sideways from the line. If you're
  157. looking along the line from its start towards its
  158. end, then a positive value here will move to the
  159. right, negative value move to the left.
  160. */
  161. Point<ValueType> getPointAlongLine (ValueType distanceFromStart,
  162. ValueType perpendicularDistance) const noexcept
  163. {
  164. const Point<ValueType> delta (end - start);
  165. const double length = juce_hypot ((double) delta.x,
  166. (double) delta.y);
  167. if (length <= 0)
  168. return start;
  169. return Point<ValueType> (start.x + static_cast <ValueType> ((delta.x * distanceFromStart - delta.y * perpendicularDistance) / length),
  170. start.y + static_cast <ValueType> ((delta.y * distanceFromStart + delta.x * perpendicularDistance) / length));
  171. }
  172. /** Returns the location of the point which is a given distance along this line
  173. proportional to the line's length.
  174. @param proportionOfLength the distance to move along the line from its
  175. start point, in multiples of the line's length.
  176. So a value of 0.0 will return the line's start point
  177. and a value of 1.0 will return its end point. (This value
  178. can be negative or greater than 1.0).
  179. @see getPointAlongLine
  180. */
  181. Point<ValueType> getPointAlongLineProportionally (ValueType proportionOfLength) const noexcept
  182. {
  183. return start + (end - start) * proportionOfLength;
  184. }
  185. /** Returns the smallest distance between this line segment and a given point.
  186. So if the point is close to the line, this will return the perpendicular
  187. distance from the line; if the point is a long way beyond one of the line's
  188. end-point's, it'll return the straight-line distance to the nearest end-point.
  189. pointOnLine receives the position of the point that is found.
  190. @returns the point's distance from the line
  191. @see getPositionAlongLineOfNearestPoint
  192. */
  193. ValueType getDistanceFromPoint (const Point<ValueType>& targetPoint,
  194. Point<ValueType>& pointOnLine) const noexcept
  195. {
  196. const Point<ValueType> delta (end - start);
  197. const double length = delta.x * delta.x + delta.y * delta.y;
  198. if (length > 0)
  199. {
  200. const double prop = ((targetPoint.x - start.x) * delta.x
  201. + (targetPoint.y - start.y) * delta.y) / length;
  202. if (prop >= 0 && prop <= 1.0)
  203. {
  204. pointOnLine = start + delta * static_cast <ValueType> (prop);
  205. return targetPoint.getDistanceFrom (pointOnLine);
  206. }
  207. }
  208. const float fromStart = targetPoint.getDistanceFrom (start);
  209. const float fromEnd = targetPoint.getDistanceFrom (end);
  210. if (fromStart < fromEnd)
  211. {
  212. pointOnLine = start;
  213. return fromStart;
  214. }
  215. else
  216. {
  217. pointOnLine = end;
  218. return fromEnd;
  219. }
  220. }
  221. /** Finds the point on this line which is nearest to a given point, and
  222. returns its position as a proportional position along the line.
  223. @returns a value 0 to 1.0 which is the distance along this line from the
  224. line's start to the point which is nearest to the point passed-in. To
  225. turn this number into a position, use getPointAlongLineProportionally().
  226. @see getDistanceFromPoint, getPointAlongLineProportionally
  227. */
  228. ValueType findNearestProportionalPositionTo (const Point<ValueType>& point) const noexcept
  229. {
  230. const Point<ValueType> delta (end - start);
  231. const double length = delta.x * delta.x + delta.y * delta.y;
  232. return length <= 0 ? 0
  233. : jlimit (ValueType(), static_cast <ValueType> (1),
  234. static_cast <ValueType> ((((point.x - start.x) * delta.x
  235. + (point.y - start.y) * delta.y) / length)));
  236. }
  237. /** Finds the point on this line which is nearest to a given point.
  238. @see getDistanceFromPoint, findNearestProportionalPositionTo
  239. */
  240. Point<ValueType> findNearestPointTo (const Point<ValueType>& point) const noexcept
  241. {
  242. return getPointAlongLineProportionally (findNearestProportionalPositionTo (point));
  243. }
  244. /** Returns true if the given point lies above this line.
  245. The return value is true if the point's y coordinate is less than the y
  246. coordinate of this line at the given x (assuming the line extends infinitely
  247. in both directions).
  248. */
  249. bool isPointAbove (const Point<ValueType>& point) const noexcept
  250. {
  251. return start.x != end.x
  252. && point.y < ((end.y - start.y)
  253. * (point.x - start.x)) / (end.x - start.x) + start.y;
  254. }
  255. //==============================================================================
  256. /** Returns a shortened copy of this line.
  257. This will chop off part of the start of this line by a certain amount, (leaving the
  258. end-point the same), and return the new line.
  259. */
  260. Line withShortenedStart (ValueType distanceToShortenBy) const noexcept
  261. {
  262. return Line (getPointAlongLine (jmin (distanceToShortenBy, getLength())), end);
  263. }
  264. /** Returns a shortened copy of this line.
  265. This will chop off part of the end of this line by a certain amount, (leaving the
  266. start-point the same), and return the new line.
  267. */
  268. Line withShortenedEnd (ValueType distanceToShortenBy) const noexcept
  269. {
  270. const ValueType length = getLength();
  271. return Line (start, getPointAlongLine (length - jmin (distanceToShortenBy, length)));
  272. }
  273. private:
  274. //==============================================================================
  275. Point<ValueType> start, end;
  276. static bool findIntersection (const Point<ValueType>& p1, const Point<ValueType>& p2,
  277. const Point<ValueType>& p3, const Point<ValueType>& p4,
  278. Point<ValueType>& intersection) noexcept
  279. {
  280. if (p2 == p3)
  281. {
  282. intersection = p2;
  283. return true;
  284. }
  285. const Point<ValueType> d1 (p2 - p1);
  286. const Point<ValueType> d2 (p4 - p3);
  287. const ValueType divisor = d1.x * d2.y - d2.x * d1.y;
  288. if (divisor == 0)
  289. {
  290. if (! (d1.isOrigin() || d2.isOrigin()))
  291. {
  292. if (d1.y == 0 && d2.y != 0)
  293. {
  294. const ValueType along = (p1.y - p3.y) / d2.y;
  295. intersection = p1.withX (p3.x + along * d2.x);
  296. return along >= 0 && along <= static_cast <ValueType> (1);
  297. }
  298. else if (d2.y == 0 && d1.y != 0)
  299. {
  300. const ValueType along = (p3.y - p1.y) / d1.y;
  301. intersection = p3.withX (p1.x + along * d1.x);
  302. return along >= 0 && along <= static_cast <ValueType> (1);
  303. }
  304. else if (d1.x == 0 && d2.x != 0)
  305. {
  306. const ValueType along = (p1.x - p3.x) / d2.x;
  307. intersection = p1.withY (p3.y + along * d2.y);
  308. return along >= 0 && along <= static_cast <ValueType> (1);
  309. }
  310. else if (d2.x == 0 && d1.x != 0)
  311. {
  312. const ValueType along = (p3.x - p1.x) / d1.x;
  313. intersection = p3.withY (p1.y + along * d1.y);
  314. return along >= 0 && along <= static_cast <ValueType> (1);
  315. }
  316. }
  317. intersection = (p2 + p3) / static_cast <ValueType> (2);
  318. return false;
  319. }
  320. const ValueType along1 = ((p1.y - p3.y) * d2.x - (p1.x - p3.x) * d2.y) / divisor;
  321. intersection = p1 + d1 * along1;
  322. if (along1 < 0 || along1 > static_cast <ValueType> (1))
  323. return false;
  324. const ValueType along2 = ((p1.y - p3.y) * d1.x - (p1.x - p3.x) * d1.y) / divisor;
  325. return along2 >= 0 && along2 <= static_cast <ValueType> (1);
  326. }
  327. };
  328. #endif // __JUCE_LINE_JUCEHEADER__