The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

431 lines
18KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI 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), end (other.end)
  40. {
  41. }
  42. /** Creates a line based on the coordinates of its start and end points. */
  43. Line (ValueType startX, ValueType startY, ValueType endX, ValueType endY) noexcept
  44. : start (startX, startY), end (endX, endY)
  45. {
  46. }
  47. /** Creates a line from its start and end points. */
  48. Line (Point<ValueType> startPoint,
  49. Point<ValueType> endPoint) noexcept
  50. : start (startPoint), end (endPoint)
  51. {
  52. }
  53. /** Copies a line from another one. */
  54. Line& operator= (const Line& other) noexcept
  55. {
  56. start = other.start;
  57. end = other.end;
  58. return *this;
  59. }
  60. /** Destructor. */
  61. ~Line() noexcept {}
  62. //==============================================================================
  63. /** Returns the x coordinate of the line's start point. */
  64. inline ValueType getStartX() const noexcept { return start.x; }
  65. /** Returns the y coordinate of the line's start point. */
  66. inline ValueType getStartY() const noexcept { return start.y; }
  67. /** Returns the x coordinate of the line's end point. */
  68. inline ValueType getEndX() const noexcept { return end.x; }
  69. /** Returns the y coordinate of the line's end point. */
  70. inline ValueType getEndY() const noexcept { return end.y; }
  71. /** Returns the line's start point. */
  72. inline Point<ValueType> getStart() const noexcept { return start; }
  73. /** Returns the line's end point. */
  74. inline Point<ValueType> getEnd() const noexcept { return end; }
  75. /** Changes this line's start point */
  76. void setStart (ValueType newStartX, ValueType newStartY) noexcept { start.setXY (newStartX, newStartY); }
  77. /** Changes this line's end point */
  78. void setEnd (ValueType newEndX, ValueType newEndY) noexcept { end.setXY (newEndX, newEndY); }
  79. /** Changes this line's start point */
  80. void setStart (const Point<ValueType> newStart) noexcept { start = newStart; }
  81. /** Changes this line's end point */
  82. void setEnd (const Point<ValueType> newEnd) noexcept { end = newEnd; }
  83. /** Returns a line that is the same as this one, but with the start and end reversed, */
  84. Line reversed() const noexcept { return Line (end, start); }
  85. /** Applies an affine transform to the line's start and end points. */
  86. void applyTransform (const AffineTransform& transform) noexcept
  87. {
  88. start.applyTransform (transform);
  89. end.applyTransform (transform);
  90. }
  91. //==============================================================================
  92. /** Returns the length of the line. */
  93. ValueType getLength() const noexcept { return start.getDistanceFrom (end); }
  94. /** Returns the length of the line. */
  95. ValueType getLengthSquared() const noexcept { return start.getDistanceSquaredFrom (end); }
  96. /** Returns true if the line's start and end x coordinates are the same. */
  97. bool isVertical() const noexcept { return start.x == end.x; }
  98. /** Returns true if the line's start and end y coordinates are the same. */
  99. bool isHorizontal() const noexcept { return start.y == end.y; }
  100. /** Returns the line's angle.
  101. This value is the number of radians clockwise from the 12 o'clock direction,
  102. where the line's start point is considered to be at the centre.
  103. */
  104. typename Point<ValueType>::FloatType getAngle() const noexcept { return start.getAngleToPoint (end); }
  105. /** Creates a line from a start point, length and angle.
  106. This angle is the number of radians clockwise from the 12 o'clock direction,
  107. where the line's start point is considered to be at the centre.
  108. */
  109. static Line fromStartAndAngle (Point<ValueType> startPoint, ValueType length, ValueType angle) noexcept
  110. {
  111. return Line (startPoint, startPoint.getPointOnCircumference (length, angle));
  112. }
  113. //==============================================================================
  114. /** Casts this line to float coordinates. */
  115. Line<float> toFloat() const noexcept { return Line<float> (start.toFloat(), end.toFloat()); }
  116. /** Casts this line to double coordinates. */
  117. Line<double> toDouble() const noexcept { return Line<double> (start.toDouble(), end.toDouble()); }
  118. //==============================================================================
  119. /** Compares two lines. */
  120. bool operator== (Line other) const noexcept { return start == other.start && end == other.end; }
  121. /** Compares two lines. */
  122. bool operator!= (Line other) const noexcept { return start != other.start || end != other.end; }
  123. //==============================================================================
  124. /** Finds the intersection between two lines.
  125. @param line the line to intersect with
  126. @returns the point at which the lines intersect, even if this lies beyond the end of the lines
  127. */
  128. Point<ValueType> getIntersection (Line line) const noexcept
  129. {
  130. Point<ValueType> p;
  131. findIntersection (start, end, line.start, line.end, p);
  132. return p;
  133. }
  134. /** Finds the intersection between two lines.
  135. @param line the other line
  136. @param intersection the position of the point where the lines meet (or
  137. where they would meet if they were infinitely long)
  138. the intersection (if the lines intersect). If the lines
  139. are parallel, this will just be set to the position
  140. of one of the line's endpoints.
  141. @returns true if the line segments intersect; false if they dont. Even if they
  142. don't intersect, the intersection coordinates returned will still
  143. be valid
  144. */
  145. bool intersects (Line line, Point<ValueType>& intersection) const noexcept
  146. {
  147. return findIntersection (start, end, line.start, line.end, intersection);
  148. }
  149. /** Returns true if this line intersects another. */
  150. bool intersects (Line other) const noexcept
  151. {
  152. Point<ValueType> ignored;
  153. return findIntersection (start, end, other.start, other.end, ignored);
  154. }
  155. //==============================================================================
  156. /** Returns the location of the point which is a given distance along this line.
  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. @see getPointAlongLineProportionally
  161. */
  162. Point<ValueType> getPointAlongLine (ValueType distanceFromStart) const noexcept
  163. {
  164. return start + (end - start) * (distanceFromStart / getLength());
  165. }
  166. /** Returns a point which is a certain distance along and to the side of this line.
  167. This effectively moves a given distance along the line, then another distance
  168. perpendicularly to this, and returns the resulting position.
  169. @param distanceFromStart the distance to move along the line from its
  170. start point. This value can be negative or longer
  171. than the line itself
  172. @param perpendicularDistance how far to move sideways from the line. If you're
  173. looking along the line from its start towards its
  174. end, then a positive value here will move to the
  175. right, negative value move to the left.
  176. */
  177. Point<ValueType> getPointAlongLine (ValueType distanceFromStart,
  178. ValueType perpendicularDistance) const noexcept
  179. {
  180. const Point<ValueType> delta (end - start);
  181. const double length = juce_hypot ((double) delta.x,
  182. (double) delta.y);
  183. if (length <= 0)
  184. return start;
  185. return Point<ValueType> (start.x + static_cast<ValueType> ((delta.x * distanceFromStart - delta.y * perpendicularDistance) / length),
  186. start.y + static_cast<ValueType> ((delta.y * distanceFromStart + delta.x * perpendicularDistance) / length));
  187. }
  188. /** Returns the location of the point which is a given distance along this line
  189. proportional to the line's length.
  190. @param proportionOfLength the distance to move along the line from its
  191. start point, in multiples of the line's length.
  192. So a value of 0.0 will return the line's start point
  193. and a value of 1.0 will return its end point. (This value
  194. can be negative or greater than 1.0).
  195. @see getPointAlongLine
  196. */
  197. Point<ValueType> getPointAlongLineProportionally (ValueType proportionOfLength) const noexcept
  198. {
  199. return start + (end - start) * proportionOfLength;
  200. }
  201. /** Returns the smallest distance between this line segment and a given point.
  202. So if the point is close to the line, this will return the perpendicular
  203. distance from the line; if the point is a long way beyond one of the line's
  204. end-point's, it'll return the straight-line distance to the nearest end-point.
  205. pointOnLine receives the position of the point that is found.
  206. @returns the point's distance from the line
  207. @see getPositionAlongLineOfNearestPoint
  208. */
  209. ValueType getDistanceFromPoint (Point<ValueType> targetPoint,
  210. Point<ValueType>& pointOnLine) const noexcept
  211. {
  212. const Point<ValueType> delta (end - start);
  213. const double length = delta.x * delta.x + delta.y * delta.y;
  214. if (length > 0)
  215. {
  216. const double prop = ((targetPoint.x - start.x) * delta.x
  217. + (targetPoint.y - start.y) * delta.y) / length;
  218. if (prop >= 0 && prop <= 1.0)
  219. {
  220. pointOnLine = start + delta * static_cast<ValueType> (prop);
  221. return targetPoint.getDistanceFrom (pointOnLine);
  222. }
  223. }
  224. const float fromStart = targetPoint.getDistanceFrom (start);
  225. const float fromEnd = targetPoint.getDistanceFrom (end);
  226. if (fromStart < fromEnd)
  227. {
  228. pointOnLine = start;
  229. return fromStart;
  230. }
  231. else
  232. {
  233. pointOnLine = end;
  234. return fromEnd;
  235. }
  236. }
  237. /** Finds the point on this line which is nearest to a given point, and
  238. returns its position as a proportional position along the line.
  239. @returns a value 0 to 1.0 which is the distance along this line from the
  240. line's start to the point which is nearest to the point passed-in. To
  241. turn this number into a position, use getPointAlongLineProportionally().
  242. @see getDistanceFromPoint, getPointAlongLineProportionally
  243. */
  244. ValueType findNearestProportionalPositionTo (Point<ValueType> point) const noexcept
  245. {
  246. const Point<ValueType> delta (end - start);
  247. const double length = delta.x * delta.x + delta.y * delta.y;
  248. return length <= 0 ? 0
  249. : jlimit (ValueType(), static_cast<ValueType> (1),
  250. static_cast<ValueType> ((((point.x - start.x) * delta.x
  251. + (point.y - start.y) * delta.y) / length)));
  252. }
  253. /** Finds the point on this line which is nearest to a given point.
  254. @see getDistanceFromPoint, findNearestProportionalPositionTo
  255. */
  256. Point<ValueType> findNearestPointTo (Point<ValueType> point) const noexcept
  257. {
  258. return getPointAlongLineProportionally (findNearestProportionalPositionTo (point));
  259. }
  260. /** Returns true if the given point lies above this line.
  261. The return value is true if the point's y coordinate is less than the y
  262. coordinate of this line at the given x (assuming the line extends infinitely
  263. in both directions).
  264. */
  265. bool isPointAbove (Point<ValueType> point) const noexcept
  266. {
  267. return start.x != end.x
  268. && point.y < ((end.y - start.y)
  269. * (point.x - start.x)) / (end.x - start.x) + start.y;
  270. }
  271. //==============================================================================
  272. /** Returns a shortened copy of this line.
  273. This will chop off part of the start of this line by a certain amount, (leaving the
  274. end-point the same), and return the new line.
  275. */
  276. Line withShortenedStart (ValueType distanceToShortenBy) const noexcept
  277. {
  278. return Line (getPointAlongLine (jmin (distanceToShortenBy, getLength())), end);
  279. }
  280. /** Returns a shortened copy of this line.
  281. This will chop off part of the end of this line by a certain amount, (leaving the
  282. start-point the same), and return the new line.
  283. */
  284. Line withShortenedEnd (ValueType distanceToShortenBy) const noexcept
  285. {
  286. const ValueType length = getLength();
  287. return Line (start, getPointAlongLine (length - jmin (distanceToShortenBy, length)));
  288. }
  289. private:
  290. //==============================================================================
  291. Point<ValueType> start, end;
  292. static bool findIntersection (const Point<ValueType> p1, const Point<ValueType> p2,
  293. const Point<ValueType> p3, const Point<ValueType> p4,
  294. Point<ValueType>& intersection) noexcept
  295. {
  296. if (p2 == p3)
  297. {
  298. intersection = p2;
  299. return true;
  300. }
  301. const Point<ValueType> d1 (p2 - p1);
  302. const Point<ValueType> d2 (p4 - p3);
  303. const ValueType divisor = d1.x * d2.y - d2.x * d1.y;
  304. if (divisor == 0)
  305. {
  306. if (! (d1.isOrigin() || d2.isOrigin()))
  307. {
  308. if (d1.y == 0 && d2.y != 0)
  309. {
  310. const ValueType along = (p1.y - p3.y) / d2.y;
  311. intersection = p1.withX (p3.x + along * d2.x);
  312. return along >= 0 && along <= static_cast<ValueType> (1);
  313. }
  314. if (d2.y == 0 && d1.y != 0)
  315. {
  316. const ValueType along = (p3.y - p1.y) / d1.y;
  317. intersection = p3.withX (p1.x + along * d1.x);
  318. return along >= 0 && along <= static_cast<ValueType> (1);
  319. }
  320. if (d1.x == 0 && d2.x != 0)
  321. {
  322. const ValueType along = (p1.x - p3.x) / d2.x;
  323. intersection = p1.withY (p3.y + along * d2.y);
  324. return along >= 0 && along <= static_cast<ValueType> (1);
  325. }
  326. if (d2.x == 0 && d1.x != 0)
  327. {
  328. const ValueType along = (p3.x - p1.x) / d1.x;
  329. intersection = p3.withY (p1.y + along * d1.y);
  330. return along >= 0 && along <= static_cast<ValueType> (1);
  331. }
  332. }
  333. intersection = (p2 + p3) / static_cast<ValueType> (2);
  334. return false;
  335. }
  336. const ValueType along1 = ((p1.y - p3.y) * d2.x - (p1.x - p3.x) * d2.y) / divisor;
  337. intersection = p1 + d1 * along1;
  338. if (along1 < 0 || along1 > static_cast<ValueType> (1))
  339. return false;
  340. const ValueType along2 = ((p1.y - p3.y) * d1.x - (p1.x - p3.x) * d1.y) / divisor;
  341. return along2 >= 0 && along2 <= static_cast<ValueType> (1);
  342. }
  343. };
  344. #endif // JUCE_LINE_H_INCLUDED