C-Uebung

Musterlösung für das Bäumchen

elektronische Abgabe | | Templates

Baeumchen
void PainterWidget::paintEvent(QPaintEvent *)
{
   QPainter painter(this);

   painter.translate(width()/2, height());
   painter.rotate(180);

   drawSomething(&painter, 0);
}

void PainterWidget::drawSomething(QPainter *p, int level) {
    static const float angle = 40;
    static const float scale1 = 0.75f;
    static const float scale2 = 0.65f;

    float length = 0.3f*height();

    if (level > 18) return;

    QColor color = Qt::black;

    if (level > 14)
        color = Qt::green;
    else if (level > 3)
        color = QColor(128,64,0);

    p->setPen(QPen(QBrush(color), 40-level));

    p->drawLine(QPoint(0,0), QPoint(0,length));

    p->translate(0, length);
    p->scale(scale1, scale1);
    p->rotate(angle);

    drawSomething(p, level+1);

    p->rotate(-2*angle);
    p->scale(1/scale1, 1/scale1);
    p->scale(scale2, scale2);

    drawSomething(p, level+1);

    p->rotate(angle);
    p->scale(1/scale2, 1/scale2);
    p->translate(0, -length);
}


elektronische Abgabe | | Templates

Options: