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.

413 lines
18KB

  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. /** Casts this line to float coordinates. */
  109. Line<float> toFloat() const noexcept { return Line<float> (start.toFloat(), end.toFloat()); }
  110. /** Casts this line to double coordinates. */
  111. Line<double> toDouble() const noexcept { return Line<double> (start.toDouble(), end.toDouble()); }
  112. //==============================================================================
  113. /** Compares two lines. */
  114. bool operator== (const Line& other) const noexcept { return start == other.start && end == other.end; }
  115. /** Compares two lines. */
  116. bool operator!= (const Line& other) const noexcept { return start != other.start || end != other.end; }
  117. //==============================================================================
  118. /** Finds the intersection between two lines.
  119. @param line the other line
  120. @param intersection the position of the point where the lines meet (or
  121. where they would meet if they were infinitely long)
  122. the intersection (if the lines intersect). If the lines
  123. are parallel, this will just be set to the position
  124. of one of the line's endpoints.
  125. @returns true if the line segments intersect; false if they dont. Even if they
  126. don't intersect, the intersection co-ordinates returned will still
  127. be valid
  128. */
  129. bool intersects (const Line& line, Point<ValueType>& intersection) const noexcept
  130. {
  131. return findIntersection (start, end, line.start, line.end, intersection);
  132. }
  133. /** Finds the intersection between two lines.
  134. @param line the line to intersect with
  135. @returns the point at which the lines intersect, even if this lies beyond the end of the lines
  136. */
  137. Point<ValueType> getIntersection (const Line& line) const noexcept
  138. {
  139. Point<ValueType> p;
  140. findIntersection (start, end, line.start, line.end, p);
  141. return p;
  142. }
  143. //==============================================================================
  144. /** Returns the location of the point which is a given distance along this line.
  145. @param distanceFromStart the distance to move along the line from its
  146. start point. This value can be negative or longer
  147. than the line itself
  148. @see getPointAlongLineProportionally
  149. */
  150. Point<ValueType> getPointAlongLine (ValueType distanceFromStart) const noexcept
  151. {
  152. return start + (end - start) * (distanceFromStart / getLength());
  153. }
  154. /** Returns a point which is a certain distance along and to the side of this line.
  155. This effectively moves a given distance along the line, then another distance
  156. perpendicularly to this, and returns the resulting position.
  157. @param distanceFromStart the distance to move along the line from its
  158. start point. This value can be negative or longer
  159. than the line itself
  160. @param perpendicularDistance how far to move sideways from the line. If you're
  161. looking along the line from its start towards its
  162. end, then a positive value here will move to the
  163. right, negative value move to the left.
  164. */
  165. Point<ValueType> getPointAlongLine (ValueType distanceFromStart,
  166. ValueType perpendicularDistance) const noexcept
  167. {
  168. const Point<ValueType> delta (end - start);
  169. const double length = juce_hypot ((double) delta.x,
  170. (double) delta.y);
  171. if (length <= 0)
  172. return start;
  173. return Point<ValueType> (start.x + static_cast <ValueType> ((delta.x * distanceFromStart - delta.y * perpendicularDistance) / length),
  174. start.y + static_cast <ValueType> ((delta.y * distanceFromStart + delta.x * perpendicularDistance) / length));
  175. }
  176. /** Returns the location of the point which is a given distance along this line
  177. proportional to the line's length.
  178. @param proportionOfLength the distance to move along the line from its
  179. start point, in multiples of the line's length.
  180. So a value of 0.0 will return the line's start point
  181. and a value of 1.0 will return its end point. (This value
  182. can be negative or greater than 1.0).
  183. @see getPointAlongLine
  184. */
  185. Point<ValueType> getPointAlongLineProportionally (ValueType proportionOfLength) const noexcept
  186. {
  187. return start + (end - start) * proportionOfLength;
  188. }
  189. /** Returns the smallest distance between this line segment and a given point.
  190. So if the point is close to the line, this will return the perpendicular
  191. distance from the line; if the point is a long way beyond one of the line's
  192. end-point's, it'll return the straight-line distance to the nearest end-point.
  193. pointOnLine receives the position of the point that is found.
  194. @returns the point's distance from the line
  195. @see getPositionAlongLineOfNearestPoint
  196. */
  197. ValueType getDistanceFromPoint (const Point<ValueType>& targetPoint,
  198. Point<ValueType>& pointOnLine) const noexcept
  199. {
  200. const Point<ValueType> delta (end - start);
  201. const double length = delta.x * delta.x + delta.y * delta.y;
  202. if (length > 0)
  203. {
  204. const double prop = ((targetPoint.x - start.x) * delta.x
  205. + (targetPoint.y - start.y) * delta.y) / length;
  206. if (prop >= 0 && prop <= 1.0)
  207. {
  208. pointOnLine = start + delta * static_cast <ValueType> (prop);
  209. return targetPoint.getDistanceFrom (pointOnLine);
  210. }
  211. }
  212. const float fromStart = targetPoint.getDistanceFrom (start);
  213. const float fromEnd = targetPoint.getDistanceFrom (end);
  214. if (fromStart < fromEnd)
  215. {
  216. pointOnLine = start;
  217. return fromStart;
  218. }
  219. else
  220. {
  221. pointOnLine = end;
  222. return fromEnd;
  223. }
  224. }
  225. /** Finds the point on this line which is nearest to a given point, and
  226. returns its position as a proportional position along the line.
  227. @returns a value 0 to 1.0 which is the distance along this line from the
  228. line's start to the point which is nearest to the point passed-in. To
  229. turn this number into a position, use getPointAlongLineProportionally().
  230. @see getDistanceFromPoint, getPointAlongLineProportionally
  231. */
  232. ValueType findNearestProportionalPositionTo (const Point<ValueType>& point) const noexcept
  233. {
  234. const Point<ValueType> delta (end - start);
  235. const double length = delta.x * delta.x + delta.y * delta.y;
  236. return length <= 0 ? 0
  237. : jlimit (ValueType(), static_cast <ValueType> (1),
  238. static_cast <ValueType> ((((point.x - start.x) * delta.x
  239. + (point.y - start.y) * delta.y) / length)));
  240. }
  241. /** Finds the point on this line which is nearest to a given point.
  242. @see getDistanceFromPoint, findNearestProportionalPositionTo
  243. */
  244. Point<ValueType> findNearestPointTo (const Point<ValueType>& point) const noexcept
  245. {
  246. return getPointAlongLineProportionally (findNearestProportionalPositionTo (point));
  247. }
  248. /** Returns true if the given point lies above this line.
  249. The return value is true if the point's y coordinate is less than the y
  250. coordinate of this line at the given x (assuming the line extends infinitely
  251. in both directions).
  252. */
  253. bool isPointAbove (const Point<ValueType>& point) const noexcept
  254. {
  255. return start.x != end.x
  256. && point.y < ((end.y - start.y)
  257. * (point.x - start.x)) / (end.x - start.x) + start.y;
  258. }
  259. //==============================================================================
  260. /** Returns a shortened copy of this line.
  261. This will chop off part of the start of this line by a certain amount, (leaving the
  262. end-point the same), and return the new line.
  263. */
  264. Line withShortenedStart (ValueType distanceToShortenBy) const noexcept
  265. {
  266. return Line (getPointAlongLine (jmin (distanceToShortenBy, getLength())), end);
  267. }
  268. /** Returns a shortened copy of this line.
  269. This will chop off part of the end of this line by a certain amount, (leaving the
  270. start-point the same), and return the new line.
  271. */
  272. Line withShortenedEnd (ValueType distanceToShortenBy) const noexcept
  273. {
  274. const ValueType length = getLength();
  275. return Line (start, getPointAlongLine (length - jmin (distanceToShortenBy, length)));
  276. }
  277. private:
  278. //==============================================================================
  279. Point<ValueType> start, end;
  280. static bool findIntersection (const Point<ValueType>& p1, const Point<ValueType>& p2,
  281. const Point<ValueType>& p3, const Point<ValueType>& p4,
  282. Point<ValueType>& intersection) noexcept
  283. {
  284. if (p2 == p3)
  285. {
  286. intersection = p2;
  287. return true;
  288. }
  289. const Point<ValueType> d1 (p2 - p1);
  290. const Point<ValueType> d2 (p4 - p3);
  291. const ValueType divisor = d1.x * d2.y - d2.x * d1.y;
  292. if (divisor == 0)
  293. {
  294. if (! (d1.isOrigin() || d2.isOrigin()))
  295. {
  296. if (d1.y == 0 && d2.y != 0)
  297. {
  298. const ValueType along = (p1.y - p3.y) / d2.y;
  299. intersection = p1.withX (p3.x + along * d2.x);
  300. return along >= 0 && along <= static_cast <ValueType> (1);
  301. }
  302. else if (d2.y == 0 && d1.y != 0)
  303. {
  304. const ValueType along = (p3.y - p1.y) / d1.y;
  305. intersection = p3.withX (p1.x + along * d1.x);
  306. return along >= 0 && along <= static_cast <ValueType> (1);
  307. }
  308. else if (d1.x == 0 && d2.x != 0)
  309. {
  310. const ValueType along = (p1.x - p3.x) / d2.x;
  311. intersection = p1.withY (p3.y + along * d2.y);
  312. return along >= 0 && along <= static_cast <ValueType> (1);
  313. }
  314. else if (d2.x == 0 && d1.x != 0)
  315. {
  316. const ValueType along = (p3.x - p1.x) / d1.x;
  317. intersection = p3.withY (p1.y + along * d1.y);
  318. return along >= 0 && along <= static_cast <ValueType> (1);
  319. }
  320. }
  321. intersection = (p2 + p3) / static_cast <ValueType> (2);
  322. return false;
  323. }
  324. const ValueType along1 = ((p1.y - p3.y) * d2.x - (p1.x - p3.x) * d2.y) / divisor;
  325. intersection = p1 + d1 * along1;
  326. if (along1 < 0 || along1 > static_cast <ValueType> (1))
  327. return false;
  328. const ValueType along2 = ((p1.y - p3.y) * d1.x - (p1.x - p3.x) * d1.y) / divisor;
  329. return along2 >= 0 && along2 <= static_cast <ValueType> (1);
  330. }
  331. };
  332. #endif // __JUCE_LINE_JUCEHEADER__