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.

415 lines
17KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. //==============================================================================
  16. /**
  17. Represents a line.
  18. This class contains a bunch of useful methods for various geometric
  19. tasks.
  20. The ValueType template parameter should be a primitive type - float or double
  21. are what it's designed for. Integer types will work in a basic way, but some methods
  22. that perform mathematical operations may not compile, or they may not produce
  23. sensible results.
  24. @see Point, Rectangle, Path, Graphics::drawLine
  25. @tags{Graphics}
  26. */
  27. template <typename ValueType>
  28. class Line
  29. {
  30. public:
  31. //==============================================================================
  32. /** Creates a line, using (0, 0) as its start and end points. */
  33. Line() = default;
  34. /** Creates a copy of another line. */
  35. Line (const Line&) = default;
  36. /** Creates a line based on the coordinates of its start and end points. */
  37. Line (ValueType startX, ValueType startY, ValueType endX, ValueType endY) noexcept
  38. : start (startX, startY), end (endX, endY)
  39. {
  40. }
  41. /** Creates a line from its start and end points. */
  42. Line (Point<ValueType> startPoint, Point<ValueType> endPoint) noexcept
  43. : start (startPoint), end (endPoint)
  44. {
  45. }
  46. /** Copies a line from another one. */
  47. Line& operator= (const Line&) = default;
  48. /** Destructor. */
  49. ~Line() = default;
  50. //==============================================================================
  51. /** Returns the x coordinate of the line's start point. */
  52. inline ValueType getStartX() const noexcept { return start.x; }
  53. /** Returns the y coordinate of the line's start point. */
  54. inline ValueType getStartY() const noexcept { return start.y; }
  55. /** Returns the x coordinate of the line's end point. */
  56. inline ValueType getEndX() const noexcept { return end.x; }
  57. /** Returns the y coordinate of the line's end point. */
  58. inline ValueType getEndY() const noexcept { return end.y; }
  59. /** Returns the line's start point. */
  60. inline Point<ValueType> getStart() const noexcept { return start; }
  61. /** Returns the line's end point. */
  62. inline Point<ValueType> getEnd() const noexcept { return end; }
  63. /** Changes this line's start point */
  64. void setStart (ValueType newStartX, ValueType newStartY) noexcept { start.setXY (newStartX, newStartY); }
  65. /** Changes this line's end point */
  66. void setEnd (ValueType newEndX, ValueType newEndY) noexcept { end.setXY (newEndX, newEndY); }
  67. /** Changes this line's start point */
  68. void setStart (const Point<ValueType> newStart) noexcept { start = newStart; }
  69. /** Changes this line's end point */
  70. void setEnd (const Point<ValueType> newEnd) noexcept { end = newEnd; }
  71. /** Returns a line that is the same as this one, but with the start and end reversed, */
  72. Line reversed() const noexcept { return { end, start }; }
  73. /** Applies an affine transform to the line's start and end points. */
  74. void applyTransform (const AffineTransform& transform) noexcept
  75. {
  76. start.applyTransform (transform);
  77. end.applyTransform (transform);
  78. }
  79. //==============================================================================
  80. /** Returns the length of the line. */
  81. ValueType getLength() const noexcept { return start.getDistanceFrom (end); }
  82. /** Returns the length of the line. */
  83. ValueType getLengthSquared() const noexcept { return start.getDistanceSquaredFrom (end); }
  84. /** Returns true if the line's start and end x coordinates are the same. */
  85. bool isVertical() const noexcept { return start.x == end.x; }
  86. /** Returns true if the line's start and end y coordinates are the same. */
  87. bool isHorizontal() const noexcept { return start.y == end.y; }
  88. /** Returns the line's angle.
  89. This value is the number of radians clockwise from the 12 o'clock direction,
  90. where the line's start point is considered to be at the centre.
  91. */
  92. typename Point<ValueType>::FloatType getAngle() const noexcept { return start.getAngleToPoint (end); }
  93. /** Creates a line from a start point, length and angle.
  94. This angle 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. static Line fromStartAndAngle (Point<ValueType> startPoint, ValueType length, ValueType angle) noexcept
  98. {
  99. return { startPoint, startPoint.getPointOnCircumference (length, angle) };
  100. }
  101. //==============================================================================
  102. /** Casts this line to float coordinates. */
  103. Line<float> toFloat() const noexcept { return { start.toFloat(), end.toFloat() }; }
  104. /** Casts this line to double coordinates. */
  105. Line<double> toDouble() const noexcept { return { start.toDouble(), end.toDouble() }; }
  106. //==============================================================================
  107. /** Compares two lines. */
  108. bool operator== (Line other) const noexcept { return start == other.start && end == other.end; }
  109. /** Compares two lines. */
  110. bool operator!= (Line other) const noexcept { return start != other.start || end != other.end; }
  111. //==============================================================================
  112. /** Finds the intersection between two lines.
  113. @param line the line to intersect with
  114. @returns the point at which the lines intersect, even if this lies beyond the end of the lines
  115. */
  116. Point<ValueType> getIntersection (Line line) const noexcept
  117. {
  118. Point<ValueType> p;
  119. findIntersection (start, end, line.start, line.end, p);
  120. return p;
  121. }
  122. /** Finds the intersection between two lines.
  123. @param line the other line
  124. @param intersection the position of the point where the lines meet (or
  125. where they would meet if they were infinitely long)
  126. the intersection (if the lines intersect). If the lines
  127. are parallel, this will just be set to the position
  128. of one of the line's endpoints.
  129. @returns true if the line segments intersect; false if they don't. Even if they
  130. don't intersect, the intersection coordinates returned will still
  131. be valid
  132. */
  133. bool intersects (Line line, Point<ValueType>& intersection) const noexcept
  134. {
  135. return findIntersection (start, end, line.start, line.end, intersection);
  136. }
  137. /** Returns true if this line intersects another. */
  138. bool intersects (Line other) const noexcept
  139. {
  140. Point<ValueType> ignored;
  141. return findIntersection (start, end, other.start, other.end, ignored);
  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. auto delta = end - start;
  169. auto length = juce_hypot ((double) delta.x,
  170. (double) delta.y);
  171. if (length <= 0)
  172. return start;
  173. return { 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 (typename Point<ValueType>::FloatType 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 (Point<ValueType> targetPoint,
  198. Point<ValueType>& pointOnLine) const noexcept
  199. {
  200. auto delta = end - start;
  201. auto length = delta.x * delta.x + delta.y * delta.y;
  202. if (length > 0)
  203. {
  204. auto prop = ((targetPoint.x - start.x) * delta.x
  205. + (targetPoint.y - start.y) * delta.y) / (double) length;
  206. if (prop >= 0 && prop <= 1.0)
  207. {
  208. pointOnLine = start + delta * prop;
  209. return targetPoint.getDistanceFrom (pointOnLine);
  210. }
  211. }
  212. auto fromStart = targetPoint.getDistanceFrom (start);
  213. auto fromEnd = targetPoint.getDistanceFrom (end);
  214. if (fromStart < fromEnd)
  215. {
  216. pointOnLine = start;
  217. return fromStart;
  218. }
  219. pointOnLine = end;
  220. return fromEnd;
  221. }
  222. /** Finds the point on this line which is nearest to a given point, and
  223. returns its position as a proportional position along the line.
  224. @returns a value 0 to 1.0 which is the distance along this line from the
  225. line's start to the point which is nearest to the point passed-in. To
  226. turn this number into a position, use getPointAlongLineProportionally().
  227. @see getDistanceFromPoint, getPointAlongLineProportionally
  228. */
  229. ValueType findNearestProportionalPositionTo (Point<ValueType> point) const noexcept
  230. {
  231. auto delta = end - start;
  232. auto length = delta.x * delta.x + delta.y * delta.y;
  233. return length <= 0 ? 0
  234. : jlimit (ValueType(), static_cast<ValueType> (1),
  235. static_cast<ValueType> ((((point.x - start.x) * delta.x
  236. + (point.y - start.y) * delta.y) / length)));
  237. }
  238. /** Finds the point on this line which is nearest to a given point.
  239. @see getDistanceFromPoint, findNearestProportionalPositionTo
  240. */
  241. Point<ValueType> findNearestPointTo (Point<ValueType> point) const noexcept
  242. {
  243. return getPointAlongLineProportionally (findNearestProportionalPositionTo (point));
  244. }
  245. /** Returns true if the given point lies above this line.
  246. The return value is true if the point's y coordinate is less than the y
  247. coordinate of this line at the given x (assuming the line extends infinitely
  248. in both directions).
  249. */
  250. bool isPointAbove (Point<ValueType> point) const noexcept
  251. {
  252. return start.x != end.x
  253. && point.y < ((end.y - start.y) * (point.x - start.x)) / (end.x - start.x) + start.y;
  254. }
  255. //==============================================================================
  256. /** Returns a shortened copy of this line.
  257. This will chop off part of the start of this line by a certain amount, (leaving the
  258. end-point the same), and return the new line.
  259. */
  260. Line withShortenedStart (ValueType distanceToShortenBy) const noexcept
  261. {
  262. return { getPointAlongLine (jmin (distanceToShortenBy, getLength())), end };
  263. }
  264. /** Returns a shortened copy of this line.
  265. This will chop off part of the end of this line by a certain amount, (leaving the
  266. start-point the same), and return the new line.
  267. */
  268. Line withShortenedEnd (ValueType distanceToShortenBy) const noexcept
  269. {
  270. auto length = getLength();
  271. return { start, getPointAlongLine (length - jmin (distanceToShortenBy, length)) };
  272. }
  273. private:
  274. //==============================================================================
  275. Point<ValueType> start, end;
  276. static bool isZeroToOne (ValueType v) noexcept { return v >= 0 && v <= static_cast<ValueType> (1); }
  277. static bool findIntersection (const Point<ValueType> p1, const Point<ValueType> p2,
  278. const Point<ValueType> p3, const Point<ValueType> p4,
  279. Point<ValueType>& intersection) noexcept
  280. {
  281. if (p2 == p3)
  282. {
  283. intersection = p2;
  284. return true;
  285. }
  286. auto d1 = p2 - p1;
  287. auto d2 = p4 - p3;
  288. auto divisor = d1.x * d2.y - d2.x * d1.y;
  289. if (divisor == 0)
  290. {
  291. if (! (d1.isOrigin() || d2.isOrigin()))
  292. {
  293. if (d1.y == 0 && d2.y != 0)
  294. {
  295. auto along = (p1.y - p3.y) / d2.y;
  296. intersection = p1.withX (p3.x + along * d2.x);
  297. return isZeroToOne (along);
  298. }
  299. if (d2.y == 0 && d1.y != 0)
  300. {
  301. auto along = (p3.y - p1.y) / d1.y;
  302. intersection = p3.withX (p1.x + along * d1.x);
  303. return isZeroToOne (along);
  304. }
  305. if (d1.x == 0 && d2.x != 0)
  306. {
  307. auto along = (p1.x - p3.x) / d2.x;
  308. intersection = p1.withY (p3.y + along * d2.y);
  309. return isZeroToOne (along);
  310. }
  311. if (d2.x == 0 && d1.x != 0)
  312. {
  313. auto along = (p3.x - p1.x) / d1.x;
  314. intersection = p3.withY (p1.y + along * d1.y);
  315. return isZeroToOne (along);
  316. }
  317. }
  318. intersection = (p2 + p3) / static_cast<ValueType> (2);
  319. return false;
  320. }
  321. auto along1 = ((p1.y - p3.y) * d2.x - (p1.x - p3.x) * d2.y) / divisor;
  322. intersection = p1 + d1 * along1;
  323. if (! isZeroToOne (along1))
  324. return false;
  325. auto along2 = ((p1.y - p3.y) * d1.x - (p1.x - p3.x) * d1.y) / divisor;
  326. return isZeroToOne (along2);
  327. }
  328. };
  329. } // namespace juce