Add static dotProduct methods to the QPoint(F) classes

Change-Id: I66ac9433b74341a83569a60038ea2f7a025e81b1
Reviewed-by: Gunnar Sletta <gunnar.sletta@digia.com>
This commit is contained in:
Laszlo Papp 2012-08-02 07:18:35 +01:00 committed by The Qt Project
parent 5f481c98de
commit e335fb7254
5 changed files with 95 additions and 0 deletions

View File

@ -79,6 +79,13 @@ p *= 2.5; // p becomes (-3, 10)
//! [5]
//! [16]
QPoint p( 3, 7);
QPoint q(-1, 4);
int lengthSquared = QPoint::dotProduct(p, q); // lengthSquared becomes 25
//! [16]
//! [6]
QPoint p(-3, 10);
p /= 2.5; // p becomes (-1, 4)
@ -147,3 +154,10 @@ p *= 2.5; // p becomes (-2.75, 10.25)
QPointF p(-2.75, 10.25);
p /= 2.5; // p becomes (-1.1, 4.1)
//! [15]
//! [17]
QPointF p( 3.1, 7.1);
QPointF q(-1.0, 4.1);
int lengthSquared = QPointF::dotProduct(p, q); // lengthSquared becomes 26.01
//! [17]

View File

@ -222,6 +222,15 @@ QT_BEGIN_NAMESPACE
\sa operator/=()
*/
/*!
\fn static int QPoint::dotProduct(const QPoint &p1, const QPoint &p2)
\since 5.1
\snippet code/src_corelib_tools_qpoint.cpp 16
Returns the dot product of \a p1 and \a p2.
*/
/*!
\fn bool operator==(const QPoint &p1, const QPoint &p2)
\relates QPoint
@ -711,6 +720,15 @@ QDebug operator<<(QDebug d, const QPointF &p)
\sa QPointF()
*/
/*!
\fn static qreal QPointF::dotProduct(const QPointF &p1, const QPointF &p2)
\since 5.1
\snippet code/src_corelib_tools_qpoint.cpp 17
Returns the dot product of \a p1 and \a p2.
*/
/*!
\fn bool operator==(const QPointF &p1, const QPointF &p2)
\relates QPointF

View File

@ -76,6 +76,8 @@ public:
inline QPoint &operator/=(qreal divisor);
Q_DECL_CONSTEXPR static inline int dotProduct(const QPoint &p1, const QPoint &p2);
friend Q_DECL_CONSTEXPR inline bool operator==(const QPoint &, const QPoint &);
friend Q_DECL_CONSTEXPR inline bool operator!=(const QPoint &, const QPoint &);
friend Q_DECL_CONSTEXPR inline const QPoint operator+(const QPoint &, const QPoint &);
@ -153,6 +155,9 @@ inline QPoint &QPoint::operator*=(double factor)
inline QPoint &QPoint::operator*=(int factor)
{ xp = xp*factor; yp = yp*factor; return *this; }
Q_DECL_CONSTEXPR inline int QPoint::dotProduct(const QPoint &p1, const QPoint &p2)
{ return p1.xp * p2.xp + p1.yp * p2.yp; }
Q_DECL_CONSTEXPR inline bool operator==(const QPoint &p1, const QPoint &p2)
{ return p1.xp == p2.xp && p1.yp == p2.yp; }
@ -233,6 +238,8 @@ public:
inline QPointF &operator*=(qreal c);
inline QPointF &operator/=(qreal c);
Q_DECL_CONSTEXPR static inline qreal dotProduct(const QPointF &p1, const QPointF &p2);
friend Q_DECL_CONSTEXPR inline bool operator==(const QPointF &, const QPointF &);
friend Q_DECL_CONSTEXPR inline bool operator!=(const QPointF &, const QPointF &);
friend Q_DECL_CONSTEXPR inline const QPointF operator+(const QPointF &, const QPointF &);
@ -330,6 +337,11 @@ inline QPointF &QPointF::operator*=(qreal c)
xp*=c; yp*=c; return *this;
}
Q_DECL_CONSTEXPR inline qreal QPointF::dotProduct(const QPointF &p1, const QPointF &p2)
{
return p1.xp * p2.xp + p1.yp * p2.yp;
}
Q_DECL_CONSTEXPR inline bool operator==(const QPointF &p1, const QPointF &p2)
{
return qFuzzyIsNull(p1.xp - p2.xp) && qFuzzyIsNull(p1.yp - p2.yp);

View File

@ -70,6 +70,9 @@ private slots:
void operator_divide_data();
void operator_divide();
void dotProduct_data();
void dotProduct();
void operator_unary_plus_data();
void operator_unary_plus();
@ -271,6 +274,28 @@ void tst_QPoint::operator_divide()
QCOMPARE(point, expected);
}
void tst_QPoint::dotProduct_data()
{
QTest::addColumn<QPoint>("point1");
QTest::addColumn<QPoint>("point2");
QTest::addColumn<int>("expected");
QTest::newRow("(0, 0) dot (0, 0)") << QPoint(0, 0) << QPoint(0, 0)<< 0;
QTest::newRow("(10, 0) dot (0, 10)") << QPoint(10, 0) << QPoint(0, 10) << 0;
QTest::newRow("(0, 10) dot (10, 0)") << QPoint(0, 10) << QPoint(10, 0) << 0;
QTest::newRow("(10, 20) dot (-10, -20)") << QPoint(10, 20) << QPoint(-10, -20) << -500;
QTest::newRow("(-10, -20) dot (10, 20)") << QPoint(-10, -20) << QPoint(10, 20) << -500;
}
void tst_QPoint::dotProduct()
{
QFETCH(QPoint, point1);
QFETCH(QPoint, point2);
QFETCH(int, expected);
QCOMPARE(QPoint::dotProduct(point1, point2), expected);
}
void tst_QPoint::operator_unary_plus_data()
{
operator_unary_minus_data();

View File

@ -75,6 +75,9 @@ private slots:
void operator_divide();
void division();
void dotProduct_data();
void dotProduct();
void operator_unary_plus_data();
void operator_unary_plus();
@ -290,6 +293,29 @@ void tst_QPointF::division()
}
}
void tst_QPointF::dotProduct_data()
{
QTest::addColumn<QPointF>("point1");
QTest::addColumn<QPointF>("point2");
QTest::addColumn<qreal>("expected");
QTest::newRow("(0, 0) dot (0, 0)") << QPointF(0, 0) << QPointF(0, 0) << qreal(0);
QTest::newRow("(10, 0) dot (0, 10)") << QPointF(10, 0) << QPointF(0, 10)<< qreal(0);
QTest::newRow("(0, 10) dot (10, 0)") << QPointF(0, 10) << QPointF(10, 0) << qreal(0);
QTest::newRow("(10, 20) dot (-10, -20)") << QPointF(10, 20) << QPointF(-10, -20) << qreal(-500);
QTest::newRow("(10.1, 20.2) dot (-10.1, -20.2)") << QPointF(10.1, 20.2) << QPointF(-10.1, -20.2) << qreal(-510.05);
QTest::newRow("(-10.1, -20.2) dot (10.1, 20.2)") << QPointF(-10.1, -20.2) << QPointF(10.1, 20.2) << qreal(-510.05);
}
void tst_QPointF::dotProduct()
{
QFETCH(QPointF, point1);
QFETCH(QPointF, point2);
QFETCH(qreal, expected);
QCOMPARE(QPointF::dotProduct(point1, point2), expected);
}
void tst_QPointF::operator_unary_plus_data()
{
operator_unary_minus_data();