QtCurvesCpp/renderarea.cpp

126 lines
2.1 KiB
C++
Raw Normal View History

2017-08-28 10:07:22 +02:00
#include "renderarea.h"
#include <QPainter>
RenderArea::RenderArea(QWidget *parent) :
QWidget{parent},
mBackgroundColour{36,35,35},
2017-08-28 11:07:50 +02:00
mShapeColour{251,250,250},
mShape{Astroid}
2017-08-28 10:07:22 +02:00
{
2017-08-28 12:32:05 +02:00
OnShapeChanged();
2017-08-28 10:07:22 +02:00
}
2017-08-28 12:32:05 +02:00
QPointF RenderArea::ComputeAstroid(double t){
2017-08-28 12:48:41 +02:00
return QPointF{
2*pow(cos(t),3),
2*pow(sin(t),3)
};
2017-08-28 11:44:07 +02:00
}
2017-08-28 12:32:05 +02:00
QPointF RenderArea::ComputeCycloid(double t)
{
2017-08-28 12:48:41 +02:00
return QPointF{
1.5 * (1 - cos(t)),
1.5 * (t - sin(t))
};
2017-08-28 12:32:05 +02:00
}
QPointF RenderArea::ComputeHuygens(double t)
{
2017-08-28 12:48:41 +02:00
return QPointF{
4 * (3 * cos(t) - cos(3 * t)),
4 * (3 * sin(t) - sin(3 * t))
};
2017-08-28 12:32:05 +02:00
}
QPointF RenderArea::ComputeHypo(double t)
{
2017-08-28 12:48:41 +02:00
return QPointF{
1.5 * (2 * cos(t) + cos(2 * t)),
1.5 * (2 * sin(t) - sin(2 * t))
};
2017-08-28 12:32:05 +02:00
}
2017-08-28 10:07:22 +02:00
QSize RenderArea::minimumSizeHint() const
{
return QSize(100,100);
}
QSize RenderArea::sizeHint() const
{
return QSize(400,200);
}
void RenderArea::paintEvent(QPaintEvent* event)
{
QPainter painter{this};
painter.setRenderHint(QPainter::Antialiasing,true);
2017-08-28 11:07:50 +02:00
2017-08-28 12:32:05 +02:00
painter.setBrush(mBackgroundColour);
painter.setPen(mShapeColour);
painter.drawRect(this->rect());
QPoint center{this->rect().center()};
double step{ mIntervalLenght / mStepCount };
for(float t = 0; t < mIntervalLenght; t += step){
QPointF point = Compute(t);
QPoint pixel;
pixel.setX(point.x() * mScale + center.x());
pixel.setY(point.y() * mScale + center.y());
painter.drawPoint(pixel);
}
}
void RenderArea::OnShapeChanged()
{
2017-08-28 11:07:50 +02:00
switch (mShape) {
case Astroid:
2017-08-28 12:32:05 +02:00
mScale=50;
mIntervalLenght=2*M_PI;
2017-08-28 12:48:41 +02:00
mStepCount=512;
2017-08-28 11:07:50 +02:00
break;
case Cycloid:
2017-08-28 12:48:41 +02:00
mScale=10;
mIntervalLenght=6*M_PI;
mStepCount=128;
2017-08-28 11:07:50 +02:00
break;
case HuygensCycloid:
2017-08-28 12:48:41 +02:00
mScale=10;
mIntervalLenght=4*M_PI;
mStepCount=512;
2017-08-28 11:07:50 +02:00
break;
case HypoCycloid:
2017-08-28 12:48:41 +02:00
mScale=50;
mIntervalLenght=2*M_PI;
mStepCount=256;
2017-08-28 11:07:50 +02:00
break;
default:
break;
}
2017-08-28 12:32:05 +02:00
}
2017-08-28 11:44:07 +02:00
2017-08-28 12:32:05 +02:00
QPointF RenderArea::Compute(double t)
{
switch (mShape) {
case Astroid:
return ComputeAstroid(t);
break;
case Cycloid:
return ComputeCycloid(t);
break;
case HuygensCycloid:
return ComputeHuygens(t);
break;
case HypoCycloid:
return ComputeHypo(t);
break;
default:
break;
2017-08-28 11:44:07 +02:00
}
2017-08-28 12:32:05 +02:00
return QPointF{0,0};
2017-08-28 10:07:22 +02:00
}