# The Coordinate System

A paint device in Qt is a drawable 2D surface. QWidget, QPixmap, QPicture and QPrinter are all paint devices.

The default coordinate system of a paint device has its origin located at the top left position. X increases to the right and Y increases downwards. The unit is one pixel.

It is possible to transform the coordinate system when drawing in a paint device.

## An Example

The illustration below shows a magnified portion of the top left corner of a paint device.

The rectangle and the line were drawn by this code:

        void MyWidget::paintEvent( QPaintEvent * )
        {
            QPainter p;
            p.begin( this );
            p.setPen( blue );
            p.drawRect( 1,2, 5,4 );
            p.setPen( red );
            p.drawLine( 9,2, 7,7 );
            p.end();
        }

## Transformations

The QPainter class has the ability to transform graphics. It can translate, scale, shear and rotate everything from simple lines to text and pixmaps.

The following QPainter functions transform the coordinate system:

1. setWindow()
2. setViewport()
3. setWorldMatrix(); uses a QWMatrix
4. translate()
5. scale()
6. shear()
7. rotate()

In addition, the QPixmap::xForm() function can scale, shear and rotate a pixmap.

## Helper Classes

Qt has three classes that provide coordinate system support:

- QPoint
- QSize
- QRect

All QPainter functions that take point, size or rectangle arguments are overloaded. For example, drawLine() can take the integers (x1,y1,x2,y2) or the QPoints (p1,p2).

Qt does not force you to use QPoint, QSize or QRect. You decide.