Audio plugin host https://kx.studio/carla
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.

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