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.

412 lines
17KB

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