draw.cpp Example File
scratchpad/src/draw.cpp
#include <QImage>
#include <QPainter>
#include "draw.h"
QImage Draw::drawCircle(const QSize &size)
{
QImage image(size, QImage::Format_RGB32);
image.fill(Qt::black);
const int centerX = size.width() / 2;
const int centerY = size.height() / 2;
const int radius = std::min(centerX, centerY) * 2 / 3;
const int diameter = radius * 2;
QPainter painter(&image);
painter.setPen(Qt::white);
painter.drawEllipse(centerX-radius, centerY-radius, diameter, diameter);
return image;
}
QImage Draw::drawSquare(const QSize &size)
{
QImage image(size, QImage::Format_RGB32);
image.fill(Qt::black);
const int centerX = size.width() / 2;
const int centerY = size.height() / 2;
const int w = size.width() * 2 / 3;
const int h = size.height() * 2 / 3;
QPainter painter(&image);
painter.setPen(Qt::white);
painter.drawRect(centerX - w/2, centerY - h/2, w, h);
return image;
}
QImage Draw::replicate(const QSize &destinationSize, const QImage &replicant, int numRows, int numCols)
{
if (numRows <= 0 || numCols <= 0)
return QImage();
QImage destination(destinationSize, QImage::Format_RGB32);
const int numPixelsX = destinationSize.width() / numCols;
const int numPixelsY = destinationSize.height() / numRows;
const int extraPixelsX = destination.width() - (numPixelsX * numCols);
const int extraPixelsY = destination.height() - (numPixelsY * numRows);
const int offsetX = extraPixelsX / 2;
const int offsetY = extraPixelsY / 2;
destination.fill(Qt::black);
QPainter painter(&destination);
for (int rows = 0; rows < numRows; ++rows) {
const int y = offsetY + rows * numPixelsY;
for (int cols = 0; cols < numCols; ++cols) {
painter.drawImage(offsetX + cols * numPixelsX, y, replicant);
}
}
return destination;
}