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 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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 other line
  118. @param intersection the position of the point where the lines meet (or
  119. where they would meet if they were infinitely long)
  120. the intersection (if the lines intersect). If the lines
  121. are parallel, this will just be set to the position
  122. of one of the line's endpoints.
  123. @returns true if the line segments intersect; false if they dont. Even if they
  124. don't intersect, the intersection coordinates returned will still
  125. be valid
  126. */
  127. bool intersects (const Line& line, Point<ValueType>& intersection) const noexcept
  128. {
  129. return findIntersection (start, end, line.start, line.end, intersection);
  130. }
  131. /** Finds the intersection between two lines.
  132. @param line the line to intersect with
  133. @returns the point at which the lines intersect, even if this lies beyond the end of the lines
  134. */
  135. Point<ValueType> getIntersection (const Line& line) const noexcept
  136. {
  137. Point<ValueType> p;
  138. findIntersection (start, end, line.start, line.end, p);
  139. return p;
  140. }
  141. //==============================================================================
  142. /** Returns the location of the point which is a given distance along this line.
  143. @param distanceFromStart the distance to move along the line from its
  144. start point. This value can be negative or longer
  145. than the line itself
  146. @see getPointAlongLineProportionally
  147. */
  148. Point<ValueType> getPointAlongLine (ValueType distanceFromStart) const noexcept
  149. {
  150. return start + (end - start) * (distanceFromStart / getLength());
  151. }
  152. /** Returns a point which is a certain distance along and to the side of this line.
  153. This effectively moves a given distance along the line, then another distance
  154. perpendicularly to this, and returns the resulting position.
  155. @param distanceFromStart the distance to move along the line from its
  156. start point. This value can be negative or longer
  157. than the line itself
  158. @param perpendicularDistance how far to move sideways from the line. If you're
  159. looking along the line from its start towards its
  160. end, then a positive value here will move to the
  161. right, negative value move to the left.
  162. */
  163. Point<ValueType> getPointAlongLine (ValueType distanceFromStart,
  164. ValueType perpendicularDistance) const noexcept
  165. {
  166. const Point<ValueType> delta (end - start);
  167. const double length = juce_hypot ((double) delta.x,
  168. (double) delta.y);
  169. if (length <= 0)
  170. return start;
  171. return Point<ValueType> (start.x + static_cast <ValueType> ((delta.x * distanceFromStart - delta.y * perpendicularDistance) / length),
  172. start.y + static_cast <ValueType> ((delta.y * distanceFromStart + delta.x * perpendicularDistance) / length));
  173. }
  174. /** Returns the location of the point which is a given distance along this line
  175. proportional to the line's length.
  176. @param proportionOfLength the distance to move along the line from its
  177. start point, in multiples of the line's length.
  178. So a value of 0.0 will return the line's start point
  179. and a value of 1.0 will return its end point. (This value
  180. can be negative or greater than 1.0).
  181. @see getPointAlongLine
  182. */
  183. Point<ValueType> getPointAlongLineProportionally (ValueType proportionOfLength) const noexcept
  184. {
  185. return start + (end - start) * proportionOfLength;
  186. }
  187. /** Returns the smallest distance between this line segment and a given point.
  188. So if the point is close to the line, this will return the perpendicular
  189. distance from the line; if the point is a long way beyond one of the line's
  190. end-point's, it'll return the straight-line distance to the nearest end-point.
  191. pointOnLine receives the position of the point that is found.
  192. @returns the point's distance from the line
  193. @see getPositionAlongLineOfNearestPoint
  194. */
  195. ValueType getDistanceFromPoint (const Point<ValueType> targetPoint,
  196. Point<ValueType>& pointOnLine) const noexcept
  197. {
  198. const Point<ValueType> delta (end - start);
  199. const double length = delta.x * delta.x + delta.y * delta.y;
  200. if (length > 0)
  201. {
  202. const double prop = ((targetPoint.x - start.x) * delta.x
  203. + (targetPoint.y - start.y) * delta.y) / length;
  204. if (prop >= 0 && prop <= 1.0)
  205. {
  206. pointOnLine = start + delta * static_cast <ValueType> (prop);
  207. return targetPoint.getDistanceFrom (pointOnLine);
  208. }
  209. }
  210. const float fromStart = targetPoint.getDistanceFrom (start);
  211. const float fromEnd = targetPoint.getDistanceFrom (end);
  212. if (fromStart < fromEnd)
  213. {
  214. pointOnLine = start;
  215. return fromStart;
  216. }
  217. else
  218. {
  219. pointOnLine = end;
  220. return fromEnd;
  221. }
  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 (const Point<ValueType> point) const noexcept
  231. {
  232. const Point<ValueType> delta (end - start);
  233. const double 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 (const 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 (const Point<ValueType> point) const noexcept
  252. {
  253. return start.x != end.x
  254. && point.y < ((end.y - start.y)
  255. * (point.x - start.x)) / (end.x - start.x) + start.y;
  256. }
  257. //==============================================================================
  258. /** Returns a shortened copy of this line.
  259. This will chop off part of the start of this line by a certain amount, (leaving the
  260. end-point the same), and return the new line.
  261. */
  262. Line withShortenedStart (ValueType distanceToShortenBy) const noexcept
  263. {
  264. return Line (getPointAlongLine (jmin (distanceToShortenBy, getLength())), end);
  265. }
  266. /** Returns a shortened copy of this line.
  267. This will chop off part of the end of this line by a certain amount, (leaving the
  268. start-point the same), and return the new line.
  269. */
  270. Line withShortenedEnd (ValueType distanceToShortenBy) const noexcept
  271. {
  272. const ValueType length = getLength();
  273. return Line (start, getPointAlongLine (length - jmin (distanceToShortenBy, length)));
  274. }
  275. private:
  276. //==============================================================================
  277. Point<ValueType> start, end;
  278. static bool findIntersection (const Point<ValueType> p1, const Point<ValueType> p2,
  279. const Point<ValueType> p3, const Point<ValueType> p4,
  280. Point<ValueType>& intersection) noexcept
  281. {
  282. if (p2 == p3)
  283. {
  284. intersection = p2;
  285. return true;
  286. }
  287. const Point<ValueType> d1 (p2 - p1);
  288. const Point<ValueType> d2 (p4 - p3);
  289. const ValueType divisor = d1.x * d2.y - d2.x * d1.y;
  290. if (divisor == 0)
  291. {
  292. if (! (d1.isOrigin() || d2.isOrigin()))
  293. {
  294. if (d1.y == 0 && d2.y != 0)
  295. {
  296. const ValueType along = (p1.y - p3.y) / d2.y;
  297. intersection = p1.withX (p3.x + along * d2.x);
  298. return along >= 0 && along <= static_cast <ValueType> (1);
  299. }
  300. else if (d2.y == 0 && d1.y != 0)
  301. {
  302. const ValueType along = (p3.y - p1.y) / d1.y;
  303. intersection = p3.withX (p1.x + along * d1.x);
  304. return along >= 0 && along <= static_cast <ValueType> (1);
  305. }
  306. else if (d1.x == 0 && d2.x != 0)
  307. {
  308. const ValueType along = (p1.x - p3.x) / d2.x;
  309. intersection = p1.withY (p3.y + along * d2.y);
  310. return along >= 0 && along <= static_cast <ValueType> (1);
  311. }
  312. else if (d2.x == 0 && d1.x != 0)
  313. {
  314. const ValueType along = (p3.x - p1.x) / d1.x;
  315. intersection = p3.withY (p1.y + along * d1.y);
  316. return along >= 0 && along <= static_cast <ValueType> (1);
  317. }
  318. }
  319. intersection = (p2 + p3) / static_cast <ValueType> (2);
  320. return false;
  321. }
  322. const ValueType along1 = ((p1.y - p3.y) * d2.x - (p1.x - p3.x) * d2.y) / divisor;
  323. intersection = p1 + d1 * along1;
  324. if (along1 < 0 || along1 > static_cast <ValueType> (1))
  325. return false;
  326. const ValueType along2 = ((p1.y - p3.y) * d1.x - (p1.x - p3.x) * d1.y) / divisor;
  327. return along2 >= 0 && along2 <= static_cast <ValueType> (1);
  328. }
  329. };
  330. #endif // JUCE_LINE_H_INCLUDED