Qt 4.8
Public Functions | Related Functions | List of all members
QPolygon Class Reference

The QPolygon class provides a vector of points using integer precision. More...

#include <qpolygon.h>

Inheritance diagram for QPolygon:
QVector< QPoint >

Public Functions

QRect boundingRect () const
 Returns the bounding rectangle of the polygon, or QRect(0, 0, 0, 0) if the polygon is empty. More...
 
bool containsPoint (const QPoint &pt, Qt::FillRule fillRule) const
 Returns true if the given point is inside the polygon according to the specified fillRule; otherwise returns false. More...
 
QPolygon intersected (const QPolygon &r) const
 Returns a polygon which is the intersection of this polygon and r. More...
 
 operator QVariant () const
 Returns the polygon as a QVariant. More...
 
void point (int i, int *x, int *y) const
 Extracts the coordinates of the point at the given index to {x} and *{y} (if they are valid pointers). More...
 
QPoint point (int i) const
 Returns the point at the given index. More...
 
void putPoints (int index, int nPoints, const int *points)
 Copies nPoints points from the points coord array into this point array, and resizes the point array if index+nPoints exceeds the size of the array. More...
 
void putPoints (int index, int nPoints, int firstx, int firsty,...)
 Copies nPoints points from the variable argument list into this polygon from the given index. More...
 
void putPoints (int index, int nPoints, const QPolygon &from, int fromIndex=0)
 Copies nPoints points from the given fromIndex ( 0 by default) in fromPolygon into this polygon, starting at the specified index. More...
 
 QPolygon ()
 Constructs a polygon with no points. More...
 
 QPolygon (int size)
 Constructs a polygon of the given size. More...
 
 QPolygon (const QPolygon &a)
 Constructs a copy of the given polygon. More...
 
 QPolygon (const QVector< QPoint > &v)
 Constructs a polygon containing the specified points. More...
 
 QPolygon (const QRect &r, bool closed=false)
 Constructs a polygon from the given rectangle. More...
 
 QPolygon (int nPoints, const int *points)
 Constructs a point array with nPoints points, taken from the points array. More...
 
void setPoint (int index, int x, int y)
 Sets the point at the given index to the point specified by ({x}, {y}). More...
 
void setPoint (int index, const QPoint &p)
 Sets the point at the given index to the given point. More...
 
void setPoints (int nPoints, const int *points)
 Resizes the polygon to nPoints and populates it with the given points. More...
 
void setPoints (int nPoints, int firstx, int firsty,...)
 Resizes the polygon to nPoints and populates it with the points specified by the variable argument list. More...
 
QPolygon subtracted (const QPolygon &r) const
 Returns a polygon which is r subtracted from this polygon. More...
 
void swap (QPolygon &other)
 Swaps polygon other with this polygon. More...
 
void translate (int dx, int dy)
 Translates all points in the polygon by ({dx}, {dy}). More...
 
void translate (const QPoint &offset)
 Translates all points in the polygon by the given offset. More...
 
QPolygon translated (int dx, int dy) const
 Returns a copy of the polygon that is translated by ({dx}, {dy}). More...
 
QPolygon translated (const QPoint &offset) const
 Returns a copy of the polygon that is translated by the given offset. More...
 
QPolygon united (const QPolygon &r) const
 Returns a polygon which is the union of this polygon and r. More...
 
 ~QPolygon ()
 Destroys the polygon. More...
 
- Public Functions inherited from QVector< QPoint >
void append (const QPoint &t)
 Inserts value at the end of the vector. More...
 
const QPointat (int i) const
 Returns the item at index position i in the vector. More...
 
reference back ()
 This function is provided for STL compatibility. More...
 
const_reference back () const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
 
iterator begin ()
 Returns an STL-style iterator pointing to the first item in the vector. More...
 
const_iterator begin () const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
 
int capacity () const
 Returns the maximum number of items that can be stored in the vector without forcing a reallocation. More...
 
void clear ()
 Removes all the elements from the vector and releases the memory used by the vector. More...
 
const_iterator constBegin () const
 Returns a const STL-style iterator pointing to the first item in the vector. More...
 
const QPointconstData () const
 Returns a const pointer to the data stored in the vector. More...
 
const_iterator constEnd () const
 Returns a const STL-style iterator pointing to the imaginary item after the last item in the vector. More...
 
