juce_Line.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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),
  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 the length of the line. */
  98. ValueType getLengthSquared() const noexcept { return start.getDistanceSquaredFrom (end); }
  99. /** Returns true if the line's start and end x coordinates are the same. */
  100. bool isVertical() const noexcept { return start.x == end.x; }
  101. /** Returns true if the line's start and end y coordinates are the same. */
  102. bool isHorizontal() const noexcept { return start.y == end.y; }
  103. /** Returns the line's angle.
  104. This value is the number of radians clockwise from the 12 o'clock direction,
  105. where the line's start point is considered to be at the centre.
  106. */
  107. typename Point<ValueType>::FloatType getAngle() const noexcept { return start.getAngleToPoint (end); }
  108. /** Casts this line to float coordinates. */
  109. Line<float> toFloat() const noexcept { return Line<float> (start.toFloat(), end.toFloat()); }
  110. /** Casts this line to double coordinates. */
  111. Line<double> toDouble() const noexcept { return Line<double> (start.toDouble(), end.toDouble()); }
  112. //==============================================================================
  113. /** Compares two lines. */
  114. bool operator== (const Line& other) const noexcept { return start == other.start && end == other.end; }
  115. /** Compares two lines. */
  116. bool operator!= (const Line& other) const noexcept { return start != other.start || end != other.end; }
  117. //==============================================================================
  118. /** Finds the intersection between two lines.
  119. @param line the line to intersect with
  120. @returns the point at which the lines intersect, even if this lies beyond the end of the lines
  121. */
  122. Point<ValueType> getIntersection (const Line& line) const noexcept
  123. {
  124. Point<ValueType> p;
  125. findIntersection (start, end, line.start, line.end, p);
  126. return p;
  127. }
  128. /** Finds the intersection between two lines.
  129. @param line the other line
  130. @param intersection the position of the point where the lines meet (or
  131. where they would meet if they were infinitely long)
  132. the intersection (if the lines intersect). If the lines
  133. are parallel, this will just be set to the position
  134. of one of the line's endpoints.
  135. @returns true if the line segments intersect; false if they dont. Even if they
  136. don't intersect, the intersection coordinates returned will still
  137. be valid
  138. */
  139. bool intersects (const Line& line, Point<ValueType>& intersection) const noexcept
  140. {
  141. return findIntersection (start, end, line.start, line.end, intersection);
  142. }
  143. /** Returns true if this line intersects another. */
  144. bool intersects (const Line& other) const noexcept
  145. {
  146. Point<ValueType> ignored;
  147. return findIntersection (start, end, other.start, other.end, ignored);
  148. }
  149. //==============================================================================
  150. /** Returns the location of the point which is a given distance along this line.
  151. @param distanceFromStart the distance to move along the line from its
  152. start point. This value can be negative or longer
  153. than the line itself
  154. @see getPointAlongLineProportionally
  155. */
  156. Point<ValueType> getPointAlongLine (ValueType distanceFromStart) const noexcept
  157. {
  158. return start + (end - start) * (distanceFromStart / getLength());
  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. const Point<ValueType> delta (end - start);
  175. const double length = juce_hypot ((double) delta.x,
  176. (double) delta.y);
  177. if (length <= 0)
  178. return start;
  179. return Point<ValueType> (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 (ValueType 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 (const Point<ValueType> targetPoint,
  204. Point<ValueType>& pointOnLine) const noexcept
  205. {
  206. const Point<ValueType> delta (end - start);
  207. const double length = delta.x * delta.x + delta.y * delta.y;
  208. if (length > 0)
  209. {
  210. const double prop = ((targetPoint.x - start.x) * delta.x
  211. + (targetPoint.y - start.y) * delta.y) / length;
  212. if (prop >= 0 && prop <= 1.0)
  213. {
  214. pointOnLine = start + delta * static_cast<ValueType> (prop);
  215. return targetPoint.getDistanceFrom (pointOnLine);
  216. }
  217. }
  218. const float fromStart = targetPoint.getDistanceFrom (start);
  219. const float fromEnd = targetPoint.getDistanceFrom (end);
  220. if (fromStart < fromEnd)
  221. {
  222. pointOnLine = start;
  223. return fromStart;
  224. }
  225. else
  226. {
  227. pointOnLine = end;
  228. return fromEnd;
  229. }
  230. }
  231. /** Finds the point on this line which is nearest to a given point, and
  232. returns its position as a proportional position along the line.
  233. @returns a value 0 to 1.0 which is the distance along this line from the
  234. line's start to the point which is nearest to the point passed-in. To
  235. turn this number into a position, use getPointAlongLineProportionally().
  236. @see getDistanceFromPoint, getPointAlongLineProportionally
  237. */
  238. ValueType findNearestProportionalPositionTo (const Point<ValueType> point) const noexcept
  239. {
  240. const Point<ValueType> delta (end - start);
  241. const double length = delta.x * delta.x + delta.y * delta.y;
  242. return length <= 0 ? 0
  243. : jlimit (ValueType(), static_cast<ValueType> (1),
  244. static_cast<ValueType> ((((point.x - start.x) * delta.x
  245. + (point.y - start.y) * delta.y) / length)));
  246. }
  247. /** Finds the point on this line which is nearest to a given point.
  248. @see getDistanceFromPoint, findNearestProportionalPositionTo
  249. */
  250. Point<ValueType> findNearestPointTo (const Point<ValueType> point) const noexcept
  251. {
  252. return getPointAlongLineProportionally (findNearestProportionalPositionTo (point));
  253. }
  254. /** Returns true if the given point lies above this line.
  255. The return value is true if the point's y coordinate is less than the y
  256. coordinate of this line at the given x (assuming the line extends infinitely
  257. in both directions).
  258. */
  259. bool isPointAbove (const Point<ValueType> point) const noexcept
  260. {
  261. return start.x != end.x
  262. && point.y < ((end.y - start.y)
  263. * (point.x - start.x)) / (end.x - start.x) + start.y;
  264. }
  265. //==============================================================================
  266. /** Returns a shortened copy of this line.
  267. This will chop off part of the start of this line by a certain amount, (leaving the
  268. end-point the same), and return the new line.
  269. */
  270. Line withShortenedStart (ValueType distanceToShortenBy) const noexcept
  271. {
  272. return Line (getPointAlongLine (jmin (distanceToShortenBy, getLength())), end);
  273. }
  274. /** Returns a shortened copy of this line.
  275. This will chop off part of the end of this line by a certain amount, (leaving the
  276. start-point the same), and return the new line.
  277. */
  278. Line withShortenedEnd (ValueType distanceToShortenBy) const noexcept
  279. {
  280. const ValueType length = getLength();
  281. return Line (start, getPointAlongLine (length - jmin (distanceToShortenBy, length)));
  282. }
  283. private:
  284. //==============================================================================
  285. Point<ValueType> start, end;
  286. static bool findIntersection (const Point<ValueType> p1, const Point<ValueType> p2,
  287. const Point<ValueType> p3, const Point<ValueType> p4,
  288. Point<ValueType>& intersection) noexcept
  289. {
  290. if (p2 == p3)
  291. {
  292. intersection = p2;
  293. return true;
  294. }
  295. const Point<ValueType> d1 (p2 - p1);
  296. const Point<ValueType> d2 (p4 - p3);
  297. const ValueType divisor = d1.x * d2.y - d2.x * d1.y;
  298. if (divisor == 0)
  299. {
  300. if (! (d1.isOrigin() || d2.isOrigin()))
  301. {
  302. if (d1.y == 0 && d2.y != 0)
  303. {
  304. const ValueType along = (p1.y - p3.y) / d2.y;
  305. intersection = p1.withX (p3.x + along * d2.x);
  306. return along >= 0 && along <= static_cast<ValueType> (1);
  307. }
  308. else if (d2.y == 0 && d1.y != 0)
  309. {
  310. const ValueType along = (p3.y - p1.y) / d1.y;
  311. intersection = p3.withX (p1.x + along * d1.x);
  312. return along >= 0 && along <= static_cast<ValueType> (1);
  313. }
  314. else if (d1.x == 0 && d2.x != 0)
  315. {
  316. const ValueType along = (p1.x - p3.x) / d2.x;
  317. intersection = p1.withY (p3.y + along * d2.y);
  318. return along >= 0 && along <= static_cast<ValueType> (1);
  319. }
  320. else if (d2.x == 0 && d1.x != 0)
  321. {
  322. const ValueType along = (p3.x - p1.x) / d1.x;
  323. intersection = p3.withY (p1.y + along * d1.y);
  324. return along >= 0 && along <= static_cast<ValueType> (1);
  325. }
  326. }
  327. intersection = (p2 + p3) / static_cast<ValueType> (2);
  328. return false;
  329. }
  330. const ValueType along1 = ((p1.y - p3.y) * d2.x - (p1.x - p3.x) * d2.y) / divisor;
  331. intersection = p1 + d1 * along1;
  332. if (along1 < 0 || along1 > static_cast<ValueType> (1))
  333. return false;
  334. const ValueType along2 = ((p1.y - p3.y) * d1.x - (p1.x - p3.x) * d1.y) / divisor;
  335. return along2 >= 0 && along2 <= static_cast<ValueType> (1);
  336. }
  337. };
  338. #endif // JUCE_LINE_H_INCLUDED