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

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