source: trunk/src/gui/painting/qpolygonclipper_p.h@ 986

Last change on this file since 986 was 846, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 9.7 KB
RevLine 
[2]1/****************************************************************************
2**
[846]3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
[561]4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
[2]6**
7** This file is part of the QtGui module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
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**
[561]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.
[2]27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
[561]36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
[2]38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#ifndef QPOLYGONCLIPPER_P_H
43#define QPOLYGONCLIPPER_P_H
44
45//
46// W A R N I N G
47// -------------
48//
49// This file is not part of the Qt API. It exists for the convenience
50// of other Qt classes. This header file may change from version to
51// version without notice, or even be removed.
52//
53// We mean it.
54//
55
56#include "private/qdatabuffer_p.h"
57
58QT_BEGIN_NAMESPACE
59
60/* based on sutherland-hodgman line-by-line clipping, as described in
61 Computer Graphics and Principles */
62template <typename InType, typename OutType, typename CastType> class QPolygonClipper
63{
64public:
[846]65 QPolygonClipper() :
66 buffer1(0), buffer2(0)
[2]67 {
68 x1 = y1 = x2 = y2 = 0;
69 }
70
71 ~QPolygonClipper()
72 {
73 }
74
75 void setBoundingRect(const QRect bounds)
76 {
77 x1 = bounds.x();
78 x2 = bounds.x() + bounds.width();
79 y1 = bounds.y();
80 y2 = bounds.y() + bounds.height();
81 }
82
83 QRect boundingRect()
84 {
85 return QRect(QPoint(x1, y1), QPoint(x2, y2));
86 }
87
88 inline OutType intersectLeft(const OutType &p1, const OutType &p2)
89 {
90 OutType t;
91 qreal dy = (p1.y - p2.y) / qreal(p1.x - p2.x);
92 t.x = x1;
93 t.y = static_cast<CastType>(p2.y + (x1 - p2.x) * dy);
94 return t;
95 }
96
97
98 inline OutType intersectRight(const OutType &p1, const OutType &p2)
99 {
100 OutType t;
101 qreal dy = (p1.y - p2.y) / qreal(p1.x - p2.x);
102 t.x = x2;
103 t.y = static_cast<CastType>(p2.y + (x2 - p2.x) * dy);
104 return t;
105 }
106
107
108 inline OutType intersectTop(const OutType &p1, const OutType &p2)
109 {
110 OutType t;
111 qreal dx = (p1.x - p2.x) / qreal(p1.y - p2.y);
112 t.x = static_cast<CastType>(p2.x + (y1 - p2.y) * dx);
113 t.y = y1;
114 return t;
115 }
116
117
118 inline OutType intersectBottom(const OutType &p1, const OutType &p2)
119 {
120 OutType t;
121 qreal dx = (p1.x - p2.x) / qreal(p1.y - p2.y);
122 t.x = static_cast<CastType>(p2.x + (y2 - p2.y) * dx);
123 t.y = y2;
124 return t;
125 }
126
127
128 void clipPolygon(const InType *inPoints, int inCount, OutType **outPoints, int *outCount,
129 bool closePolygon = true)