bool contains (const QPoint &t) const
 Returns true if the vector contains an occurrence of value; otherwise returns false. More...
 
int count (const QPoint &t) const
 Returns the number of occurrences of value in the vector. More...
 
int count () const
 Same as size(). More...
 
QPointdata ()
 Returns a pointer to the data stored in the vector. More...
 
const QPointdata () const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
 
void detach ()
 
bool empty () const
 This function is provided for STL compatibility. More...
 
iterator end ()
 Returns an STL-style iterator pointing to the imaginary item after the last item in the vector. More...
 
const_iterator end () const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
 
bool endsWith (const QPoint &t) const
 Returns true if this vector is not empty and its last item is equal to value; otherwise returns false. More...
 
iterator erase (iterator begin, iterator end)
 Removes all the items from begin up to (but not including) end. More...
 
iterator erase (iterator pos)
 Removes the item pointed to by the iterator pos from the vector, and returns an iterator to the next item in the vector (which may be end()). More...
 
QVector< QPoint > & fill (const QPoint &t, int size=-1)
 Assigns value to all items in the vector. More...
 
QPointfirst ()
 Returns a reference to the first item in the vector. More...
 
const QPointfirst () const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
 
QPointfront ()
 This function is provided for STL compatibility. More...
 
const_reference front () const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
 
int indexOf (const QPoint &t, int from=0) const
 Returns the index position of the first occurrence of value in the vector, searching forward from index position from. More...
 
void insert (int i, const QPoint &t)
 Inserts value at index position i in the vector. More...
 
void insert (int i, int n, const QPoint &t)
 Inserts count copies of value at index position i in the vector. More...
 
iterator insert (iterator before, int n, const QPoint &x)
 Inserts count copies of value in front of the item pointed to by the iterator before. More...
 
iterator insert (iterator before, const QPoint &x)
 Inserts value in front of the item pointed to by the iterator before. More...
 
bool isDetached () const
 
bool isEmpty () const
 Returns true if the vector has size 0; otherwise returns false. More...
 
bool isSharedWith (const QVector< QPoint > &other) const
 
QPointlast ()
 Returns a reference to the last item in the vector. More...
 
const QPointlast () const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
 
int lastIndexOf (const QPoint &t, int from=-1) const
 Returns the index position of the last occurrence of the value value in the vector, searching backward from index position from. More...
 
QVector< QPointmid (int pos, int length=-1) const
 Returns a vector whose elements are copied from this vector, starting at position pos. More...
 
bool operator!= (const QVector< QPoint > &v) const
 Returns true if other is not equal to this vector; otherwise returns false. More...
 
QVector< QPointoperator+ (const QVector< QPoint > &l) const
 Returns a vector that contains all the items in this vector followed by all the items in the other vector. More...
 
QVector< QPoint > & operator+= (const QVector< QPoint > &l)
 Appends the items of the other vector to this vector and returns a reference to this vector. More...
 
QVector< QPoint > & operator+= (const QPoint &t)
 Appends value to the vector. More...
 
QVector< QPoint > & operator<< (const QVector< QPoint > &l)
 Appends other to the vector and returns a reference to the vector. More...
 
QVector< QPoint > & operator<< (const QPoint &t)
 Appends value to the vector and returns a reference to this vector. More...
 
QVector< QPoint > & operator= (const QVector< QPoint > &v)
 Assigns other to this vector and returns a reference to this vector. More...
 
bool operator== (const QVector< QPoint > &v) const
 Returns true if other is equal to this vector; otherwise returns false. More...
 
QPointoperator[] (int i)
 Returns the item at index position i as a modifiable reference. More...
 
const QPointoperator[] (int i) const
 Same as at(i). More...
 
void pop_back ()
 This function is provided for STL compatibility. More...
 
void pop_front ()
 This function is provided for STL compatibility. More...
 
void prepend (const QPoint &t)
 Inserts value at the beginning of the vector. More...
 
void push_back (const QPoint &t)
 This function is provided for STL compatibility. More...
 
void push_front (const QPoint &t)
 This function is provided for STL compatibility. More...
 
 QVector ()
 Constructs an empty vector. More...
 
 QVector (int size)
 Constructs a vector with an initial size of size elements. More...
 
 QVector (int size, const QPoint &t)
 Constructs a vector with an initial size of size elements. More...
 
 QVector (const QVector< QPoint > &v)
 Constructs a copy of other. More...
 
