3D金字塔图(2)

以前画过一个金字塔图,效果不是很好(有兴趣的话可以看看我的博客http://blog.csdn.net/chyuanrufeng/article/details/76473056)。这次这个是从另外一个角度观察的,配色自行修改。效果如下:
3D金字塔图(2)_第1张图片

核心代码:

void test1::drawPyramid3( QPainter &painter )
{
    int w = width();
    int h = height();
    int x= w/2;


    QPointF toppoint(x,h*0.2); 
    QPointF midbottompoint(x,h*0.9); 
    QPointF leftbottompoint(w*0.2,h*0.85); 
    QPointF rightbottompoint(w*0.8,h*0.85); 

    int laycount = 4;
    int hsetp = (leftbottompoint.y()-toppoint.y())/laycount;
    float wsetp = (rightbottompoint.x() - leftbottompoint.x())/2; 

    for (int i = laycount ; i > 0; --i)
    {
        float bpy = (midbottompoint.y() - toppoint.y())*i/laycount;
        float tpy = (midbottompoint.y() - toppoint.y())*0.9/laycount;
        QPointF tmpmidbp(x,toppoint.y()+bpy);  //底中间点 
        QPointF tmpmidtp(x,toppoint.y()+bpy-tpy);  //上部中间点

        float txw = wsetp*i*0.95/laycount ;
        float hlength = hsetp*i;
        QPointF tmpleftbp(x-txw, toppoint.y() + hlength);  //底部左侧点
        QPointF tmprightbp(x+txw,toppoint.y() + hlength); //底部右侧点

        txw = wsetp*(i-1)/laycount;
        //hlength = hsetp*(i-1);
        QPointF tmplefttp(x-txw,toppoint.y() + hlength-hsetp*0.9);  //上部左侧点
        QPointF tmprighttp(x+txw,toppoint.y() + hlength-hsetp*0.9); //上部右侧点

        //左侧面
        painter.save();
        QPolygonF leftpolygon;
        leftpolygon<<  tmpmidbp << tmpmidtp  << tmplefttp << tmpleftbp;

        QLinearGradient leftgradient(tmpmidbp.x(),tmpleftbp.y(),tmplefttp.x(),tmplefttp.y());
        //leftgradient.setColorAt(0,QColor("#00A8E1").lighter(120));
        leftgradient.setColorAt(0,QColor("#FF0000").lighter(120));

        leftgradient.setColorAt(0.5,QColor("#00A8E1").dark(100));
        leftgradient.setColorAt(1,QColor("#246B95"));
        painter.setBrush(leftgradient);
        painter.drawPolygon(leftpolygon);
        painter.restore();
        //右侧
        painter.save();
        QPolygonF rightpolygon;
        rightpolygon << tmpmidbp << tmpmidtp  <;
        QLinearGradient rightgradient(tmpmidbp.x(),tmpmidbp.y(),tmplefttp.x(),tmplefttp.y());
        rightgradient.setColorAt(0,QColor("#78DAFF").darker(120));
        rightgradient.setColorAt(0.5,QColor("#00A8E1").dark(100));
        rightgradient.setColorAt(1,QColor("#78DAFF"));
        painter.setBrush(rightgradient);
        painter.drawPolygon(rightpolygon);
        painter.restore();

        //顶
        painter.save();
        QPolygonF toppolygon;
        toppolygon  << tmpmidtp  <;
        QLinearGradient topgradient(tmpmidbp.x(),tmpmidbp.y(),tmplefttp.x(),tmplefttp.y());
        topgradient.setColorAt(0,QColor("#78DAFF").darker(120));
        topgradient.setColorAt(0.5,QColor("#00A8E1").dark(100));
        topgradient.setColorAt(1,QColor("#78DAFF"));
        painter.setBrush(rightgradient);
        painter.drawPolygon(toppolygon);
        painter.restore();

    }


}

你可能感兴趣的:(Qt)