Qt 6.x
The Qt SDK
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
241#ifndef QT_NO_DEBUG_STREAM
243{
244 QDebugStateSaver saver(dbg);
245 dbg.nospace() << "QLine(" << p.p1() << ',' << p.p2() << ')';
246 return dbg;
247}
248#endif
249
250#ifndef QT_NO_DATASTREAM
261{
262 stream << line.p1() << line.p2();
263 return stream;
264}
265
276{
277 QPoint p1, p2;
278 stream >> p1;
279 stream >> p2;
280 line = QLine(p1, p2);
281
282 return stream;
283}
284
285#endif // QT_NO_DATASTREAM
286
543{
544 return qHypot(dx(), dy());
545}
546
559{
560 const qreal dx = pt2.x() - pt1.x();
561 const qreal dy = pt2.y() - pt1.y();
562
563 const qreal theta = qRadiansToDegrees(qAtan2(-dy, dx));
564
565 const qreal theta_normalized = theta < 0 ? theta + 360 : theta;
566
567 if (qFuzzyCompare(theta_normalized, qreal(360)))
568 return qreal(0);
569 else
570 return theta_normalized;
571}
572
586{
587 const qreal angleR = qDegreesToRadians(angle);
588 const qreal l = length();
589
590 const qreal dx = qCos(angleR) * l;
591 const qreal dy = -qSin(angleR) * l;
592
593 pt2.rx() = pt1.x() + dx;
594 pt2.ry() = pt1.y() + dy;
595}
596
608{
609 const qreal angleR = qDegreesToRadians(angle);
610 return QLineF(0, 0, qCos(angleR) * length, -qSin(angleR) * length);
611}
612
621{
622 const qreal x = dx();
623 const qreal y = dy();
624
625 const qreal len = qHypot(x, y);
626 QLineF f(p1(), QPointF(pt1.x() + x / len, pt1.y() + y / len));
627
628#ifndef QT_NO_DEBUG
629 if (qAbs(f.length() - 1) >= 0.001)
630 qWarning("QLine::unitVector: New line does not have unit length");
631#endif
632
633 return f;
634}
635
648{
649 // ipmlementation is based on Graphics Gems III's "Faster Line Segment Intersection"
650 const QPointF a = pt2 - pt1;
651 const QPointF b = l.pt1 - l.pt2;
652 const QPointF c = pt1 - l.pt1;
653
654 const qreal denominator = a.y() * b.x() - a.x() * b.y();
655 if (denominator == 0 || !qt_is_finite(denominator))
656 return NoIntersection;
657
658 const qreal reciprocal = 1 / denominator;
659 const qreal na = (b.y() * c.x() - b.x() * c.y()) * reciprocal;
660 if (intersectionPoint)
661 *intersectionPoint = pt1 + a * na;
662
663 if (na < 0 || na > 1)
665
666 const qreal nb = (a.x() * c.y() - a.y() * c.x()) * reciprocal;
667 if (nb < 0 || nb > 1)
669
670 return BoundedIntersection;
671}
672
768{
769 if (isNull() || l.isNull())
770 return 0;
771
772 const qreal a1 = angle();
773 const qreal a2 = l.angle();
774
775 const qreal delta = a2 - a1;
776 const qreal delta_normalized = delta < 0 ? delta + 360 : delta;
777
778 if (qFuzzyCompare(delta, qreal(360)))
779 return 0;
780 else
781 return delta_normalized;
782}
783
784#ifndef QT_NO_DEBUG_STREAM
786{
787 QDebugStateSaver saver(dbg);
788 dbg.nospace() << "QLineF(" << p.p1() << ',' << p.p2() << ')';
789 return dbg;
790}
791#endif
792
793#ifndef QT_NO_DATASTREAM
804{
805 stream << line.p1() << line.p2();
806 return stream;
807}
808
819{
821 stream >> start;
822 stream >> end;
823 line = QLineF(start, end);
824
825 return stream;
826}
827
828#endif // QT_NO_DATASTREAM
829
\inmodule QtCore\reentrant
Definition qdatastream.h:30
\inmodule QtCore
\inmodule QtCore
\inmodule QtCore
Definition qline.h:182
constexpr QPointF p1() const
Returns the line's start point.
Definition qline.h:289
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:818
constexpr bool isNull() const
Returns true if the line does not have distinct start and end points; otherwise returns false.
Definition qline.h:284
void setAngle(qreal angle)
Definition qline.cpp:585
constexpr qreal dx() const
Returns the horizontal component of the line's vector.
Definition qline.h:299
constexpr qreal dy() const
Returns the vertical component of the line's vector.
Definition qline.h:304
constexpr QLineF()
Constructs a null line.
Definition qline.h:250
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:803
qreal angle() const
Definition qline.cpp:558
qreal angleTo(const QLineF &l) const
Definition qline.cpp:767
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:620
qreal length() const
Returns the length of the line.
Definition qline.cpp:542
IntersectionType
\typealias QLineF::IntersectType
Definition qline.h:185
@ NoIntersection
Definition qline.h:185
@ BoundedIntersection
Definition qline.h:185
@ UnboundedIntersection
Definition qline.h:185
IntersectionType intersects(const QLineF &l, QPointF *intersectionPoint=nullptr) const
Definition qline.cpp:647
static QLineF fromPolar(qreal length, qreal angle)
Definition qline.cpp:607
\inmodule QtCore
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:260
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:275
\inmodule QtCore\reentrant
Definition qpoint.h:214
constexpr qreal & ry() noexcept
Returns a reference to the y coordinate of this point.
Definition qpoint.h:358
constexpr qreal x() const noexcept
Returns the x coordinate of this point.
Definition qpoint.h:333
constexpr qreal y() const noexcept
Returns the y coordinate of this point.
Definition qpoint.h:338
constexpr qreal & rx() noexcept
Returns a reference to the x coordinate of this point.
Definition qpoint.h:353
\inmodule QtCore\reentrant
Definition qpoint.h:23
QPixmap p2
QPixmap p1
[0]
Combined button and popup list for selecting options.
EGLStreamKHR stream
bool qFuzzyCompare(qfloat16 p1, qfloat16 p2) noexcept
Definition qfloat16.h:287
auto qHypot(qfloat16 x, qfloat16 y)
Definition qfloat16.h:397
QDebug operator<<(QDebug dbg, const QLine &p)
Definition qline.cpp:242
#define qWarning
Definition qlogging.h:162
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:111
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
GLenum GLsizei len
GLfloat GLfloat p
[1]
#define a2
#define a1
double qreal
Definition qtypes.h:92