void remove (int i)
 Removes the element at index position i. More...
 
void remove (int i, int n)
 Removes count elements from the middle of the vector, starting at index position i. More...
 
void replace (int i, const QPoint &t)
 Replaces the item at index position i with value. More...
 
void reserve (int size)
 Attempts to allocate memory for at least size elements. More...
 
void resize (int size)
 Sets the size of the vector to size. More...
 
void setSharable (bool sharable)
 
int size () const
 Returns the number of items in the vector. More...
 
void squeeze ()
 Releases any memory not required to store the items. More...
 
bool startsWith (const QPoint &t) const
 Returns true if this vector is not empty and its first item is equal to value; otherwise returns false. More...
 
void swap (QVector< QPoint > &other)
 Swaps vector other with this vector. More...
 
QList< QPointtoList () const
 Returns a QList object with the data contained in this QVector. More...
 
std::vector< QPointtoStdVector () const
 Returns a std::vector object with the data contained in this QVector. More...
 
QPoint value (int i) const
 Returns the value at index position i in the vector. More...
 
QPoint value (int i, const QPoint &defaultValue) const
 If the index i is out of bounds, the function returns defaultValue. More...
 
 ~QVector ()
 Destroys the vector. More...
 

Related Functions

(Note that these are not member functions.)

QDataStreamoperator<< (QDataStream &stream, const QPolygon &polygon)
 Writes the given polygon to the given stream, and returns a reference to the stream. More...
 
QDataStreamoperator>> (QDataStream &stream, QPolygon &polygon)
 Reads a polygon from the given stream into the given polygon, and returns a reference to the stream. More...
 

Additional Inherited Members

- Public Types inherited from QVector< QPoint >
typedef const QPointconst_iterator
 The QVector::const_iterator typedef provides an STL-style const iterator for QVector and QStack. More...
 
typedef const value_typeconst_pointer
 Typedef for const T *. More...
 
typedef const value_typeconst_reference
 Typedef for T &. More...
 
typedef const_iterator ConstIterator
 Qt-style synonym for QVector::const_iterator. More...
 
typedef qptrdiff difference_type
 Typedef for ptrdiff_t. More...
 
typedef QPointiterator
 The QVector::iterator typedef provides an STL-style non-const iterator for QVector and QStack. More...
 
typedef iterator Iterator
 Qt-style synonym for QVector::iterator. More...
 
typedef value_typepointer
 Typedef for T *. More...
 
typedef value_typereference
 Typedef for T &. More...
 
typedef int size_type
 Typedef for int. More...
 
typedef QPoint value_type
 Typedef for T. More...
 
- Static Public Functions inherited from QVector< QPoint >
static QVector< QPointfromList (const QList< QPoint > &list)
 Returns a QVector object with the data contained in list. More...
 
static QVector< QPointfromStdVector (const std::vector< QPoint > &vector)
 Returns a QVector object with the data contained in vector. More...
 
- Public Variables inherited from QVector< QPoint >
QVectorDatad
 
Datap
 

Detailed Description

The QPolygon class provides a vector of points using integer precision.

Note
This class or function is reentrant.

A QPolygon object is a QVector<QPoint>. The easiest way to add points to a QPolygon is to use QVector's streaming operator, as illustrated below:

QPolygon polygon;
polygon << QPoint(10, 20) << QPoint(20, 30);

In addition to the functions provided by QVector, QPolygon provides some point-specific functions.

Each point in a polygon can be retrieved by passing its index to the point() function. To populate the polygon, QPolygon provides the setPoint() function to set the point at a given index, the setPoints() function to set all the points in the polygon (resizing it to the given number of points), and the putPoints() function which copies a number of given points into the polygon from a specified index (resizing the polygon if necessary).

QPolygon provides the boundingRect() and translate() functions for geometry functions. Use the QMatrix::map() function for more general transformations of QPolygons.

The QPolygon class is Implicit Data Sharing.

See also
QVector, QPolygonF, QLine

Definition at line 60 of file qpolygon.h.

Constructors and Destructors

◆ QPolygon() [1/6]

QPolygon::QPolygon ( )
inline

Constructs a polygon with no points.

See also
QVector::isEmpty()

Definition at line 63 of file qpolygon.h.

63 {}

◆ ~QPolygon()

QPolygon::~QPolygon ( )
inline

