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.

427 lines
18KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. #pragma once
  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), end (other.end)
  41. {
  42. }
  43. /** Creates a line based on the coordinates of its start and end points. */
  44. Line (ValueType startX, ValueType startY, ValueType endX, ValueType endY) noexcept
  45. : start (startX, startY), end (endX, endY)
  46. {
  47. }
  48. /** Creates a line from its start and end points. */
  49. Line (Point<ValueType> startPoint, Point<ValueType> endPoint) noexcept
  50. : start (startPoint), end (endPoint)
  51. {
  52. }
  53. /** Copies a line from another one. */
  54. Line& operator= (const Line& other) noexcept
  55. {
  56. start = other.start;
  57. end = other.end;
  58. return *this;
  59. }
  60. /** Destructor. */
  61. ~Line() noexcept {}
  62. //==============================================================================
  63. /** Returns the x coordinate of the line's start point. */
  64. inline ValueType getStartX() const noexcept { return start.x; }
  65. /** Returns the y coordinate of the line's start point. */
  66. inline ValueType getStartY() const noexcept { return start.y; }
  67. /** Returns the x coordinate of the line's end point. */
  68. inline ValueType getEndX() const noexcept { return end.x; }
  69. /** Returns the y coordinate of the line's end point. */
  70. inline ValueType getEndY() const noexcept { return end.y; }
  71. /** Returns the line's start point. */
  72. inline Point<ValueType> getStart() const noexcept { return start; }
  73. /** Returns the line's end point. */
  74. inline Point<ValueType> getEnd() const noexcept { return end; }
  75. /** Changes this line's start point */
  76. void setStart (ValueType newStartX, ValueType newStartY) noexcept { start.setXY (newStartX, newStartY); }
  77. /** Changes this line's end point */
  78. void setEnd (ValueType newEndX, ValueType newEndY) noexcept { end.setXY (newEndX, newEndY); }
  79. /** Changes this line's start point */
  80. void setStart (const Point<ValueType> newStart) noexcept { start = newStart; }
  81. /** Changes this line's end point */
  82. void setEnd (const Point<ValueType> newEnd) noexcept { end = newEnd; }
  83. /** Returns a line that is the same as this one, but with the start and end reversed, */
  84. Line reversed() const noexcept { return { end, start }; }
  85. /** Applies an affine transform to the line's start and end points. */
  86. void applyTransform (const AffineTransform& transform) noexcept
  87. {
  88. start.applyTransform (transform);
  89. end.applyTransform (transform);
  90. }
  91. //==============================================================================
  92. /** Returns the length of the line. */
  93. ValueType getLength() const noexcept { return start.getDistanceFrom (end); }
  94. /** Returns the length of the line. */
  95. ValueType getLengthSquared() const noexcept { return start.getDistanceSquaredFrom (end); }
  96. /** Returns true if the line's start and end x coordinates are the same. */
  97. bool isVertical() const noexcept { return start.x == end.x; }
  98. /** Returns true if the line's start and end y coordinates are the same. */
  99. bool isHorizontal() const noexcept { return start.y == end.y; }
  100. /** Returns the line's angle.
  101. This value is the number of radians clockwise from the 12 o'clock direction,
  102. where the line's start point is considered to be at the centre.
  103. */
  104. typename Point<ValueType>::FloatType getAngle() const noexcept { return start.getAngleToPoint (end); }
  105. /** Creates a line from a start point, length and angle.
  106. This angle is the number of radians clockwise from the 12 o'clock direction,
  107. where the line's start point is considered to be at the centre.
  108. */
  109. static Line fromStartAndAngle (Point<ValueType> startPoint, ValueType length, ValueType angle) noexcept
  110. {
  111. return { startPoint, startPoint.getPointOnCircumference (length, angle) };
  112. }
  113. //==============================================================================
  114. /** Casts this line to float coordinates. */
  115. Line<float> toFloat() const noexcept { return { start.toFloat(), end.toFloat() }; }
  116. /** Casts this line to double coordinates. */
  117. Line<double> toDouble() const noexcept { return { start.toDouble(), end.toDouble() }; }
  118. //==============================================================================
  119. /** Compares two lines. */
  120. bool operator== (Line other) const noexcept { return start == other.start && end == other.end; }
  121. /** Compares two lines. */
  122. bool operator!= (Line other) const noexcept { return start != other.start || end != other.end; }
  123. //==============================================================================
  124. /** Finds the intersection between two lines.
  125. @param line the line to intersect with
  126. @returns the point at which the lines intersect, even if this lies beyond the end of the lines
  127. */
  128. Point<ValueType> getIntersection (Line line) const noexcept
  129. {
  130. Point<ValueType> p;
  131. findIntersection (start, end, line.start, line.end, p);
  132. return p;
  133. }
  134. /** Finds the intersection between two lines.
  135. @param line the other line
  136. @param intersection the position of the point where the lines meet (or
  137. where they would meet if they were infinitely long)
  138. the intersection (if the lines intersect). If the lines
  139. are parallel, this will just be set to the position
  140. of one of the line's endpoints.
  141. @returns true if the line segments intersect; false if they dont. Even if they
  142. don't intersect, the intersection coordinates returned will still
  143. be valid
  144. */
  145. bool intersects (Line line, Point<ValueType>& intersection) const noexcept
  146. {
  147. return findIntersection (start, end, line.start, line.end, intersection);
  148. }
  149. /** Returns true if this line intersects another. */
  150. bool intersects (Line other) const noexcept
  151. {
  152. Point<ValueType> ignored;
  153. return findIntersection (start, end, other.start, other.end, ignored);
  154. }
  155. //==============================================================================
  156. /** Returns the location of the point which is a given distance along this line.
  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. @see getPointAlongLineProportionally
  161. */
  162. Point<ValueType> getPointAlongLine (ValueType distanceFromStart) const noexcept
  163. {
  164. return start + (end - start) * (distanceFromStart / getLength());
  165. }
  166. /** Returns a point which is a certain distance along and to the side of this line.
  167. This effectively moves a given distance along the line, then another distance
  168. perpendicularly to this, and returns the resulting position.
  169. @param distanceFromStart the distance to move along the line from its
  170. start point. This value can be negative or longer
  171. than the line itself
  172. @param perpendicularDistance how far to move sideways from the line. If you're
  173. looking along the line from its start towards its
  174. end, then a positive value here will move to the
  175. right, negative value move to the left.
  176. */
  177. Point<ValueType> getPointAlongLine (ValueType distanceFromStart,
  178. ValueType perpendicularDistance) const noexcept
  179. {
  180. auto delta = end - start;
  181. auto length = juce_hypot ((double) delta.x,
  182. (double) delta.y);
  183. if (length <= 0)
  184. return start;
  185. return { start.x + static_cast<ValueType> ((delta.x * distanceFromStart - delta.y * perpendicularDistance) / length),
  186. start.y + static_cast<ValueType> ((delta.y * distanceFromStart + delta.x * perpendicularDistance) / length) };
  187. }
  188. /** Returns the location of the point which is a given distance along this line
  189. proportional to the line's length.
  190. @param proportionOfLength the distance to move along the line from its
  191. start point, in multiples of the line's length.
  192. So a value of 0.0 will return the line's start point
  193. and a value of 1.0 will return its end point. (This value
  194. can be negative or greater than 1.0).
  195. @see getPointAlongLine
  196. */
  197. Point<ValueType> getPointAlongLineProportionally (typename Point<ValueType>::FloatType proportionOfLength) const noexcept
  198. {
  199. return start + Point<ValueType> ((end - start) * proportionOfLength);
  200. }
  201. /** Returns the smallest distance between this line segment and a given point.
  202. So if the point is close to the line, this will return the perpendicular
  203. distance from the line; if the point is a long way beyond one of the line's
  204. end-point's, it'll return the straight-line distance to the nearest end-point.
  205. pointOnLine receives the position of the point that is found.
  206. @returns the point's distance from the line
  207. @see getPositionAlongLineOfNearestPoint
  208. */
  209. ValueType getDistanceFromPoint (Point<ValueType> targetPoint,
  210. Point<ValueType>& pointOnLine) const noexcept
  211. {
  212. auto delta = end - start;
  213. auto length = delta.x * delta.x + delta.y * delta.y;
  214. if (length > 0)
  215. {
  216. auto prop = ((targetPoint.x - start.x) * delta.x
  217. + (targetPoint.y - start.y) * delta.y) / length;
  218. if (prop >= 0 && prop <= 1.0)
  219. {
  220. pointOnLine = start + delta * static_cast<ValueType> (prop);
  221. return targetPoint.getDistanceFrom (pointOnLine);
  222. }
  223. }
  224. auto fromStart = targetPoint.getDistanceFrom (start);
  225. auto fromEnd = targetPoint.getDistanceFrom (end);
  226. if (fromStart < fromEnd)
  227. {
  228. pointOnLine = start;
  229. return fromStart;
  230. }
  231. pointOnLine = end;
  232. return fromEnd;
  233. }
  234. /** Finds the point on this line which is nearest to a given point, and
  235. returns its position as a proportional position along the line.
  236. @returns a value 0 to 1.0 which is the distance along this line from the
  237. line's start to the point which is nearest to the point passed-in. To
  238. turn this number into a position, use getPointAlongLineProportionally().
  239. @see getDistanceFromPoint, getPointAlongLineProportionally
  240. */
  241. ValueType findNearestProportionalPositionTo (Point<ValueType> point) const noexcept
  242. {
  243. auto delta = end - start;
  244. auto length = delta.x * delta.x + delta.y * delta.y;
  245. return length <= 0 ? 0
  246. : jlimit (ValueType(), static_cast<ValueType> (1),
  247. static_cast<ValueType> ((((point.x - start.x) * delta.x
  248. + (point.y - start.y) * delta.y) / length)));
  249. }
  250. /** Finds the point on this line which is nearest to a given point.
  251. @see getDistanceFromPoint, findNearestProportionalPositionTo
  252. */
  253. Point<ValueType> findNearestPointTo (Point<ValueType> point) const noexcept
  254. {
  255. return getPointAlongLineProportionally (findNearestProportionalPositionTo (point));
  256. }
  257. /** Returns true if the given point lies above this line.
  258. The return value is true if the point's y coordinate is less than the y
  259. coordinate of this line at the given x (assuming the line extends infinitely
  260. in both directions).
  261. */
  262. bool isPointAbove (Point<ValueType> point) const noexcept
  263. {
  264. return start.x != end.x
  265. && point.y < ((end.y - start.y) * (point.x - start.x)) / (end.x - start.x) + start.y;
  266. }
  267. //==============================================================================
  268. /** Returns a shortened copy of this line.
  269. This will chop off part of the start of this line by a certain amount, (leaving the
  270. end-point the same), and return the new line.
  271. */
  272. Line withShortenedStart (ValueType distanceToShortenBy) const noexcept
  273. {
  274. return { getPointAlongLine (jmin (distanceToShortenBy, getLength())), end };
  275. }
  276. /** Returns a shortened copy of this line.
  277. This will chop off part of the end of this line by a certain amount, (leaving the
  278. start-point the same), and return the new line.
  279. */
  280. Line withShortenedEnd (ValueType distanceToShortenBy) const noexcept
  281. {
  282. auto length = getLength();
  283. return { start, getPointAlongLine (length - jmin (distanceToShortenBy, length)) };
  284. }
  285. private:
  286. //==============================================================================
  287. Point<ValueType> start, end;
  288. static bool isZeroToOne (ValueType v) noexcept { return v >= 0 && v <= static_cast<ValueType> (1); }
  289. static bool findIntersection (const Point<ValueType> p1, const Point<ValueType> p2,
  290. const Point<ValueType> p3, const Point<ValueType> p4,
  291. Point<ValueType>& intersection) noexcept
  292. {
  293. if (p2 == p3)
  294. {
  295. intersection = p2;
  296. return true;
  297. }
  298. auto d1 = p2 - p1;
  299. auto d2 = p4 - p3;
  300. auto divisor = d1.x * d2.y - d2.x * d1.y;
  301. if (divisor == 0)
  302. {
  303. if (! (d1.isOrigin() || d2.isOrigin()))
  304. {
  305. if (d1.y == 0 && d2.y != 0)
  306. {
  307. auto along = (p1.y - p3.y) / d2.y;
  308. intersection = p1.withX (p3.x + along * d2.x);
  309. return isZeroToOne (along);
  310. }
  311. if (d2.y == 0 && d1.y != 0)
  312. {
  313. auto along = (p3.y - p1.y) / d1.y;
  314. intersection = p3.withX (p1.x + along * d1.x);
  315. return isZeroToOne (along);
  316. }
  317. if (d1.x == 0 && d2.x != 0)
  318. {
  319. auto along = (p1.x - p3.x) / d2.x;
  320. intersection = p1.withY (p3.y + along * d2.y);
  321. return isZeroToOne (along);
  322. }
  323. if (d2.x == 0 && d1.x != 0)
  324. {
  325. auto along = (p3.x - p1.x) / d1.x;
  326. intersection = p3.withY (p1.y + along * d1.y);
  327. return isZeroToOne (along);
  328. }
  329. }
  330. intersection = (p2 + p3) / static_cast<ValueType> (2);
  331. return false;
  332. }
  333. auto along1 = ((p1.y - p3.y) * d2.x - (p1.x - p3.x) * d2.y) / divisor;
  334. intersection = p1 + d1 * along1;
  335. if (! isZeroToOne (along1))
  336. return false;
  337. auto along2 = ((p1.y - p3.y) * d1.x - (p1.x - p3.x) * d1.y) / divisor;
  338. return isZeroToOne (along2);
  339. }
  340. };