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.

juce_Line.h 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - 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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-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. const auto length = getLength();
  158. return length == 0 ? start : start + (end - start) * (distanceFromStart / length);
  159. }
  160. /** Returns a point which is a certain distance along and to the side of this line.
  161. This effectively moves a given distance along the line, then another distance
  162. perpendicularly to this, and returns the resulting position.
  163. @param distanceFromStart the distance to move along the line from its
  164. start point. This value can be negative or longer
  165. than the line itself
  166. @param perpendicularDistance how far to move sideways from the line. If you're
  167. looking along the line from its start towards its
  168. end, then a positive value here will move to the
  169. right, negative value move to the left.
  170. */
  171. Point<ValueType> getPointAlongLine (ValueType distanceFromStart,
  172. ValueType perpendicularDistance) const noexcept
  173. {
  174. auto delta = end - start;
  175. auto length = juce_hypot ((double) delta.x,
  176. (double) delta.y);
  177. if (length <= 0)
  178. return start;
  179. return { start.x + static_cast<ValueType> ((delta.x * distanceFromStart - delta.y * perpendicularDistance) / length),
  180. start.y + static_cast<ValueType> ((delta.y * distanceFromStart + delta.x * perpendicularDistance) / length) };
  181. }
  182. /** Returns the location of the point which is a given distance along this line
  183. proportional to the line's length.
  184. @param proportionOfLength the distance to move along the line from its
  185. start point, in multiples of the line's length.
  186. So a value of 0.0 will return the line's start point
  187. and a value of 1.0 will return its end point. (This value
  188. can be negative or greater than 1.0).
  189. @see getPointAlongLine
  190. */
  191. Point<ValueType> getPointAlongLineProportionally (typename Point<ValueType>::FloatType proportionOfLength) const noexcept
  192. {
  193. return start + (end - start) * proportionOfLength;
  194. }
  195. /** Returns the smallest distance between this line segment and a given point.
  196. So if the point is close to the line, this will return the perpendicular
  197. distance from the line; if the point is a long way beyond one of the line's
  198. end-point's, it'll return the straight-line distance to the nearest end-point.
  199. pointOnLine receives the position of the point that is found.
  200. @returns the point's distance from the line
  201. @see getPositionAlongLineOfNearestPoint
  202. */
  203. ValueType getDistanceFromPoint (Point<ValueType> targetPoint,
  204. Point<ValueType>& pointOnLine) const noexcept
  205. {
  206. auto delta = end - start;
  207. auto length = delta.x * delta.x + delta.y * delta.y;
  208. if (length > 0)
  209. {
  210. auto prop = ((targetPoint.x - start.x) * delta.x
  211. + (targetPoint.y - start.y) * delta.y) / (double) length;
  212. if (prop >= 0 && prop <= 1.0)
  213. {
  214. pointOnLine = start + delta * prop;
  215. return targetPoint.getDistanceFrom (pointOnLine);
  216. }
  217. }
  218. auto fromStart = targetPoint.getDistanceFrom (start);
  219. auto fromEnd = targetPoint.getDistanceFrom (end);
  220. if (fromStart < fromEnd)
  221. {
  222. pointOnLine = start;
  223. return fromStart;
  224. }
  225. pointOnLine = end;
  226. return fromEnd;
  227. }
  228. /** Finds the point on this line which is nearest to a given point, and
  229. returns its position as a proportional position along the line.
  230. @returns a value 0 to 1.0 which is the distance along this line from the
  231. line's start to the point which is nearest to the point passed-in. To
  232. turn this number into a position, use getPointAlongLineProportionally().
  233. @see getDistanceFromPoint, getPointAlongLineProportionally
  234. */
  235. ValueType findNearestProportionalPositionTo (Point<ValueType> point) const noexcept
  236. {
  237. auto delta = end - start;
  238. auto length = delta.x * delta.x + delta.y * delta.y;
  239. return length <= 0 ? 0
  240. : jlimit (ValueType(), static_cast<ValueType> (1),
  241. static_cast<ValueType> ((((point.x - start.x) * delta.x
  242. + (point.y - start.y) * delta.y) / length)));
  243. }
  244. /** Finds the point on this line which is nearest to a given point.
  245. @see getDistanceFromPoint, findNearestProportionalPositionTo
  246. */
  247. Point<ValueType> findNearestPointTo (Point<ValueType> point) const noexcept
  248. {
  249. return getPointAlongLineProportionally (findNearestProportionalPositionTo (point));
  250. }
  251. /** Returns true if the given point lies above this line.
  252. The return value is true if the point's y coordinate is less than the y
  253. coordinate of this line at the given x (assuming the line extends infinitely
  254. in both directions).
  255. */
  256. bool isPointAbove (Point<ValueType> point) const noexcept
  257. {
  258. return start.x != end.x
  259. && point.y < ((end.y - start.y) * (point.x - start.x)) / (end.x - start.x) + start.y;
  260. }
  261. /** Returns a lengthened copy of this line.
  262. This will extend the line by a certain amount by moving the start away from the end
  263. (leaving the end-point the same), and return the new line.
  264. */
  265. Line withLengthenedStart (ValueType distanceToLengthenBy) const noexcept
  266. {
  267. return withShortenedStart (-distanceToLengthenBy);
  268. }
  269. //==============================================================================
  270. /** Returns a shortened copy of this line.
  271. This will chop off part of the start of this line by a certain amount, (leaving the
  272. end-point the same), and return the new line.
  273. */
  274. Line withShortenedStart (ValueType distanceToShortenBy) const noexcept
  275. {
  276. return { getPointAlongLine (jmin (distanceToShortenBy, getLength())), end };
  277. }
  278. /** Returns a lengthened copy of this line.
  279. This will extend the line by a certain amount by moving the end away from the start
  280. (leaving the start-point the same), and return the new line.
  281. */
  282. Line withLengthenedEnd (ValueType distanceToLengthenBy) const noexcept
  283. {
  284. return withShortenedEnd (-distanceToLengthenBy);
  285. }
  286. /** Returns a shortened copy of this line.
  287. This will chop off part of the end of this line by a certain amount, (leaving the
  288. start-point the same), and return the new line.
  289. */
  290. Line withShortenedEnd (ValueType distanceToShortenBy) const noexcept
  291. {
  292. auto length = getLength();
  293. return { start, getPointAlongLine (length - jmin (distanceToShortenBy, length)) };
  294. }
  295. private:
  296. //==============================================================================
  297. Point<ValueType> start, end;
  298. static bool isZeroToOne (ValueType v) noexcept { return v >= 0 && v <= static_cast<ValueType> (1); }
  299. static bool findIntersection (const Point<ValueType> p1, const Point<ValueType> p2,
  300. const Point<ValueType> p3, const Point<ValueType> p4,
  301. Point<ValueType>& intersection) noexcept
  302. {
  303. if (p2 == p3)
  304. {
  305. intersection = p2;
  306. return true;
  307. }
  308. auto d1 = p2 - p1;
  309. auto d2 = p4 - p3;
  310. auto divisor = d1.x * d2.y - d2.x * d1.y;
  311. if (divisor == 0)
  312. {
  313. if (! (d1.isOrigin() || d2.isOrigin()))
  314. {
  315. if (d1.y == 0 && d2.y != 0)
  316. {
  317. auto along = (p1.y - p3.y) / d2.y;
  318. intersection = p1.withX (p3.x + along * d2.x);
  319. return isZeroToOne (along);
  320. }
  321. if (d2.y == 0 && d1.y != 0)
  322. {
  323. auto along = (p3.y - p1.y) / d1.y;
  324. intersection = p3.withX (p1.x + along * d1.x);
  325. return isZeroToOne (along);
  326. }
  327. if (d1.x == 0 && d2.x != 0)
  328. {
  329. auto along = (p1.x - p3.x) / d2.x;
  330. intersection = p1.withY (p3.y + along * d2.y);
  331. return isZeroToOne (along);
  332. }
  333. if (d2.x == 0 && d1.x != 0)
  334. {
  335. auto along = (p3.x - p1.x) / d1.x;
  336. intersection = p3.withY (p1.y + along * d1.y);
  337. return isZeroToOne (along);
  338. }
  339. }
  340. intersection = (p2 + p3) / static_cast<ValueType> (2);
  341. return false;
  342. }
  343. auto along1 = ((p1.y - p3.y) * d2.x - (p1.x - p3.x) * d2.y) / divisor;
  344. intersection = p1 + d1 * along1;
  345. if (! isZeroToOne (along1))
  346. return false;
  347. auto along2 = ((p1.y - p3.y) * d1.x - (p1.x - p3.x) * d1.y) / divisor;
  348. return isZeroToOne (along2);
  349. }
  350. };
  351. } // namespace juce