Destroys the polygon.

Definition at line 64 of file qpolygon.h.

64 {}

◆ QPolygon() [2/6]

QPolygon::QPolygon ( int  size)
inline

Constructs a polygon of the given size.

Creates an empty polygon if size == 0.

See also
QVector::isEmpty()

Definition at line 99 of file qpolygon.h.

◆ QPolygon() [3/6]

QPolygon::QPolygon ( const QPolygon polygon)
inline

Constructs a copy of the given polygon.

See also
setPoints()

Definition at line 66 of file qpolygon.h.

66 : QVector<QPoint>(a) {}
long ASN1_INTEGER_get ASN1_INTEGER * a

◆ QPolygon() [4/6]

QPolygon::QPolygon ( const QVector< QPoint > &  points)
inline

Constructs a polygon containing the specified points.

See also
setPoints()

Definition at line 67 of file qpolygon.h.

◆ QPolygon() [5/6]

QPolygon::QPolygon ( const QRect rectangle,
bool  closed = false 
)

Constructs a polygon from the given rectangle.

If closed is false, the polygon just contains the four points of the rectangle ordered clockwise, otherwise the polygon's fifth point is set to {rectangle}.topLeft().

Note that the bottom-right corner of the rectangle is located at (rectangle.x() + rectangle.width(), rectangle.y() + rectangle.height()).

See also
setPoints()

Definition at line 194 of file qpolygon.cpp.

195 {
196  reserve(closed ? 5 : 4);
197  *this << QPoint(r.x(), r.y())
198  << QPoint(r.x() + r.width(), r.y())
199  << QPoint(r.x() + r.width(), r.y() + r.height())
200  << QPoint(r.x(), r.y() + r.height());
201  if (closed)
202  *this << QPoint(r.left(), r.top());
203 }
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
void reserve(int size)
Attempts to allocate memory for at least size elements.
Definition: qvector.h:339

◆ QPolygon() [6/6]

QPolygon::QPolygon ( int  nPoints,
const int *  points 
)

Constructs a point array with nPoints points, taken from the points array.

Warning
This function is not part of the public interface.

Equivalent to setPoints(nPoints, points).

Definition at line 216 of file qpolygon.cpp.

217 {
218  setPoints(nPoints, points);
219 }
void setPoints(int nPoints, const int *points)
Resizes the polygon to nPoints and populates it with the given points.
Definition: qpolygon.cpp:350

Functions

◆ boundingRect()

QRect QPolygon::boundingRect ( ) const

Returns the bounding rectangle of the polygon, or QRect(0, 0, 0, 0) if the polygon is empty.

See also
QVector::isEmpty()

Definition at line 492 of file qpolygon.cpp.

Referenced by QWindowsStyle::drawPrimitive(), QX11PaintEnginePrivate::fillPolygon_dev(), and QPolygonF::isClosed().

493 {
494  if (isEmpty())
495  return QRect(0, 0, 0, 0);
496  register const QPoint *pd = constData();
497  int minx, maxx, miny, maxy;
498  minx = maxx = pd->x();
499  miny = maxy = pd->y();
500  ++pd;
501  for (int i = 1; i < size(); ++i) {
502  if (pd->x() < minx)
503  minx = pd->x();
504  else if (pd->x() > maxx)
505  maxx = pd->x();
506  if (pd->y() < miny)
507  miny = pd->y();
508  else if (pd->y() > maxy)
509  maxy = pd->y();
510  ++pd;
511  }
512  return QRect(QPoint(minx,miny), QPoint(maxx,maxy));
513 }
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
int y() const
Returns the y coordinate of this point.
Definition: qpoint.h:131
bool isEmpty() const
Returns true if the vector has size 0; otherwise returns false.
Definition: qvector.h:139
const QPoint * constData() const
Returns a const pointer to the data stored in the vector.
Definition: qvector.h:154
int x() const
Returns the x coordinate of this point.
Definition: qpoint.h:128
int size() const
Returns the number of items in the vector.
Definition: qvector.h:137

◆ containsPoint()

bool QPolygon::containsPoint ( const QPoint point,
Qt::FillRule  fillRule 
) const

Returns true if the given point is inside the polygon according to the specified fillRule; otherwise returns false.

Since
4.3

Definition at line 974 of file qpolygon.cpp.

Referenced by QPolygonF::isClosed().

