src/corelib/tools/qline.h
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtCore module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #ifndef QLINE_H
       
    43 #define QLINE_H
       
    44 
       
    45 #include <QtCore/qpoint.h>
       
    46 
       
    47 QT_BEGIN_HEADER
       
    48 
       
    49 QT_BEGIN_NAMESPACE
       
    50 
       
    51 QT_MODULE(Core)
       
    52 
       
    53 /*******************************************************************************
       
    54  * class QLine
       
    55  *******************************************************************************/
       
    56 
       
    57 class Q_CORE_EXPORT QLine
       
    58 {
       
    59 public:
       
    60     inline QLine();
       
    61     inline QLine(const QPoint &pt1, const QPoint &pt2);
       
    62     inline QLine(int x1, int y1, int x2, int y2);
       
    63 
       
    64     inline bool isNull() const;
       
    65 
       
    66     inline QPoint p1() const;
       
    67     inline QPoint p2() const;
       
    68 
       
    69     inline int x1() const;
       
    70     inline int y1() const;
       
    71 
       
    72     inline int x2() const;
       
    73     inline int y2() const;
       
    74 
       
    75     inline int dx() const;
       
    76     inline int dy() const;
       
    77 
       
    78     inline void translate(const QPoint &p);
       
    79     inline void translate(int dx, int dy);
       
    80 
       
    81     inline QLine translated(const QPoint &p) const;
       
    82     inline QLine translated(int dx, int dy) const;
       
    83 
       
    84     inline void setP1(const QPoint &p1);
       
    85     inline void setP2(const QPoint &p2);
       
    86     inline void setPoints(const QPoint &p1, const QPoint &p2);
       
    87     inline void setLine(int x1, int y1, int x2, int y2);
       
    88 
       
    89     inline bool operator==(const QLine &d) const;
       
    90     inline bool operator!=(const QLine &d) const { return !(*this == d); }
       
    91 
       
    92 private:
       
    93     QPoint pt1, pt2;
       
    94 };
       
    95 Q_DECLARE_TYPEINFO(QLine, Q_MOVABLE_TYPE);
       
    96 
       
    97 /*******************************************************************************
       
    98  * class QLine inline members
       
    99  *******************************************************************************/
       
   100 
       
   101 inline QLine::QLine() { }
       
   102 
       
   103 inline QLine::QLine(const QPoint &pt1_, const QPoint &pt2_) : pt1(pt1_), pt2(pt2_) { }
       
   104 
       
   105 inline QLine::QLine(int x1pos, int y1pos, int x2pos, int y2pos) : pt1(QPoint(x1pos, y1pos)), pt2(QPoint(x2pos, y2pos)) { }
       
   106 
       
   107 inline bool QLine::isNull() const
       
   108 {
       
   109     return pt1 == pt2;
       
   110 }
       
   111 
       
   112 inline int QLine::x1() const
       
   113 {
       
   114     return pt1.x();
       
   115 }
       
   116 
       
   117 inline int QLine::y1() const
       
   118 {
       
   119     return pt1.y();
       
   120 }
       
   121 
       
   122 inline int QLine::x2() const
       
   123 {
       
   124     return pt2.x();
       
   125 }
       
   126 
       
   127 inline int QLine::y2() const
       
   128 {
       
   129     return pt2.y();
       
   130 }
       
   131 
       
   132 inline QPoint QLine::p1() const
       
   133 {
       
   134     return pt1;
       
   135 }
       
   136 
       
   137 inline QPoint QLine::p2() const
       
   138 {
       
   139     return pt2;
       
   140 }
       
   141 
       
   142 inline int QLine::dx() const
       
   143 {
       
   144     return pt2.x() - pt1.x();
       
   145 }
       
   146 
       
   147 inline int QLine::dy() const
       
   148 {
       
   149     return pt2.y() - pt1.y();
       
   150 }
       
   151 
       
   152 inline void QLine::translate(const QPoint &point)
       
   153 {
       
   154     pt1 += point;
       
   155     pt2 += point;
       
   156 }
       
   157 
       
   158 inline void QLine::translate(int adx, int ady)
       
   159 {
       
   160     this->translate(QPoint(adx, ady));
       
   161 }
       
   162 
       
   163 inline QLine QLine::translated(const QPoint &p) const
       
   164 {
       
   165     return QLine(pt1 + p, pt2 + p);
       
   166 }
       
   167 
       
   168 inline QLine QLine::translated(int adx, int ady) const
       
   169 {
       
   170     return translated(QPoint(adx, ady));
       
   171 }
       
   172 
       
   173 inline void QLine::setP1(const QPoint &aP1)
       
   174 {
       
   175     pt1 = aP1;
       
   176 }
       
   177 
       
   178 inline void QLine::setP2(const QPoint &aP2)
       
   179 {
       
   180     pt2 = aP2;
       
   181 }
       
   182 
       
   183 inline void QLine::setPoints(const QPoint &aP1, const QPoint &aP2)
       
   184 {
       
   185     pt1 = aP1;
       
   186     pt2 = aP2;
       
   187 }
       
   188 
       
   189 inline void QLine::setLine(int aX1, int aY1, int aX2, int aY2)
       
   190 {
       
   191     pt1 = QPoint(aX1, aY1);
       
   192     pt2 = QPoint(aX2, aY2);
       
   193 }
       
   194 
       
   195 inline bool QLine::operator==(const QLine &d) const
       
   196 {
       
   197     return pt1 == d.pt1 && pt2 == d.pt2;
       
   198 }
       
   199 
       
   200 #ifndef QT_NO_DEBUG_STREAM
       
   201 Q_CORE_EXPORT QDebug operator<<(QDebug d, const QLine &p);
       
   202 #endif
       
   203 
       
   204 #ifndef QT_NO_DATASTREAM
       
   205 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QLine &);
       
   206 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QLine &);
       
   207 #endif
       
   208 
       
   209 /*******************************************************************************
       
   210  * class QLineF
       
   211  *******************************************************************************/
       
   212 class Q_CORE_EXPORT QLineF {
       
   213 public:
       
   214 
       
   215     enum IntersectType { NoIntersection, BoundedIntersection, UnboundedIntersection };
       
   216 
       
   217     inline QLineF();
       
   218     inline QLineF(const QPointF &pt1, const QPointF &pt2);
       
   219     inline QLineF(qreal x1, qreal y1, qreal x2, qreal y2);
       
   220     inline QLineF(const QLine &line) : pt1(line.p1()), pt2(line.p2()) { }
       
   221 
       
   222     static QLineF fromPolar(qreal length, qreal angle);
       
   223 
       
   224     bool isNull() const;
       
   225 
       
   226     inline QPointF p1() const;
       
   227     inline QPointF p2() const;
       
   228 
       
   229     inline qreal x1() const;
       
   230     inline qreal y1() const;
       
   231 
       
   232     inline qreal x2() const;
       
   233     inline qreal y2() const;
       
   234 
       
   235     inline qreal dx() const;
       
   236     inline qreal dy() const;
       
   237 
       
   238     qreal length() const;
       
   239     void setLength(qreal len);
       
   240 
       
   241     qreal angle() const;
       
   242     void setAngle(qreal angle);
       
   243 
       
   244     qreal angleTo(const QLineF &l) const;
       
   245 
       
   246     QLineF unitVector() const;
       
   247     QLineF normalVector() const;
       
   248 
       
   249     // ### Qt 5: rename intersects() or intersection() and rename IntersectType IntersectionType
       
   250     IntersectType intersect(const QLineF &l, QPointF *intersectionPoint) const;
       
   251 
       
   252     qreal angle(const QLineF &l) const;
       
   253 
       
   254     QPointF pointAt(qreal t) const;
       
   255     inline void translate(const QPointF &p);
       
   256     inline void translate(qreal dx, qreal dy);
       
   257 
       
   258     inline QLineF translated(const QPointF &p) const;
       
   259     inline QLineF translated(qreal dx, qreal dy) const;
       
   260 
       
   261     inline void setP1(const QPointF &p1);
       
   262     inline void setP2(const QPointF &p2);
       
   263     inline void setPoints(const QPointF &p1, const QPointF &p2);
       
   264     inline void setLine(qreal x1, qreal y1, qreal x2, qreal y2);
       
   265 
       
   266     inline bool operator==(const QLineF &d) const;
       
   267     inline bool operator!=(const QLineF &d) const { return !(*this == d); }
       
   268 
       
   269     QLine toLine() const;
       
   270 
       
   271 private:
       
   272     QPointF pt1, pt2;
       
   273 };
       
   274 Q_DECLARE_TYPEINFO(QLineF, Q_MOVABLE_TYPE);
       
   275 
       
   276 /*******************************************************************************
       
   277  * class QLineF inline members
       
   278  *******************************************************************************/
       
   279 
       
   280 inline QLineF::QLineF()
       
   281 {
       
   282 }
       
   283 
       
   284 inline QLineF::QLineF(const QPointF &apt1, const QPointF &apt2)
       
   285     : pt1(apt1), pt2(apt2)
       
   286 {
       
   287 }
       
   288 
       
   289 inline QLineF::QLineF(qreal x1pos, qreal y1pos, qreal x2pos, qreal y2pos)
       
   290     : pt1(x1pos, y1pos), pt2(x2pos, y2pos)
       
   291 {
       
   292 }
       
   293 
       
   294 inline qreal QLineF::x1() const
       
   295 {
       
   296     return pt1.x();
       
   297 }
       
   298 
       
   299 inline qreal QLineF::y1() const
       
   300 {
       
   301     return pt1.y();
       
   302 }
       
   303 
       
   304 inline qreal QLineF::x2() const
       
   305 {
       
   306     return pt2.x();
       
   307 }
       
   308 
       
   309 inline qreal QLineF::y2() const
       
   310 {
       
   311     return pt2.y();
       
   312 }
       
   313 
       
   314 inline QPointF QLineF::p1() const
       
   315 {
       
   316     return pt1;
       
   317 }
       
   318 
       
   319 inline QPointF QLineF::p2() const
       
   320 {
       
   321     return pt2;
       
   322 }
       
   323 
       
   324 inline qreal QLineF::dx() const
       
   325 {
       
   326     return pt2.x() - pt1.x();
       
   327 }
       
   328 
       
   329 inline qreal QLineF::dy() const
       
   330 {
       
   331     return pt2.y() - pt1.y();
       
   332 }
       
   333 
       
   334 inline QLineF QLineF::normalVector() const
       
   335 {
       
   336     return QLineF(p1(), p1() + QPointF(dy(), -dx()));
       
   337 }
       
   338 
       
   339 inline void QLineF::translate(const QPointF &point)
       
   340 {
       
   341     pt1 += point;
       
   342     pt2 += point;
       
   343 }
       
   344 
       
   345 inline void QLineF::translate(qreal adx, qreal ady)
       
   346 {
       
   347     this->translate(QPointF(adx, ady));
       
   348 }
       
   349 
       
   350 inline QLineF QLineF::translated(const QPointF &p) const
       
   351 {
       
   352     return QLineF(pt1 + p, pt2 + p);
       
   353 }
       
   354 
       
   355 inline QLineF QLineF::translated(qreal adx, qreal ady) const
       
   356 {
       
   357     return translated(QPointF(adx, ady));
       
   358 }
       
   359 
       
   360 inline void QLineF::setLength(qreal len)
       
   361 {
       
   362     if (isNull())
       
   363         return;
       
   364     QLineF v = unitVector();
       
   365     pt2 = QPointF(pt1.x() + v.dx() * len, pt1.y() + v.dy() * len);
       
   366 }
       
   367 
       
   368 inline QPointF QLineF::pointAt(qreal t) const
       
   369 {
       
   370     qreal vx = pt2.x() - pt1.x();
       
   371     qreal vy = pt2.y() - pt1.y();
       
   372     return QPointF(pt1.x() + vx * t, pt1.y() + vy * t);
       
   373 }
       
   374 
       
   375 inline QLine QLineF::toLine() const
       
   376 {
       
   377     return QLine(pt1.toPoint(), pt2.toPoint());
       
   378 }
       
   379 
       
   380 
       
   381 inline void QLineF::setP1(const QPointF &aP1)
       
   382 {
       
   383     pt1 = aP1;
       
   384 }
       
   385 
       
   386 inline void QLineF::setP2(const QPointF &aP2)
       
   387 {
       
   388     pt2 = aP2;
       
   389 }
       
   390 
       
   391 inline void QLineF::setPoints(const QPointF &aP1, const QPointF &aP2)
       
   392 {
       
   393     pt1 = aP1;
       
   394     pt2 = aP2;
       
   395 }
       
   396 
       
   397 inline void QLineF::setLine(qreal aX1, qreal aY1, qreal aX2, qreal aY2)
       
   398 {
       
   399     pt1 = QPointF(aX1, aY1);
       
   400     pt2 = QPointF(aX2, aY2);
       
   401 }
       
   402 
       
   403 
       
   404 inline bool QLineF::operator==(const QLineF &d) const
       
   405 {
       
   406     return pt1 == d.pt1 && pt2 == d.pt2;
       
   407 }
       
   408 
       
   409 
       
   410 
       
   411 #ifndef QT_NO_DEBUG_STREAM
       
   412 Q_CORE_EXPORT QDebug operator<<(QDebug d, const QLineF &p);
       
   413 #endif
       
   414 
       
   415 #ifndef QT_NO_DATASTREAM
       
   416 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QLineF &);
       
   417 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QLineF &);
       
   418 #endif
       
   419 
       
   420 QT_END_NAMESPACE
       
   421 
       
   422 QT_END_HEADER
       
   423 
       
   424 #endif // QLINE_H