code handles curves dinamically

This commit is contained in:
thepra 2017-08-28 12:32:05 +02:00
parent 4f847aa671
commit 66b3828254
2 changed files with 77 additions and 32 deletions

View File

@ -6,13 +6,28 @@ RenderArea::RenderArea(QWidget *parent) :
mBackgroundColour{36,35,35},
mShapeColour{251,250,250},
mShape{Astroid}
{
OnShapeChanged();
}
QPointF RenderArea::ComputeAstroid(double t){
double cos_t{cos(t)},sin_t{sin(t)},x{2*cos_t*cos_t*cos_t},y{2*sin_t*sin_t*sin_t};
return QPointF{x,y};
}
QPointF RenderArea::ComputeCycloid(double t)
{
}
QPointF RenderArea::ComputeAstroid(float t){
double cos_t{cos(t)},sin_t{sin(t)},x{2*cos_t*cos_t*cos_t},y{2*sin_t*sin_t*sin_t};
return QPointF{x,y};
QPointF RenderArea::ComputeHuygens(double t)
{
}
QPointF RenderArea::ComputeHypo(double t)
{
}
QSize RenderArea::minimumSizeHint() const
@ -30,40 +45,64 @@ void RenderArea::paintEvent(QPaintEvent* event)
QPainter painter{this};
painter.setRenderHint(QPainter::Antialiasing,true);
switch (mShape) {
case Astroid:
mBackgroundColour = Qt::red;
break;
case Cycloid:
mBackgroundColour = Qt::green;
break;
case HuygensCycloid:
mBackgroundColour = Qt::blue;
break;
case HypoCycloid:
mBackgroundColour = Qt::yellow;
break;
default:
break;
}
painter.setBrush(mBackgroundColour);
painter.setPen(mShapeColour);
painter.drawRect(this->rect());
QPoint center{this->rect().center()};
int stepCount{1024};
double scale{40},intervalLenght{2 * M_PI},step{intervalLenght / stepCount};
for(float t = 0; t < intervalLenght; t += step){
QPointF point = ComputeAstroid(t);
double step{ mIntervalLenght / mStepCount };
for(float t = 0; t < mIntervalLenght; t += step){
QPointF point = Compute(t);
QPoint pixel;
pixel.setX(point.x() * scale + center.x());
pixel.setY(point.y() * scale + center.y());
pixel.setX(point.x() * mScale + center.x());
pixel.setY(point.y() * mScale + center.y());
painter.drawPoint(pixel);
}
}
void RenderArea::OnShapeChanged()
{
switch (mShape) {
case Astroid:
mScale=50;
mIntervalLenght=2*M_PI;
mStepCount=256;
break;
case Cycloid:
break;
case HuygensCycloid:
break;
case HypoCycloid:
break;
default:
break;
}
}
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;
}
return QPointF{0,0};
}

View File

@ -21,7 +21,7 @@ class RenderArea : public QWidget
void setBackgroundColor(QColor color) { mBackgroundColour = color; }
QColor backgroundColor() const { return mBackgroundColour; }
void setShape(ShapesType shape) { mShape = shape; }
void setShape(ShapesType shape) { mShape = shape; OnShapeChanged(); }
ShapesType shape() const { return mShape; }
@ -33,9 +33,15 @@ class RenderArea : public QWidget
public slots:
private:
QPointF ComputeAstroid(float t);
QColor mBackgroundColour;
QColor mShapeColour;
void OnShapeChanged();
QPointF ComputeAstroid(double t);
QPointF ComputeCycloid(double t);
QPointF ComputeHuygens(double t);
QPointF ComputeHypo(double t);
QPointF Compute(double t);
QColor mBackgroundColour,mShapeColour;
ShapesType mShape;
double mScale,mIntervalLenght;
int mStepCount;
};
#endif // RENDERAREA_H