975 {
976  if (isEmpty())
977  return false;
978 
979  int winding_number = 0;
980 
981  QPoint last_pt = at(0);
982  QPoint last_start = at(0);
983  for (int i = 1; i < size(); ++i) {
984  const QPoint &e = at(i);
985  qt_polygon_isect_line(last_pt, e, pt, &winding_number);
986  last_pt = e;
987  }
988 
989  // implicitly close last subpath
990  if (last_pt != last_start)
991  qt_polygon_isect_line(last_pt, last_start, pt, &winding_number);
992 
993  return (fillRule == Qt::WindingFill
994  ? (winding_number != 0)
995  : ((winding_number % 2) != 0));
996 }
static void qt_polygon_isect_line(const QPointF &p1, const QPointF &p2, const QPointF &pos, int *winding)
Definition: qpolygon.cpp:57
const QPoint & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
bool isEmpty() const
Returns true if the vector has size 0; otherwise returns false.
Definition: qvector.h:139
int size() const
Returns the number of items in the vector.
Definition: qvector.h:137

◆ intersected()

QPolygon QPolygon::intersected ( const QPolygon r) const

Returns a polygon which is the intersection of this polygon and r.

Since
4.3

Set operations on polygons will treat the polygons as areas. Non-closed polygons will be treated as implicitly closed.

Definition at line 1032 of file qpolygon.cpp.

Referenced by QPolygonF::isClosed().

1033 {
1034  QPainterPath subject; subject.addPolygon(*this);
1035  QPainterPath clip; clip.addPolygon(r);
1036 
1037  return subject.intersected(clip).toFillPolygon().toPolygon();
1038 }
The QPainterPath class provides a container for painting operations, enabling graphical shapes to be ...
Definition: qpainterpath.h:67
QPainterPath intersected(const QPainterPath &r) const
Returns a path which is the intersection of this path&#39;s fill area and p&#39;s fill area.
void addPolygon(const QPolygonF &polygon)
Adds the given polygon to the path as an (unclosed) subpath.
QPolygon toPolygon() const
Creates and returns a QPolygon by converting each QPointF to a QPoint.
Definition: qpolygon.cpp:772
QPolygonF toFillPolygon(const QMatrix &matrix=QMatrix()) const
This is an overloaded member function, provided for convenience. It differs from the above function o...

◆ operator QVariant()

QPolygon::operator QVariant ( ) const

Returns the polygon as a QVariant.

Definition at line 806 of file qpolygon.cpp.

