Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qdoublevector2d.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 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
6#include <QtCore/qdatastream.h>
7#include <QtCore/qdebug.h>
8#include <QtCore/qmath.h>
9
11
12QDoubleVector2D::QDoubleVector2D(const QDoubleVector3D &vector) :
13 xp(vector.xp), yp(vector.yp)
14{
15}
16
17double QDoubleVector2D::length() const
18{
19 return qSqrt(xp * xp + yp * yp);
20}
21
22QDoubleVector2D QDoubleVector2D::normalized() const
23{
24 // Need some extra precision if the length is very small.
25 double len = double(xp) * double(xp) +
26 double(yp) * double(yp);
27 if (qFuzzyIsNull(len - 1.0))
28 return *this;
29 else if (!qFuzzyIsNull(len))
30 return *this / (double)qSqrt(len);
31 else
32 return QDoubleVector2D();
33}
34
35void QDoubleVector2D::normalize()
36{
37 // Need some extra precision if the length is very small.
38 double len = double(xp) * double(xp) +
39 double(yp) * double(yp);
40 if (qFuzzyIsNull(len - 1.0) || qFuzzyIsNull(len))
41 return;
42
43 len = qSqrt(len);
44
45 xp /= len;
46 yp /= len;
47}
48
49QDoubleVector3D QDoubleVector2D::toVector3D() const
50{
51 return QDoubleVector3D(xp, yp, 0.0);
52}
53
54#ifndef QT_NO_DEBUG_STREAM
55
56QDebug operator<<(QDebug dbg, const QDoubleVector2D &vector)
57{
58 QDebugStateSaver saver(dbg);
59 dbg.nospace() << "QDoubleVector2D(" << vector.x() << ", " << vector.y() << ')';
60 return dbg;
61}
62
63#endif
64
65#ifndef QT_NO_DATASTREAM
66
67QDataStream &operator<<(QDataStream &stream, const QDoubleVector2D &vector)
68{
69 stream << double(vector.x()) << double(vector.y());
70 return stream;
71}
72
73QDataStream &operator>>(QDataStream &stream, QDoubleVector2D &vector)
74{
75 double x, y;
76 stream >> x;
77 stream >> y;
78 vector.setX(double(x));
79 vector.setY(double(y));
80 return stream;
81}
82
83#endif // QT_NO_DATASTREAM
84
85QT_END_NAMESPACE
Combined button and popup list for selecting options.
QDebug operator<<(QDebug dbg, const QFileInfo &fi)
QDataStream & operator<<(QDataStream &out, const MyClass &myObj)
[4]
QDataStream & operator>>(QDataStream &in, MyClass &myObj)