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.

422 lines
18KB

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