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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #ifndef JUCE_LINE_H_INCLUDED
  18. #define JUCE_LINE_H_INCLUDED
  19. //==============================================================================
  20. /**
  21. Represents a line.
  22. This class contains a bunch of useful methods for various geometric
  23. tasks.
  24. The ValueType template parameter should be a primitive type - float or double
  25. are what it's designed for. Integer types will work in a basic way, but some methods
  26. that perform mathematical operations may not compile, or they may not produce
  27. sensible results.
  28. @see Point, Rectangle, Path, Graphics::drawLine
  29. */
  30. template <typename ValueType>
  31. class Line
  32. {
  33. public:
  34. //==============================================================================
  35. /** Creates a line, using (0, 0) as its start and end points. */
  36. Line() noexcept {}
  37. /** Creates a copy of another line. */
  38. Line (const Line& other) noexcept
  39. : start (other.start),
  40. end (other.end)
  41. {
  42. }
  43. /** Creates a line based on the coordinates of its start and end points. */
  44. Line (ValueType startX, ValueType startY, ValueType endX, ValueType endY) noexcept
  45. : start (startX, startY),
  46. end (endX, endY)
  47. {
  48. }
  49. /** Creates a line from its start and end points. */
  50. Line (const Point<ValueType> startPoint,
  51. const Point<ValueType> endPoint) noexcept
  52. : start (startPoint),
  53. end (endPoint)
  54. {
  55. }
  56. /** Copies a line from another one. */
  57. Line& operator= (const Line& other) noexcept
  58. {
  59. start = other.start;
  60. end = other.end;
  61. return *this;
  62. }
  63. /** Destructor. */
  64. ~Line() noexcept {}
  65. //==============================================================================
  66. /** Returns the x coordinate of the line's start point. */
  67. inline ValueType getStartX() const noexcept { return start.x; }
  68. /** Returns the y coordinate of the line's start point. */
  69. inline ValueType getStartY() const noexcept { return start.y; }
  70. /** Returns the x coordinate of the line's end point. */
  71. inline ValueType getEndX() const noexcept { return end.x; }
  72. /** Returns the y coordinate of the line's end point. */
  73. inline ValueType getEndY() const noexcept { return end.y; }
  74. /** Returns the line's start point. */
  75. inline Point<ValueType> getStart() const noexcept { return start; }
  76. /** Returns the line's end point. */
  77. inline Point<ValueType> getEnd() const noexcept { return end; }
  78. /** Changes this line's start point */
  79. void setStart (ValueType newStartX, ValueType newStartY) noexcept { start.setXY (newStartX, newStartY); }
  80. /** Changes this line's end point */
  81. void setEnd (ValueType newEndX, ValueType newEndY) noexcept { end.setXY (newEndX, newEndY); }
  82. /** Changes this line's start point */
  83. void setStart (const Point<ValueType> newStart) noexcept { start = newStart; }
  84. /** Changes this line's end point */
  85. void setEnd (const Point<ValueType> newEnd) noexcept { end = newEnd; }
  86. /** Returns a line that is the same as this one, but with the start and end reversed, */
  87. const Line reversed() const noexcept { return Line (end, start); }
  88. /** Applies an affine transform to the line's start and end points. */
  89. void applyTransform (const AffineTransform& transform) noexcept
  90. {
  91. start.applyTransform (transform);
  92. end.applyTransform (transform);
  93. }
  94. //==============================================================================
  95. /** Returns the length of the line. */
  96. ValueType getLength() const noexcept { return start.getDistanceFrom (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. /** Casts this line to float coordinates. */
  107. Line<float> toFloat() const noexcept { return Line<float> (start.toFloat(), end.toFloat()); }
  108. /** Casts this line to double coordinates. */
  109. Line<double> toDouble() const noexcept { return Line<double> (start.toDouble(), end.toDouble()); }
  110. //==============================================================================
  111. /** Compares two lines. */
  112. bool operator== (const Line& other) const noexcept { return start == other.start && end == other.end; }
  113. /** Compares two lines. */
  114. bool operator!= (const Line& other) const noexcept { return start != other.start || end != other.end; }
  115. //==============================================================================
  116. /** Finds the intersection between two lines.
  117. @param line the line to intersect with
  118. @returns the point at which the lines intersect, even if this lies beyond the end of the lines
  119. */
  120. Point<ValueType> getIntersection (const Line& line) const noexcept
  121. {
  122. Point<ValueType> p;
  123. findIntersection (start, end, line.start, line.end, p);
  124. return p;
  125. }
  126. /** Finds the intersection between two lines.
  127. @param line the other line
  128. @param intersection the position of the point where the lines meet (or
  129. where they would meet if they were infinitely long)
  130. the intersection (if the lines intersect). If the lines
  131. are parallel, this will just be set to the position
  132. of one of the line's endpoints.
  133. @returns true if the line segments intersect; false if they dont. Even if they
  134. don't intersect, the intersection coordinates returned will still
  135. be valid
  136. */
  137. bool intersects (const Line& line, Point<ValueType>& intersection) const noexcept
  138. {
  139. return findIntersection (start, end, line.start, line.end, intersection);
  140. }
  141. /** Returns true if this line intersects another. */
  142. bool intersects (const Line& other) const noexcept
  143. {
  144. Point<ValueType> ignored;
  145. return findIntersection (start, end, other.start, other.end, ignored);
  146. }
  147. //==============================================================================
  148. /** Returns the location of the point which is a given distance along this line.
  149. @param distanceFromStart the distance to move along the line from its
  150. start point. This value can be negative or longer
  151. than the line itself
  152. @see getPointAlongLineProportionally
  153. */
  154. Point<ValueType> getPointAlongLine (ValueType distanceFromStart) const noexcept
  155. {
  156. return start + (end - start) * (distanceFromStart / getLength());
  157. }
  158. /** Returns a point which is a certain distance along and to the side of this line.
  159. This effectively moves a given distance along the line, then another distance
  160. perpendicularly to this, and returns the resulting position.
  161. @param distanceFromStart the distance to move along the line from its
  162. start point. This value can be negative or longer
  163. than the line itself
  164. @param perpendicularDistance how far to move sideways from the line. If you're
  165. looking along the line from its start towards its
  166. end, then a positive value here will move to the
  167. right, negative value move to the left.
  168. */
  169. Point<ValueType> getPointAlongLine (ValueType distanceFromStart,
  170. ValueType perpendicularDistance) const noexcept
  171. {
  172. const Point<ValueType> delta (end - start);
  173. const double length = juce_hypot ((double) delta.x,
  174. (double) delta.y);
  175. if (length <= 0)
  176. return start;
  177. return Point<ValueType> (start.x + static_cast <ValueType> ((delta.x * distanceFromStart - delta.y * perpendicularDistance) / length),
  178. start.y + static_cast <ValueType> ((delta.y * distanceFromStart + delta.x * perpendicularDistance) / length));
  179. }
  180. /** Returns the location of the point which is a given distance along this line
  181. proportional to the line's length.
  182. @param proportionOfLength the distance to move along the line from its
  183. start point, in multiples of the line's length.
  184. So a value of 0.0 will return the line's start point
  185. and a value of 1.0 will return its end point. (This value
  186. can be negative or greater than 1.0).
  187. @see getPointAlongLine
  188. */
  189. Point<ValueType> getPointAlongLineProportionally (ValueType proportionOfLength) const noexcept
  190. {
  191. return start + (end - start) * proportionOfLength;
  192. }
  193. /** Returns the smallest distance between this line segment and a given point.
  194. So if the point is close to the line, this will return the perpendicular
  195. distance from the line; if the point is a long way beyond one of the line's
  196. end-point's, it'll return the straight-line distance to the nearest end-point.
  197. pointOnLine receives the position of the point that is found.
  198. @returns the point's distance from the line
  199. @see getPositionAlongLineOfNearestPoint
  200. */
  201. ValueType getDistanceFromPoint (const Point<ValueType> targetPoint,
  202. Point<ValueType>& pointOnLine) const noexcept
  203. {
  204. const Point<ValueType> delta (end - start);
  205. const double length = delta.x * delta.x + delta.y * delta.y;
  206. if (length > 0)
  207. {
  208. const double prop = ((targetPoint.x - start.x) * delta.x
  209. + (targetPoint.y - start.y) * delta.y) / length;
  210. if (prop >= 0 && prop <= 1.0)
  211. {
  212. pointOnLine = start + delta * static_cast <ValueType> (prop);
  213. return targetPoint.getDistanceFrom (pointOnLine);
  214. }
  215. }
  216. const float fromStart = targetPoint.getDistanceFrom (start);
  217. const float fromEnd = targetPoint.getDistanceFrom (end);
  218. if (fromStart < fromEnd)
  219. {
  220. pointOnLine = start;
  221. return fromStart;
  222. }
  223. else
  224. {
  225. pointOnLine = end;
  226. return fromEnd;
  227. }
  228. }
  229. /** Finds the point on this line which is nearest to a given point, and
  230. returns its position as a proportional position along the line.
  231. @returns a value 0 to 1.0 which is the distance along this line from the
  232. line's start to the point which is nearest to the point passed-in. To
  233. turn this number into a position, use getPointAlongLineProportionally().
  234. @see getDistanceFromPoint, getPointAlongLineProportionally
  235. */
  236. ValueType findNearestProportionalPositionTo (const Point<ValueType> point) const noexcept
  237. {
  238. const Point<ValueType> delta (end - start);
  239. const double length = delta.x * delta.x + delta.y * delta.y;
  240. return length <= 0 ? 0
  241. : jlimit (ValueType(), static_cast <ValueType> (1),
  242. static_cast <ValueType> ((((point.x - start.x) * delta.x
  243. + (point.y - start.y) * delta.y) / length)));
  244. }
  245. /** Finds the point on this line which is nearest to a given point.
  246. @see getDistanceFromPoint, findNearestProportionalPositionTo
  247. */
  248. Point<ValueType> findNearestPointTo (const Point<ValueType> point) const noexcept
  249. {
  250. return getPointAlongLineProportionally (findNearestProportionalPositionTo (point));
  251. }
  252. /** Returns true if the given point lies above this line.
  253. The return value is true if the point's y coordinate is less than the y
  254. coordinate of this line at the given x (assuming the line extends infinitely
  255. in both directions).
  256. */
  257. bool isPointAbove (const Point<ValueType> point) const noexcept
  258. {
  259. return start.x != end.x
  260. && point.y < ((end.y - start.y)
  261. * (point.x - start.x)) / (end.x - start.x) + start.y;
  262. }
  263. //==============================================================================
  264. /** Returns a shortened copy of this line.
  265. This will chop off part of the start of this line by a certain amount, (leaving the
  266. end-point the same), and return the new line.
  267. */
  268. Line withShortenedStart (ValueType distanceToShortenBy) const noexcept
  269. {
  270. return Line (getPointAlongLine (jmin (distanceToShortenBy, getLength())), end);
  271. }
  272. /** Returns a shortened copy of this line.
  273. This will chop off part of the end of this line by a certain amount, (leaving the
  274. start-point the same), and return the new line.
  275. */
  276. Line withShortenedEnd (ValueType distanceToShortenBy) const noexcept
  277. {
  278. const ValueType length = getLength();
  279. return Line (start, getPointAlongLine (length - jmin (distanceToShortenBy, length)));
  280. }
  281. private:
  282. //==============================================================================
  283. Point<ValueType> start, end;
  284. static bool findIntersection (const Point<ValueType> p1, const Point<ValueType> p2,
  285. const Point<ValueType> p3, const Point<ValueType> p4,
  286. Point<ValueType>& intersection) noexcept
  287. {
  288. if (p2 == p3)
  289. {
  290. intersection = p2;
  291. return true;
  292. }
  293. const Point<ValueType> d1 (p2 - p1);
  294. const Point<ValueType> d2 (p4 - p3);
  295. const ValueType divisor = d1.x * d2.y - d2.x * d1.y;
  296. if (divisor == 0)
  297. {
  298. if (! (d1.isOrigin() || d2.isOrigin()))
  299. {
  300. if (d1.y == 0 && d2.y != 0)
  301. {
  302. const ValueType along = (p1.y - p3.y) / d2.y;
  303. intersection = p1.withX (p3.x + along * d2.x);
  304. return along >= 0 && along <= static_cast <ValueType> (1);
  305. }
  306. else if (d2.y == 0 && d1.y != 0)
  307. {
  308. const ValueType along = (p3.y - p1.y) / d1.y;
  309. intersection = p3.withX (p1.x + along * d1.x);
  310. return along >= 0 && along <= static_cast <ValueType> (1);
  311. }
  312. else if (d1.x == 0 && d2.x != 0)
  313. {
  314. const ValueType along = (p1.x - p3.x) / d2.x;
  315. intersection = p1.withY (p3.y + along * d2.y);
  316. return along >= 0 && along <= static_cast <ValueType> (1);
  317. }
  318. else if (d2.x == 0 && d1.x != 0)
  319. {
  320. const ValueType along = (p3.x - p1.x) / d1.x;
  321. intersection = p3.withY (p1.y + along * d1.y);
  322. return along >= 0 && along <= static_cast <ValueType> (1);
  323. }
  324. }
  325. intersection = (p2 + p3) / static_cast <ValueType> (2);
  326. return false;
  327. }
  328. const ValueType along1 = ((p1.y - p3.y) * d2.x - (p1.x - p3.x) * d2.y) / divisor;
  329. intersection = p1 + d1 * along1;
  330. if (along1 < 0 || along1 > static_cast <ValueType> (1))
  331. return false;
  332. const ValueType along2 = ((p1.y - p3.y) * d1.x - (p1.x - p3.x) * d1.y) / divisor;
  333. return along2 >= 0 && along2 <= static_cast <ValueType> (1);
  334. }
  335. };
  336. #endif // JUCE_LINE_H_INCLUDED