807 {
808  return QVariant(QVariant::Polygon, this);
809 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92

◆ point() [1/2]

void QPolygon::point ( int  index,
int *  x,
int *  y 
) const

Extracts the coordinates of the point at the given index to {x} and *{y} (if they are valid pointers).

See also
setPoint()

Definition at line 297 of file qpolygon.cpp.

Referenced by QCommonStyle::drawControl().

298 {
299  QPoint p = at(index);
300  if (x)
301  *x = (int)p.x();
302  if (y)
303  *y = (int)p.y();
304 }
const QPoint & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
int y() const
Returns the y coordinate of this point.
Definition: qpoint.h:131
quint16 index
int x() const
Returns the x coordinate of this point.
Definition: qpoint.h:128

◆ point() [2/2]

QPoint QPolygon::point ( int  index) const
inline

Returns the point at the given index.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 123 of file qpolygon.h.

124 { return at(index); }
const QPoint & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350
quint16 index

◆ putPoints() [1/3]

void QPolygon::putPoints ( int  index,
int  nPoints,
const int *  points 
)

Copies nPoints points from the points coord array into this point array, and resizes the point array if index+nPoints exceeds the size of the array.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Warning
This function is not part of the public interface.
See also
setPoint()

Definition at line 406 of file qpolygon.cpp.

Referenced by QMotifStyle::drawPrimitive(), and qDrawPlainRect().

407 {
408  if (index + nPoints > size())
409  resize(index + nPoints);
410  int i = index;
411  while (nPoints--) {
412  setPoint(i++, *points, *(points+1));
413  points += 2;
414  }
415 }
void resize(int size)
Sets the size of the vector to size.
Definition: qvector.h:342
void setPoint(int index, int x, int y)
Sets the point at the given index to the point specified by ({x}, {y}).
Definition: qpolygon.h:120
quint16 index
int size() const
Returns the number of items in the vector.
Definition: qvector.h:137

◆ putPoints() [2/3]

void QPolygon::putPoints ( int  index,
int  nPoints,
int  firstx,
int  firsty,
  ... 
)

Copies nPoints points from the variable argument list into this polygon from the given index.

The points are given as a sequence of integers, starting with firstx then firsty, and so on. The polygon is resized if index+nPoints exceeds its current size.

The example code creates a polygon with three points (4,5), (6,7) and (8,9), by expanding the polygon from 1 to 3 points:

QPolygon polygon(1);
polygon[0] = QPoint(4, 5);
polygon.putPoints(1, 2, 6,7, 8,9);

The following code has the same result, but here the putPoints() function overwrites rather than extends:

QPolygon polygon(3);
polygon.putPoints(0, 3, 4,5, 0,0, 8,9);
polygon.putPoints(1, 1, 6,7);
See also
setPoints()

Definition at line 438 of file qpolygon.cpp.

439 {
440  va_list ap;
441  if (index + nPoints > size())
442  resize(index + nPoints);
443  if (nPoints <= 0)
444  return;
445  setPoint(index, firstx, firsty);
446  int i = index, x, y;
447  va_start(ap, firsty);
448  while (--nPoints) {
449  x = va_arg(ap, int);
450  y = va_arg(ap, int);
451  setPoint(++i, x, y);
452  }
453  va_end(ap);
454 }
void resize(int size)
Sets the size of the vector to size.
Definition: qvector.h:342
void setPoint(int index, int x, int y)
Sets the point at the given index to the point specified by ({x}, {y}).
Definition: qpolygon.h:120
quint16 index
int size() const
Returns the number of items in the vector.
Definition: qvector.h:137

◆ putPoints() [3/3]

void QPolygon::putPoints ( int  index,
int  nPoints,
const QPolygon fromPolygon,
int  fromIndex = 0 
)

Copies nPoints points from the given fromIndex ( 0 by default) in fromPolygon into this polygon, starting at the specified index.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

For example:

QPolygon polygon1;
polygon1.putPoints(0, 3, 1,2, 0,0, 5,6);
// polygon1 is now the three-point polygon(1,2, 0,0, 5,6);
QPolygon polygon2;
polygon2.putPoints(0, 3, 4,4, 5,5, 6,6);
// polygon2 is now (4,4, 5,5, 6,6);
polygon1.putPoints(2, 3, polygon2);
// polygon1 is now the five-point polygon(1,2, 0,0, 4,4, 5,5, 6,6);

Definition at line 471 of file qpolygon.cpp.

472 {
473  if (index + nPoints > size())
474  resize(index + nPoints);
475  if (nPoints <= 0)
476  return;
477  int n = 0;
478  while(n < nPoints) {
479  setPoint(index + n, from[fromIndex+n]);
480  ++n;
481  }
482 }
void resize(int size)
Sets the size of the vector to size.
Definition: qvector.h:342
void setPoint(int index, int x, int y)
Sets the point at the given index to the point specified by ({x}, {y}).
Definition: qpolygon.h:120
quint16 index
int size() const
Returns the number of items in the vector.
Definition: qvector.h:137

◆ setPoint() [1/2]

void QPolygon::setPoint ( int  index,
int  x,
int  y 
)
inline

Sets the point at the given index to the point specified by ({x}, {y}).

See also
point(), putPoints(), setPoints(),

Definition at line 120 of file qpolygon.h.

Referenced by addPoint(), QWindowsStyle::drawComplexControl(), QCommonStyle::drawControl(), QCommonStyle::drawPrimitive(), QCDEStyle::drawPrimitive(), QMotifStyle::drawPrimitive(), putPoints(), qDrawPlainRect(), rot(), and setPoints().

121 { (*this)[index] = QPoint(x, y); }
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
quint16 index

◆ setPoint() [2/2]

void QPolygon::setPoint ( int  index,
const QPoint point 
)
inline

Sets the point at the given index to the given point.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 117 of file qpolygon.h.

118 { (*this)[index] = pt; }
quint16 index

◆ setPoints() [1/2]

void QPolygon::setPoints ( int  nPoints,
const int *  points 
)

Resizes the polygon to nPoints and populates it with the given points.

The example code creates a polygon with two points (10, 20) and (30, 40):

static const int points[] = { 10, 20, 30, 40 };
QPolygon polygon;
polygon.setPoints(2, points);
See also
setPoint() putPoints()

Definition at line 350 of file qpolygon.cpp.

Referenced by QWindowsCEStyle::drawComplexControl(), QWindowsMobileStyle::drawComplexControl(), QWindowsStyle::drawComplexControl(), QMotifStyle::drawComplexControl(), QMotifStyle::drawControl(), QCommonStyle::drawPrimitive(), QCDEStyle::drawPrimitive(), QWindowsStyle::drawPrimitive(), QMotifStyle::drawPrimitive(), QMatrix::mapToPolygon(), QTransform::mapToPolygon(), QColorLuminancePicker::paintEvent(), qDrawPlainRect(), qDrawShadeLine(), and QPolygon().

351 {
352  resize(nPoints);
353  int i = 0;
354  while (nPoints--) {
355  setPoint(i++, *points, *(points+1));
356  points += 2;
357  }
358 }
void resize(int size)
Sets the size of the vector to size.
Definition: qvector.h:342
void setPoint(int index, int x, int y)
Sets the point at the given index to the point specified by ({x}, {y}).
Definition: qpolygon.h:120

◆ setPoints() [2/2]

void QPolygon::setPoints ( int  nPoints,
int  firstx,
int  firsty,
  ... 
)

Resizes the polygon to nPoints and populates it with the points specified by the variable argument list.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

The points are given as a sequence of integers, starting with firstx then firsty, and so on.

The example code creates a polygon with two points (10, 20) and (30, 40):

QPolygon polygon;
polygon.setPoints(2, 10, 20, 30, 40);

Definition at line 377 of file qpolygon.cpp.

378 {
379  va_list ap;
380  resize(nPoints);
381  setPoint(0, firstx, firsty);
382  int i = 0, x, y;
383  va_start(ap, firsty);
384  while (--nPoints) {
385  x = va_arg(ap, int);
386  y = va_arg(ap, int);
387  setPoint(++i, x, y);
388  }
389  va_end(ap);
390 }
void resize(int size)
Sets the size of the vector to size.
Definition: qvector.h:342
void setPoint(int index, int x, int y)
Sets the point at the given index to the point specified by ({x}, {y}).
Definition: qpolygon.h:120

◆ subtracted()

QPolygon QPolygon::subtracted ( const QPolygon r) const

Returns a polygon which is r subtracted from this polygon.

Since
4.3

Set operations on polygons will treat the polygons as areas. Non-closed polygons will be treated as implicitly closed.

Definition at line 1053 of file qpolygon.cpp.

Referenced by QPolygonF::isClosed().

1054 {
1055  QPainterPath subject; subject.addPolygon(*this);
1056  QPainterPath clip; clip.addPolygon(r);
1057 
1058  return subject.subtracted(clip).toFillPolygon().toPolygon();
1059 }
The QPainterPath class provides a container for painting operations, enabling graphical shapes to be ...
Definition: qpainterpath.h:67
QPainterPath subtracted(const QPainterPath &r) const
Returns a path which is p&#39;s fill area subtracted from this path&#39;s fill area.
void addPolygon(const QPolygonF &polygon)
Adds the given polygon to the path as an (unclosed) subpath.
QPolygon toPolygon() const
Creates and returns a QPolygon by converting each QPointF to a QPoint.
Definition: qpolygon.cpp:772
QPolygonF toFillPolygon(const QMatrix &matrix=QMatrix()) const
This is an overloaded member function, provided for convenience. It differs from the above function o...

◆ swap()

void QPolygon::swap ( QPolygon other)
inline

Swaps polygon other with this polygon.

Since
4.8

This operation is very fast and never fails.

Definition at line 70 of file qpolygon.h.

70 { QVector<QPoint>::swap(other); } // prevent QVector<QPoint><->QPolygon swaps
void swap(QVector< T > &other)
Swaps vector other with this vector.
Definition: qvector.h:130

◆ translate() [1/2]

void QPolygon::translate ( int  dx,
int  dy 
)

Translates all points in the polygon by ({dx}, {dy}).

See also
translated()

Definition at line 238 of file qpolygon.cpp.

Referenced by QCommonStyle::drawControl(), QCommonStyle::drawPrimitive(), QCDEStyle::drawPrimitive(), QMotifStyle::drawPrimitive(), qDrawPlainRect(), QPolygonF::swap(), translate(), and translated().

239 {
240  if (dx == 0 && dy == 0)
241  return;
242 
243  register QPoint *p = data();
244  register int i = size();
245  QPoint pt(dx, dy);
246  while (i--) {
247  *p += pt;
248  ++p;
249  }
250 }
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
QPoint * data()
Returns a pointer to the data stored in the vector.
Definition: qvector.h:152
int size() const
Returns the number of items in the vector.
Definition: qvector.h:137

◆ translate() [2/2]

void QPolygon::translate ( const QPoint offset)
inline

Translates all points in the polygon by the given offset.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

See also
translated()

Definition at line 126 of file qpolygon.h.

127 { translate(offset.x(), offset.y()); }
void translate(int dx, int dy)
Translates all points in the polygon by ({dx}, {dy}).
Definition: qpolygon.cpp:238
int y() const
Returns the y coordinate of this point.
Definition: qpoint.h:131
int x() const
Returns the x coordinate of this point.
Definition: qpoint.h:128

◆ translated() [1/2]

QPolygon QPolygon::translated ( int  dx,
int  dy 
) const

Returns a copy of the polygon that is translated by ({dx}, {dy}).

Since
4.6
See also
translate()

Definition at line 270 of file qpolygon.cpp.

Referenced by QTransform::map(), QPolygonF::swap(), and translated().

271 {
272  QPolygon copy(*this);
273  copy.translate(dx, dy);
274  return copy;
275 }
The QPolygon class provides a vector of points using integer precision.
Definition: qpolygon.h:60

◆ translated() [2/2]

QPolygon QPolygon::translated ( const QPoint offset) const
inline

Returns a copy of the polygon that is translated by the given offset.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Since
4.6
See also
translate()

Definition at line 129 of file qpolygon.h.

130 { return translated(offset.x(), offset.y()); }
QPolygon translated(int dx, int dy) const
Returns a copy of the polygon that is translated by ({dx}, {dy}).
Definition: qpolygon.cpp:270
int y() const
Returns the y coordinate of this point.
Definition: qpoint.h:131
int x() const
Returns the x coordinate of this point.
Definition: qpoint.h:128

◆ united()

QPolygon QPolygon::united ( const QPolygon r) const

Returns a polygon which is the union of this polygon and r.

Since
4.3

Set operations on polygons, will treat the polygons as areas, and implicitly close the polygon.

See also
intersected(), subtracted()

Definition at line 1012 of file qpolygon.cpp.

Referenced by QPolygonF::isClosed().

1013 {
1014  QPainterPath subject; subject.addPolygon(*this);
1015  QPainterPath clip; clip.addPolygon(r);
1016 
1017  return subject.united(clip).toFillPolygon().toPolygon();
1018 }
The QPainterPath class provides a container for painting operations, enabling graphical shapes to be ...
Definition: qpainterpath.h:67
void addPolygon(const QPolygonF &polygon)
Adds the given polygon to the path as an (unclosed) subpath.
QPolygon toPolygon() const
Creates and returns a QPolygon by converting each QPointF to a QPoint.
Definition: qpolygon.cpp:772
QPolygonF toFillPolygon(const QMatrix &matrix=QMatrix()) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
QPainterPath united(const QPainterPath &r) const
Returns a path which is the union of this path&#39;s fill area and p&#39;s fill area.

Friends and Related Functions

◆ operator<<()

QDataStream & operator<< ( QDataStream stream,
const QPolygon polygon 
)
related

Writes the given polygon to the given stream, and returns a reference to the stream.

Since
4.4
See also
{Serializing Qt Data Types}

Definition at line 828 of file qpolygon.cpp.

Referenced by QPolygon().

829 {
830  const QVector<QPoint> &v = a;
831  return s << v;
832 }
long ASN1_INTEGER_get ASN1_INTEGER * a

◆ operator>>()

QDataStream & operator>> ( QDataStream stream,
QPolygon polygon 
)
related

Reads a polygon from the given stream into the given polygon, and returns a reference to the stream.

Since
4.4
See also
{Serializing Qt Data Types}

Definition at line 847 of file qpolygon.cpp.

Referenced by QPolygon().

848 {
849  QVector<QPoint> &v = a;
850  return s >> v;
851 }
long ASN1_INTEGER_get ASN1_INTEGER * a

The documentation for this class was generated from the following files: