PolarGraph::PolarGraph()
{
int imageWidth = 600, imageHeight = 600;
image = new QImage(imageWidth, imageHeight, QImage::Format_RGB32);
image->fill(Qt::white);
painter = new QPainter(image);
painter->setRenderHint(QPainter::Antialiasing);
View.setSize(QSize(imageWidth, imageHeight));
painter->setViewport(View);
painter->setViewTransformEnabled(true);
Window.setTopLeft(QPoint(-300, 300));
Window.setBottomRight(QPoint(300,-300));
painter->setWindow(Window);
painter->setWorldMatrixEnabled(true);
World.setTopLeft(QPointF(-10.,10.));
World.setBottomRight(QPointF(10.,-10.));
QPen myPen;
myPen.setColor(Qt::lightGray);
myPen.setWidth(1);
painter->setPen(myPen);
for (qreal i = 0; i < 360; i+=45) {
painter->save();
painter->rotate(i);
painter->drawLine(QPoint(-300,0), QPoint(300,0));
painter->restore();
}
QPoint center(0, 0);
painter->drawEllipse(center,50,50);
painter->drawEllipse(center,150,150);
painter->drawEllipse(center,299,299);
myPen.setColor(Qt::black);
myPen.setWidth(2);
painter->setPen(myPen);
plotLemniscate();
image->save("lemniscate.png");
plotCardiod();
image->save("cardiod.png");
plotCurve1();
image->save("curve1.png");
plotCurve2();
image->save("curve2.png");
}
void PolarGraph::plotCurve1()
{
// r=2cos(Θ/3)
qreal r;
for (qreal theta = 0.; theta < M_PI*2; theta += 0.01) {
r = qCos(theta/3.);
plotRTheta(r, theta);
plotRTheta(r, -theta);
}
}
void PolarGraph::plotCurve2()
{
// r=cos3Θ
qreal r;
for (qreal theta = 0.; theta < M_PI*2; theta += 0.01) {
r = qCos(theta * 3.);
plotRTheta(r, theta);
}
}
void PolarGraph::plotLemniscate()
{
// r^2 = sin(2Θ)
// r = +-sqrt(sin(2Θ)
qreal r;
for (qreal theta = 0.; theta < M_PI*2; theta += 0.01) {
r = qSqrt(qSin(theta * 2.));
plotRTheta(r, theta);
plotRTheta(-r, theta);
}
}
void PolarGraph::plotCardiod()
{
// r=1-cosΘ
qreal r;
for (qreal theta = 0.; theta < M_PI * 2.; theta += 0.01) {
r = 1 - qCos(theta);
plotRTheta(r, theta);
}
}
void PolarGraph::plotRTheta(qreal R, qreal Theta)
{
QPoint Pv;
QPointF Pw;
Pw = QPointF(R, 0.);
qreal thetaDegrees = qRadiansToDegrees(Theta);
int x;
Pv = World2View(Pw);
x = Pv.x();
painter->save();
painter->rotate(thetaDegrees);
painter->drawPoint(x,0);
painter->restore();
}
QPoint PolarGraph::World2View(QPointF Pworld)
{
// Transform matrix from Foley & van Dam _Computer Graphics_ Ch 5.5, fig 5.15
/* View-ViewMin World-WorldMin
* --------------- = -------------------
* ViewMax-ViewMin WorldMax-WorldMin
*
* Vmax-Vmin
* V = ---------(W-Wmin)+Vmin = S(W-Wmin)+Vmin
* Wmax-Wmin
*/
QPoint Pview;
QPointF PviewF;
Vmax = Window.topRight();
Vmin = Window.bottomLeft();
QPointF WtopLeft = QPointF(-5., 5.);
QPointF WbottomRight = QPointF(5., -5);
World = QRectF(WtopLeft, WbottomRight);
Wmax = QPointF(World.topRight());
Wmin = QPointF(World.bottomLeft());
Sx = ( Vmax.x() - Vmin.x() ) / ( Wmax.x() - Wmin.x() );
Sy = ( Vmax.y() - Vmin.y() ) / ( Wmax.y() - Wmin.y() );
PviewF.setX(Sx * (Pworld.x() - Wmin.x()) + Vmin.x());
PviewF.setY(Sy * (Pworld.y() - Wmin.y()) + Vmin.y());
Pview = PviewF.toPoint();
return Pview;
}