Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qline.cpp
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qline.h"
5
6#include "qdebug.h"
7#include "qdatastream.h"
8#include "qmath.h"
9#include <private/qnumeric_p.h>
10
12
244#ifndef QT_NO_DEBUG_STREAM
246{
247 QDebugStateSaver saver(dbg);
248 dbg.nospace() << "QLine(" << p.p1() << ',' << p.p2() << ')';
249 return dbg;
250}
251#endif
252
253#ifndef QT_NO_DATASTREAM
264{
265 stream << line.p1() << line.p2();
266 return stream;
267}
268
279{
280 QPoint p1, p2;
281 stream >> p1;
282 stream >> p2;
283 line = QLine(p1, p2);
284
285 return stream;
286}
287
288#endif // QT_NO_DATASTREAM
289
549{
550 return qHypot(dx(), dy());
551}
552
565{
566 const qreal dx = pt2.x() - pt1.x();
567 const qreal dy = pt2.y() - pt1.y();
568
569 const qreal theta = qRadiansToDegrees(qAtan2(-dy, dx));
570
571 const qreal theta_normalized = theta < 0 ? theta + 360 : theta;
572
573 if (qFuzzyCompare(theta_normalized, qreal(360)))
574 return qreal(0);
575 else
576 return theta_normalized;
577}
578
592{
593 const qreal angleR = qDegreesToRadians(angle);
594 const qreal l = length();
595
596 const qreal dx = qCos(angleR) * l;
597 const qreal dy = -qSin(angleR) * l;
598
599 pt2.rx() = pt1.x() + dx;
600 pt2.ry() = pt1.y() + dy;
601}
602
614{
615 const qreal angleR = qDegreesToRadians(angle);
616 return QLineF(0, 0, qCos(angleR) * length, -qSin(angleR) * length);
617}
618
627{
628 const qreal x = dx();
629 const qreal y = dy();
630
631 const qreal len = qHypot(x, y);
632 QLineF f(p1(), QPointF(pt1.x() + x / len, pt1.y() + y / len));
633
634#ifndef QT_NO_DEBUG
635 if (qAbs(f.length() - 1) >= 0.001)
636 qWarning("QLine::unitVector: New line does not have unit length");
637#endif
638
639 return f;
640}
641
654{
655 // ipmlementation is based on Graphics Gems III's "Faster Line Segment Intersection"
656 const QPointF a = pt2 - pt1;
657 const QPointF b = l.pt1 - l.pt2;
658 const QPointF c = pt1 - l.pt1;
659
660 const qreal denominator = a.y() * b.x() - a.x() * b.y();
661 if (denominator == 0 || !qt_is_finite(denominator))
662 return NoIntersection;
663
664 const qreal reciprocal = 1 / denominator;
665 const qreal na = (b.y() * c.x() - b.x() * c.y()) * reciprocal;
666 if (intersectionPoint)
667 *intersectionPoint = pt1 + a * na;
668
669 if (na < 0 || na > 1)
671
672 const qreal nb = (a.x() * c.y() - a.y() * c.x()) * reciprocal;
673 if (nb < 0 || nb > 1)
675
676 return BoundedIntersection;
677}
678
774{
775 if (isNull() || l.isNull())
776 return 0;
777
778 const qreal a1 = angle();
779 const qreal a2 = l.angle();
780
781 const qreal delta = a2 - a1;
782 const qreal delta_normalized = delta < 0 ? delta + 360 : delta;
783
784 if (qFuzzyCompare(delta, qreal(360)))
785 return 0;
786 else
787 return delta_normalized;
788}
789
809#ifndef QT_NO_DEBUG_STREAM
811{
812 QDebugStateSaver saver(dbg);
813 dbg.nospace() << "QLineF(" << p.p1() << ',' << p.p2() << ')';
814 return dbg;
815}
816#endif
817
818#ifndef QT_NO_DATASTREAM
829{
830 stream << line.p1() << line.p2();
831 return stream;
832}
833
844{
846 stream >> start;
847 stream >> end;
848 line = QLineF(start, end);
849
850 return stream;
851}
852
853#endif // QT_NO_DATASTREAM
854
\inmodule QtCore\reentrant
Definition qdatastream.h:46
\inmodule QtCore
\inmodule QtCore
\inmodule QtCore\compares equality \compareswith equality QLine \endcompareswith
Definition qline.h:192
constexpr QPointF p1() const
Returns the line's start point.
Definition qline.h:317
QDataStream & operator>>(QDataStream &stream, QLineF &line)
Reads a line from the given stream into the given line and returns a reference to the stream.
Definition qline.cpp:843
constexpr bool isNull() const
Returns true if the line does not have distinct start and end points; otherwise returns false.
Definition qline.h:312
void setAngle(qreal angle)
Definition qline.cpp:591
friend constexpr bool qFuzzyCompare(const QLineF &lhs, const QLineF &rhs) noexcept
Definition qline.h:264
constexpr qreal dx() const
Returns the horizontal component of the line's vector.
Definition qline.h:327
constexpr qreal dy() const
Returns the vertical component of the line's vector.
Definition qline.h:332
constexpr QLineF()
Constructs a null line.
Definition qline.h:278
QDataStream & operator<<(QDataStream &stream, const QLineF &line)
Writes the given line to the given stream and returns a reference to the stream.
Definition qline.cpp:828
qreal angle() const
Definition qline.cpp:564
qreal angleTo(const QLineF &l) const
Definition qline.cpp:773
QLineF unitVector() const
Returns the unit vector for this line, i.e a line starting at the same point as this line with a leng...
Definition qline.cpp:626
qreal length() const
Returns the length of the line.
Definition qline.cpp:548
IntersectionType
\typealias QLineF::IntersectType
Definition qline.h:195
@ NoIntersection
Definition qline.h:195
@ BoundedIntersection
Definition qline.h:195
@ UnboundedIntersection
Definition qline.h:195
IntersectionType intersects(const QLineF &l, QPointF *intersectionPoint=nullptr) const
Definition qline.cpp:653
static QLineF fromPolar(qreal length, qreal angle)
Definition qline.cpp:613
\inmodule QtCore\compares equality \compareswith equality QLineF \endcompareswith
Definition qline.h:18
QDataStream & operator<<(QDataStream &stream, const QLine &line)
Writes the given line to the given stream and returns a reference to the stream.
Definition qline.cpp:263
QDataStream & operator>>(QDataStream &stream, QLine &line)
Reads a line from the given stream into the given line and returns a reference to the stream.
Definition qline.cpp:278
\inmodule QtCore\reentrant
Definition qpoint.h:217
constexpr qreal & ry() noexcept
Returns a reference to the y coordinate of this point.
Definition qpoint.h:368
constexpr qreal x() const noexcept
Returns the x coordinate of this point.
Definition qpoint.h:343
constexpr qreal y() const noexcept
Returns the y coordinate of this point.
Definition qpoint.h:348
constexpr qreal & rx() noexcept
Returns a reference to the x coordinate of this point.
Definition qpoint.h:363
\inmodule QtCore\reentrant
Definition qpoint.h:25
QPixmap p2
QPixmap p1
[0]
Combined button and popup list for selecting options.
EGLStreamKHR stream
auto qHypot(qfloat16 x, qfloat16 y)
Definition qfloat16.h:443
QDebug operator<<(QDebug dbg, const QLine &p)
Definition qline.cpp:245
#define qWarning
Definition qlogging.h:166
constexpr float qRadiansToDegrees(float radians)
Definition qmath.h:281
auto qAtan2(T1 y, T2 x)
Definition qmath.h:90
auto qCos(T v)
Definition qmath.h:60
auto qSin(T v)
Definition qmath.h:54
constexpr float qDegreesToRadians(float degrees)
Definition qmath.h:260
constexpr T qAbs(const T &t)
Definition qnumeric.h:328
static Q_DECL_CONST_FUNCTION bool qt_is_finite(double d)
Definition qnumeric_p.h:117
GLboolean GLboolean GLboolean b
GLint GLint GLint GLint GLint x
[0]
GLboolean GLboolean GLboolean GLboolean a
[7]
GLuint GLuint end
GLenum GLuint GLenum GLsizei length
GLfloat GLfloat f
GLfloat angle
GLuint start
GLint y
const GLubyte * c
GLfloat GLfloat p
[1]
GLenum GLsizei len
#define a2
#define a1
double qreal
Definition qtypes.h:187