1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtCore module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #include "qline.h" |
41 | |
42 | #include "qdebug.h" |
43 | #include "qdatastream.h" |
44 | #include "qmath.h" |
45 | #include <private/qnumeric_p.h> |
46 | |
47 | QT_BEGIN_NAMESPACE |
48 | |
49 | /*! |
50 | \class QLine |
51 | \inmodule QtCore |
52 | \ingroup painting |
53 | |
54 | \brief The QLine class provides a two-dimensional vector using |
55 | integer precision. |
56 | |
57 | A QLine describes a finite length line (or a line segment) on a |
58 | two-dimensional surface. The start and end points of the line are |
59 | specified using integer point accuracy for coordinates. Use the |
60 | QLineF constructor to retrieve a floating point copy. |
61 | |
62 | \table |
63 | \row |
64 | \li \inlineimage qline-point.png |
65 | \li \inlineimage qline-coordinates.png |
66 | \endtable |
67 | |
68 | The positions of the line's start and end points can be retrieved |
69 | using the p1(), x1(), y1(), p2(), x2(), and y2() functions. The |
70 | dx() and dy() functions return the horizontal and vertical |
71 | components of the line. Use isNull() to determine whether the |
72 | QLine represents a valid line or a null line. |
73 | |
74 | Finally, the line can be translated a given offset using the |
75 | translate() function. |
76 | |
77 | \sa QLineF, QPolygon, QRect |
78 | */ |
79 | |
80 | /*! |
81 | \fn QLine::QLine() |
82 | |
83 | Constructs a null line. |
84 | */ |
85 | |
86 | /*! |
87 | \fn QLine::QLine(const QPoint &p1, const QPoint &p2) |
88 | |
89 | Constructs a line object that represents the line between \a p1 and |
90 | \a p2. |
91 | */ |
92 | |
93 | /*! |
94 | \fn QLine::QLine(int x1, int y1, int x2, int y2) |
95 | |
96 | Constructs a line object that represents the line between (\a x1, \a y1) and |
97 | (\a x2, \a y2). |
98 | */ |
99 | |
100 | /*! |
101 | \fn bool QLine::isNull() const |
102 | |
103 | Returns \c true if the line does not have distinct start and end points; |
104 | otherwise returns \c false. |
105 | */ |
106 | |
107 | /*! |
108 | \fn QPoint QLine::p1() const |
109 | |
110 | Returns the line's start point. |
111 | |
112 | \sa x1(), y1(), p2() |
113 | */ |
114 | |
115 | /*! |
116 | \fn QPoint QLine::p2() const |
117 | |
118 | Returns the line's end point. |
119 | |
120 | \sa x2(), y2(), p1() |
121 | */ |
122 | |
123 | /*! |
124 | \fn int QLine::x1() const |
125 | |
126 | Returns the x-coordinate of the line's start point. |
127 | |
128 | \sa p1() |
129 | */ |
130 | |
131 | /*! |
132 | \fn int QLine::y1() const |
133 | |
134 | Returns the y-coordinate of the line's start point. |
135 | |
136 | \sa p1() |
137 | */ |
138 | |
139 | /*! |
140 | \fn int QLine::x2() const |
141 | |
142 | Returns the x-coordinate of the line's end point. |
143 | |
144 | \sa p2() |
145 | */ |
146 | |
147 | /*! |
148 | \fn int QLine::y2() const |
149 | |
150 | Returns the y-coordinate of the line's end point. |
151 | |
152 | \sa p2() |
153 | */ |
154 | |
155 | /*! |
156 | \fn int QLine::dx() const |
157 | |
158 | Returns the horizontal component of the line's vector. |
159 | |
160 | \sa dy() |
161 | */ |
162 | |
163 | /*! |
164 | \fn int QLine::dy() const |
165 | |
166 | Returns the vertical component of the line's vector. |
167 | |
168 | \sa dx() |
169 | */ |
170 | |
171 | /*! |
172 | \fn bool QLine::operator!=(const QLine &line) const |
173 | |
174 | Returns \c true if the given \a line is not the same as \e this line. |
175 | |
176 | A line is different from another line if any of their start or |
177 | end points differ, or the internal order of the points is different. |
178 | */ |
179 | |
180 | /*! |
181 | \fn bool QLine::operator==(const QLine &line) const |
182 | |
183 | Returns \c true if the given \a line is the same as \e this line. |
184 | |
185 | A line is identical to another line if the start and end points |
186 | are identical, and the internal order of the points is the same. |
187 | */ |
188 | |
189 | /*! |
190 | \fn void QLine::translate(const QPoint &offset) |
191 | |
192 | Translates this line by the given \a offset. |
193 | */ |
194 | |
195 | /*! |
196 | \fn void QLine::translate(int dx, int dy) |
197 | \overload |
198 | |
199 | Translates this line the distance specified by \a dx and \a dy. |
200 | */ |
201 | |
202 | /*! |
203 | \fn QLine QLine::translated(const QPoint &offset) const |
204 | |
205 | \since 4.4 |
206 | |
207 | Returns this line translated by the given \a offset. |
208 | */ |
209 | |
210 | /*! |
211 | \fn QLine QLine::translated(int dx, int dy) const |
212 | \overload |
213 | \since 4.4 |
214 | |
215 | Returns this line translated the distance specified by \a dx and \a dy. |
216 | */ |
217 | |
218 | /*! |
219 | \fn QPoint QLine::center() const |
220 | |
221 | \since 5.8 |
222 | |
223 | Returns the center point of this line. This is equivalent to |
224 | (p1() + p2()) / 2, except it will never overflow. |
225 | */ |
226 | |
227 | /*! |
228 | \fn void QLine::setP1(const QPoint &p1) |
229 | \since 4.4 |
230 | |
231 | Sets the starting point of this line to \a p1. |
232 | |
233 | \sa setP2(), p1() |
234 | */ |
235 | |
236 | |
237 | /*! |
238 | \fn void QLine::setP2(const QPoint &p2) |
239 | \since 4.4 |
240 | |
241 | Sets the end point of this line to \a p2. |
242 | |
243 | \sa setP1(), p2() |
244 | */ |
245 | |
246 | |
247 | /*! |
248 | \fn void QLine::setPoints(const QPoint &p1, const QPoint &p2) |
249 | \since 4.4 |
250 | |
251 | Sets the start point of this line to \a p1 and the end point of this line to \a p2. |
252 | |
253 | \sa setP1(), setP2(), p1(), p2() |
254 | */ |
255 | |
256 | |
257 | /*! |
258 | \fn void QLine::setLine(int x1, int y1, int x2, int y2) |
259 | \since 4.4 |
260 | |
261 | Sets this line to the start in \a x1, \a y1 and end in \a x2, \a y2. |
262 | |
263 | \sa setP1(), setP2(), p1(), p2() |
264 | */ |
265 | |
266 | |
267 | |
268 | #ifndef QT_NO_DEBUG_STREAM |
269 | QDebug operator<<(QDebug dbg, const QLine &p) |
270 | { |
271 | QDebugStateSaver saver(dbg); |
272 | dbg.nospace() << "QLine(" << p.p1() << ',' << p.p2() << ')'; |
273 | return dbg; |
274 | } |
275 | #endif |
276 | |
277 | #ifndef QT_NO_DATASTREAM |
278 | /*! |
279 | \relates QLine |
280 | |
281 | Writes the given \a line to the given \a stream and returns a |
282 | reference to the stream. |
283 | |
284 | \sa {Serializing Qt Data Types} |
285 | */ |
286 | |
287 | QDataStream &operator<<(QDataStream &stream, const QLine &line) |
288 | { |
289 | stream << line.p1() << line.p2(); |
290 | return stream; |
291 | } |
292 | |
293 | /*! |
294 | \relates QLine |
295 | |
296 | Reads a line from the given \a stream into the given \a line and |
297 | returns a reference to the stream. |
298 | |
299 | \sa {Serializing Qt Data Types} |
300 | */ |
301 | |
302 | QDataStream &operator>>(QDataStream &stream, QLine &line) |
303 | { |
304 | QPoint p1, p2; |
305 | stream >> p1; |
306 | stream >> p2; |
307 | line = QLine(p1, p2); |
308 | |
309 | return stream; |
310 | } |
311 | |
312 | #endif // QT_NO_DATASTREAM |
313 | |
314 | |
315 | #ifndef M_2PI |
316 | #define M_2PI 6.28318530717958647692528676655900576 |
317 | #endif |
318 | |
319 | /*! |
320 | \class QLineF |
321 | \inmodule QtCore |
322 | \ingroup painting |
323 | |
324 | \brief The QLineF class provides a two-dimensional vector using |
325 | floating point precision. |
326 | |
327 | A QLineF describes a finite length line (or line segment) on a |
328 | two-dimensional surface. QLineF defines the start and end points |
329 | of the line using floating point accuracy for coordinates. Use |
330 | the toLine() function to retrieve an integer based copy of this |
331 | line. |
332 | |
333 | \table |
334 | \row |
335 | \li \inlineimage qline-point.png |
336 | \li \inlineimage qline-coordinates.png |
337 | \endtable |
338 | |
339 | The positions of the line's start and end points can be retrieved |
340 | using the p1(), x1(), y1(), p2(), x2(), and y2() functions. The |
341 | dx() and dy() functions return the horizontal and vertical |
342 | components of the line, respectively. |
343 | |
344 | The line's length can be retrieved using the length() function, |
345 | and altered using the setLength() function. Similarly, angle() |
346 | and setAngle() are respectively used for retrieving and altering |
347 | the angle of the line. Use the isNull() |
348 | function to determine whether the QLineF represents a valid line |
349 | or a null line. |
350 | |
351 | The intersects() function determines the IntersectionType for this |
352 | line and a given line, while the angleTo() function returns the |
353 | angle between the lines. In addition, the unitVector() function |
354 | returns a line that has the same starting point as this line, but |
355 | with a length of only 1, while the normalVector() function returns |
356 | a line that is perpendicular to this line with the same starting |
357 | point and length. |
358 | |
359 | Finally, the line can be translated a given offset using the |
360 | translate() function, and can be traversed using the pointAt() |
361 | function. |
362 | |
363 | \section1 Constraints |
364 | |
365 | QLine is limited to the minimum and maximum values for the |
366 | \c int type. Operations on a QLine that could potentially result |
367 | in values outside this range will result in undefined behavior. |
368 | |
369 | \sa QLine, QPolygonF, QRectF |
370 | */ |
371 | |
372 | /*! |
373 | \enum QLineF::IntersectType |
374 | \obsolete Use QLineF::IntersectionType instead. |
375 | |
376 | \value NoIntersection |
377 | Lines do not intersect. |
378 | \value UnboundedIntersection |
379 | Lines intersect, but not within the range defined by their lengths. |
380 | \value BoundedIntersection |
381 | Lnes intersect within the range defined by their lengths. |
382 | */ |
383 | |
384 | /*! |
385 | \typealias QLineF::IntersectionType |
386 | |
387 | Describes the intersection between two lines. |
388 | |
389 | \table |
390 | \row |
391 | \li \inlineimage qlinef-unbounded.png |
392 | \li \inlineimage qlinef-bounded.png |
393 | \row |
394 | \li QLineF::UnboundedIntersection |
395 | \li QLineF::BoundedIntersection |
396 | \endtable |
397 | |
398 | \value NoIntersection Indicates that the lines do not intersect; |
399 | i.e. they are parallel. |
400 | |
401 | \value UnboundedIntersection The two lines intersect, but not |
402 | within the range defined by their lengths. This will be the case |
403 | if the lines are not parallel. intersect() will also return this |
404 | value if the intersect point is within the start and end point of |
405 | only one of the lines. |
406 | |
407 | \value BoundedIntersection The two lines intersect with each other |
408 | within the start and end points of each line. |
409 | |
410 | \sa intersect() |
411 | */ |
412 | |
413 | /*! |
414 | \fn QLineF::QLineF() |
415 | |
416 | Constructs a null line. |
417 | */ |
418 | |
419 | /*! |
420 | \fn QLineF::QLineF(const QPointF &p1, const QPointF &p2) |
421 | |
422 | Constructs a line object that represents the line between \a p1 and |
423 | \a p2. |
424 | */ |
425 | |
426 | /*! |
427 | \fn QLineF::QLineF(qreal x1, qreal y1, qreal x2, qreal y2) |
428 | |
429 | Constructs a line object that represents the line between (\a x1, \a y1) and |
430 | (\a x2, \a y2). |
431 | */ |
432 | |
433 | /*! |
434 | \fn QLineF::QLineF(const QLine &line) |
435 | |
436 | Construct a QLineF object from the given integer-based \a line. |
437 | |
438 | \sa toLine() |
439 | */ |
440 | |
441 | /*! |
442 | \fn bool QLineF::isNull() const |
443 | |
444 | Returns \c true if the line does not have distinct start and end points; |
445 | otherwise returns \c false. The start and end points are considered distinct |
446 | if qFuzzyCompare() can distinguish them in at least one coordinate. |
447 | |
448 | \note Due to the use of fuzzy comparison, isNull() may return \c true for |
449 | lines whose length() is not zero. |
450 | |
451 | \sa qFuzzyCompare(), length() |
452 | */ |
453 | |
454 | /*! |
455 | \fn QPointF QLineF::p1() const |
456 | |
457 | Returns the line's start point. |
458 | |
459 | \sa x1(), y1(), p2() |
460 | */ |
461 | |
462 | /*! |
463 | \fn QPointF QLineF::p2() const |
464 | |
465 | Returns the line's end point. |
466 | |
467 | \sa x2(), y2(), p1() |
468 | */ |
469 | |
470 | /*! |
471 | \fn QLine QLineF::toLine() const |
472 | |
473 | Returns an integer based copy of this line. |
474 | |
475 | Note that the returned line's start and end points are rounded to |
476 | the nearest integer. |
477 | |
478 | \sa QLineF() |
479 | */ |
480 | /*! |
481 | \fn qreal QLineF::x1() const |
482 | |
483 | Returns the x-coordinate of the line's start point. |
484 | |
485 | \sa p1() |
486 | */ |
487 | |
488 | /*! |
489 | \fn qreal QLineF::y1() const |
490 | |
491 | Returns the y-coordinate of the line's start point. |
492 | |
493 | \sa p1() |
494 | */ |
495 | |
496 | /*! |
497 | \fn qreal QLineF::x2() const |
498 | |
499 | Returns the x-coordinate of the line's end point. |
500 | |
501 | \sa p2() |
502 | */ |
503 | |
504 | /*! |
505 | \fn qreal QLineF::y2() const |
506 | |
507 | Returns the y-coordinate of the line's end point. |
508 | |
509 | \sa p2() |
510 | */ |
511 | |
512 | /*! |
513 | \fn qreal QLineF::dx() const |
514 | |
515 | Returns the horizontal component of the line's vector. |
516 | |
517 | \sa dy(), pointAt() |
518 | */ |
519 | |
520 | /*! |
521 | \fn qreal QLineF::dy() const |
522 | |
523 | Returns the vertical component of the line's vector. |
524 | |
525 | \sa dx(), pointAt() |
526 | */ |
527 | |
528 | /*! |
529 | \fn void QLineF::setLength(qreal length) |
530 | |
531 | Sets the length of the line to the given \a length. QLineF will |
532 | move the end point - p2() - of the line to give the line its new |
533 | length, unless length() was previously zero, in which case no |
534 | scaling is attempted. For lines with very short lengths |
535 | (represented by denormal floating-point values), results may be |
536 | imprecise. |
537 | |
538 | \sa length(), unitVector() |
539 | */ |
540 | |
541 | /*! |
542 | \fn QLineF QLineF::normalVector() const |
543 | |
544 | Returns a line that is perpendicular to this line with the same starting |
545 | point and length. |
546 | |
547 | \image qlinef-normalvector.png |
548 | |
549 | \sa unitVector() |
550 | */ |
551 | |
552 | /*! |
553 | \fn bool QLineF::operator!=(const QLineF &line) const |
554 | |
555 | Returns \c true if the given \a line is not the same as \e this line. |
556 | |
557 | A line is different from another line if their start or end points |
558 | differ, or the internal order of the points is different. |
559 | */ |
560 | |
561 | /*! |
562 | \fn bool QLineF::operator==(const QLineF &line) const |
563 | |
564 | Returns \c true if the given \a line is the same as this line. |
565 | |
566 | A line is identical to another line if the start and end points |
567 | are identical, and the internal order of the points is the same. |
568 | */ |
569 | |
570 | /*! |
571 | \fn qreal QLineF::pointAt(qreal t) const |
572 | |
573 | Returns the point at the parameterized position specified by \a |
574 | t. The function returns the line's start point if t = 0, and its end |
575 | point if t = 1. |
576 | |
577 | \sa dx(), dy() |
578 | */ |
579 | |
580 | /*! |
581 | Returns the length of the line. |
582 | |
583 | \sa setLength(), isNull() |
584 | */ |
585 | qreal QLineF::length() const |
586 | { |
587 | using std::hypot; |
588 | return hypot(x: dx(), y: dy()); |
589 | } |
590 | |
591 | /*! |
592 | \since 4.4 |
593 | |
594 | Returns the angle of the line in degrees. |
595 | |
596 | The return value will be in the range of values from 0.0 up to but not |
597 | including 360.0. The angles are measured counter-clockwise from a point |
598 | on the x-axis to the right of the origin (x > 0). |
599 | |
600 | \sa setAngle() |
601 | */ |
602 | qreal QLineF::angle() const |
603 | { |
604 | const qreal dx = pt2.x() - pt1.x(); |
605 | const qreal dy = pt2.y() - pt1.y(); |
606 | |
607 | const qreal theta = qAtan2(y: -dy, x: dx) * 360.0 / M_2PI; |
608 | |
609 | const qreal theta_normalized = theta < 0 ? theta + 360 : theta; |
610 | |
611 | if (qFuzzyCompare(p1: theta_normalized, p2: qreal(360))) |
612 | return qreal(0); |
613 | else |
614 | return theta_normalized; |
615 | } |
616 | |
617 | /*! |
618 | \since 4.4 |
619 | |
620 | Sets the angle of the line to the given \a angle (in degrees). |
621 | This will change the position of the second point of the line such that |
622 | the line has the given angle. |
623 | |
624 | Positive values for the angles mean counter-clockwise while negative values |
625 | mean the clockwise direction. Zero degrees is at the 3 o'clock position. |
626 | |
627 | \sa angle() |
628 | */ |
629 | void QLineF::setAngle(qreal angle) |
630 | { |
631 | const qreal angleR = angle * M_2PI / 360.0; |
632 | const qreal l = length(); |
633 | |
634 | const qreal dx = qCos(v: angleR) * l; |
635 | const qreal dy = -qSin(v: angleR) * l; |
636 | |
637 | pt2.rx() = pt1.x() + dx; |
638 | pt2.ry() = pt1.y() + dy; |
639 | } |
640 | |
641 | /*! |
642 | \since 4.4 |
643 | |
644 | Returns a QLineF with the given \a length and \a angle. |
645 | |
646 | The first point of the line will be on the origin. |
647 | |
648 | Positive values for the angles mean counter-clockwise while negative values |
649 | mean the clockwise direction. Zero degrees is at the 3 o'clock position. |
650 | */ |
651 | QLineF QLineF::fromPolar(qreal length, qreal angle) |
652 | { |
653 | const qreal angleR = angle * M_2PI / 360.0; |
654 | return QLineF(0, 0, qCos(v: angleR) * length, -qSin(v: angleR) * length); |
655 | } |
656 | |
657 | /*! |
658 | Returns the unit vector for this line, i.e a line starting at the |
659 | same point as \e this line with a length of 1.0, provided the line |
660 | is non-null. |
661 | |
662 | \sa normalVector(), setLength() |
663 | */ |
664 | QLineF QLineF::unitVector() const |
665 | { |
666 | qreal x = dx(); |
667 | qreal y = dy(); |
668 | using std::hypot; |
669 | qreal len = hypot(x: x, y: y); |
670 | |
671 | QLineF f(p1(), QPointF(pt1.x() + x/len, pt1.y() + y/len)); |
672 | |
673 | #ifndef QT_NO_DEBUG |
674 | if (qAbs(t: f.length() - 1) >= 0.001) |
675 | qWarning(msg: "QLine::unitVector: New line does not have unit length" ); |
676 | #endif |
677 | |
678 | return f; |
679 | } |
680 | |
681 | #if QT_DEPRECATED_SINCE(5, 14) |
682 | /*! |
683 | \fn QLineF::IntersectType QLineF::intersect(const QLineF &line, QPointF *intersectionPoint) const |
684 | \obsolete Use intersects() instead |
685 | |
686 | Returns a value indicating whether or not \e this line intersects |
687 | with the given \a line. |
688 | |
689 | The actual intersection point is extracted to \a intersectionPoint |
690 | (if the pointer is valid). If the lines are parallel, the |
691 | intersection point is undefined. |
692 | */ |
693 | |
694 | QLineF::IntersectType QLineF::intersect(const QLineF &l, QPointF *intersectionPoint) const |
695 | { |
696 | return intersects(l, intersectionPoint); |
697 | } |
698 | #endif |
699 | |
700 | /*! |
701 | \fn QLineF::IntersectionType QLineF::intersects(const QLineF &line, QPointF *intersectionPoint) const |
702 | \since 5.14 |
703 | |
704 | Returns a value indicating whether or not \e this line intersects |
705 | with the given \a line. |
706 | |
707 | The actual intersection point is extracted to \a intersectionPoint |
708 | (if the pointer is valid). If the lines are parallel, the |
709 | intersection point is undefined. |
710 | */ |
711 | QLineF::IntersectionType QLineF::intersects(const QLineF &l, QPointF *intersectionPoint) const |
712 | { |
713 | // ipmlementation is based on Graphics Gems III's "Faster Line Segment Intersection" |
714 | const QPointF a = pt2 - pt1; |
715 | const QPointF b = l.pt1 - l.pt2; |
716 | const QPointF c = pt1 - l.pt1; |
717 | |
718 | const qreal denominator = a.y() * b.x() - a.x() * b.y(); |
719 | if (denominator == 0 || !qt_is_finite(d: denominator)) |
720 | return NoIntersection; |
721 | |
722 | const qreal reciprocal = 1 / denominator; |
723 | const qreal na = (b.y() * c.x() - b.x() * c.y()) * reciprocal; |
724 | if (intersectionPoint) |
725 | *intersectionPoint = pt1 + a * na; |
726 | |
727 | if (na < 0 || na > 1) |
728 | return UnboundedIntersection; |
729 | |
730 | const qreal nb = (a.x() * c.y() - a.y() * c.x()) * reciprocal; |
731 | if (nb < 0 || nb > 1) |
732 | return UnboundedIntersection; |
733 | |
734 | return BoundedIntersection; |
735 | } |
736 | |
737 | /*! |
738 | \fn void QLineF::translate(const QPointF &offset) |
739 | |
740 | Translates this line by the given \a offset. |
741 | */ |
742 | |
743 | /*! |
744 | \fn void QLineF::translate(qreal dx, qreal dy) |
745 | \overload |
746 | |
747 | Translates this line the distance specified by \a dx and \a dy. |
748 | */ |
749 | |
750 | /*! |
751 | \fn QLineF QLineF::translated(const QPointF &offset) const |
752 | |
753 | \since 4.4 |
754 | |
755 | Returns this line translated by the given \a offset. |
756 | */ |
757 | |
758 | /*! |
759 | \fn QLineF QLineF::translated(qreal dx, qreal dy) const |
760 | \overload |
761 | \since 4.4 |
762 | |
763 | Returns this line translated the distance specified by \a dx and \a dy. |
764 | */ |
765 | |
766 | /*! |
767 | \fn QPointF QLineF::center() const |
768 | |
769 | \since 5.8 |
770 | |
771 | Returns the center point of this line. This is equivalent to |
772 | 0.5 * p1() + 0.5 * p2(). |
773 | */ |
774 | |
775 | /*! |
776 | \fn void QLineF::setP1(const QPointF &p1) |
777 | \since 4.4 |
778 | |
779 | Sets the starting point of this line to \a p1. |
780 | |
781 | \sa setP2(), p1() |
782 | */ |
783 | |
784 | |
785 | /*! |
786 | \fn void QLineF::setP2(const QPointF &p2) |
787 | \since 4.4 |
788 | |
789 | Sets the end point of this line to \a p2. |
790 | |
791 | \sa setP1(), p2() |
792 | */ |
793 | |
794 | |
795 | /*! |
796 | \fn void QLineF::setPoints(const QPointF &p1, const QPointF &p2) |
797 | \since 4.4 |
798 | |
799 | Sets the start point of this line to \a p1 and the end point of this line to \a p2. |
800 | |
801 | \sa setP1(), setP2(), p1(), p2() |
802 | */ |
803 | |
804 | |
805 | /*! |
806 | \fn void QLineF::setLine(qreal x1, qreal y1, qreal x2, qreal y2) |
807 | \since 4.4 |
808 | |
809 | Sets this line to the start in \a x1, \a y1 and end in \a x2, \a y2. |
810 | |
811 | \sa setP1(), setP2(), p1(), p2() |
812 | */ |
813 | |
814 | /*! |
815 | \fn qreal QLineF::angleTo(const QLineF &line) const |
816 | |
817 | \since 4.4 |
818 | |
819 | Returns the angle (in degrees) from this line to the given \a |
820 | line, taking the direction of the lines into account. If the lines |
821 | do not intersect within their range, it is the intersection point of |
822 | the extended lines that serves as origin (see |
823 | QLineF::UnboundedIntersection). |
824 | |
825 | The returned value represents the number of degrees you need to add |
826 | to this line to make it have the same angle as the given \a line, |
827 | going counter-clockwise. |
828 | |
829 | \sa intersect() |
830 | */ |
831 | qreal QLineF::angleTo(const QLineF &l) const |
832 | { |
833 | if (isNull() || l.isNull()) |
834 | return 0; |
835 | |
836 | const qreal a1 = angle(); |
837 | const qreal a2 = l.angle(); |
838 | |
839 | const qreal delta = a2 - a1; |
840 | const qreal delta_normalized = delta < 0 ? delta + 360 : delta; |
841 | |
842 | if (qFuzzyCompare(p1: delta, p2: qreal(360))) |
843 | return 0; |
844 | else |
845 | return delta_normalized; |
846 | } |
847 | |
848 | #if QT_DEPRECATED_SINCE(5, 14) |
849 | /*! |
850 | \fn qreal QLineF::angle(const QLineF &line) const |
851 | |
852 | \obsolete |
853 | |
854 | Returns the angle (in degrees) between this line and the given \a |
855 | line, taking the direction of the lines into account. If the lines |
856 | do not intersect within their range, it is the intersection point of |
857 | the extended lines that serves as origin (see |
858 | QLineF::UnboundedIntersection). |
859 | |
860 | \table |
861 | \row |
862 | \li \inlineimage qlinef-angle-identicaldirection.png |
863 | \li \inlineimage qlinef-angle-oppositedirection.png |
864 | \endtable |
865 | |
866 | When the lines are parallel, this function returns 0 if they have |
867 | the same direction; otherwise it returns 180. |
868 | |
869 | \sa intersect() |
870 | */ |
871 | qreal QLineF::angle(const QLineF &l) const |
872 | { |
873 | if (isNull() || l.isNull()) |
874 | return 0; |
875 | qreal cos_line = (dx()*l.dx() + dy()*l.dy()) / (length()*l.length()); |
876 | qreal rad = 0; |
877 | // only accept cos_line in the range [-1,1], if it is outside, use 0 (we return 0 rather than PI for those cases) |
878 | if (cos_line >= -1.0 && cos_line <= 1.0) rad = qAcos( v: cos_line ); |
879 | return rad * 360 / M_2PI; |
880 | } |
881 | #endif |
882 | |
883 | |
884 | #ifndef QT_NO_DEBUG_STREAM |
885 | QDebug operator<<(QDebug dbg, const QLineF &p) |
886 | { |
887 | QDebugStateSaver saver(dbg); |
888 | dbg.nospace() << "QLineF(" << p.p1() << ',' << p.p2() << ')'; |
889 | return dbg; |
890 | } |
891 | #endif |
892 | |
893 | #ifndef QT_NO_DATASTREAM |
894 | /*! |
895 | \relates QLineF |
896 | |
897 | Writes the given \a line to the given \a stream and returns a |
898 | reference to the stream. |
899 | |
900 | \sa {Serializing Qt Data Types} |
901 | */ |
902 | |
903 | QDataStream &operator<<(QDataStream &stream, const QLineF &line) |
904 | { |
905 | stream << line.p1() << line.p2(); |
906 | return stream; |
907 | } |
908 | |
909 | /*! |
910 | \relates QLineF |
911 | |
912 | Reads a line from the given \a stream into the given \a line and |
913 | returns a reference to the stream. |
914 | |
915 | \sa {Serializing Qt Data Types} |
916 | */ |
917 | |
918 | QDataStream &operator>>(QDataStream &stream, QLineF &line) |
919 | { |
920 | QPointF start, end; |
921 | stream >> start; |
922 | stream >> end; |
923 | line = QLineF(start, end); |
924 | |
925 | return stream; |
926 | } |
927 | |
928 | #endif // QT_NO_DATASTREAM |
929 | |
930 | QT_END_NAMESPACE |
931 | |