首页 - 趣闻中心 - 七台河天气预报,小说阅读器,鸡腿-开市代码,开源代码,汇聚各大框架,技术分享,时事分享

七台河天气预报,小说阅读器,鸡腿-开市代码,开源代码,汇聚各大框架,技术分享,时事分享

发布时间:2019-05-21  分类:趣闻中心  作者:admin  浏览:139

前语

前次发了个纯painter制作的老鼠,那个便是qt目录下的demo,改的,仅仅比demo中的老鼠稍微胖一点,估量人到中年都发福吧。这次来一个魔法小鱼,这条鱼能够改换色彩,尾巴还会摇摆,能够设定旋转的视点以及尾巴摇摆的起伏等,原理是参阅网上一个安卓大神写的(制作原理 https://www.jianshu.com/p/3dd3d1524851)。

其实在Qt学习过程中,假如越到问题找不到相关文章和答案,能够试着将关键字改成安卓试试,你会发现别的一篇六合,许多的资源和文章介绍等,就比方安卓顶用的java的painter,就简直和Qt中的相同,估量填写编程言语都很相似,连办法姓名简直都是相同,设置参数,具有许多通用性,作为一名程序员,最重要的是了解思路和原理,乃至学习的办法,这些把握了,任何言语都不是问题。

完成的功用

* 制作原理 https://www.jianshu.com/p/3dd3d1524851

* 1:可设置鱼头+鱼身+鱼鳍+鱼尾的色彩

* 2:可设置鱼头+鱼身+鱼鳍+鱼尾的份额

* 3:可设置基准色彩,作为一切一致色彩

* 4:可设置鱼鳍是否摇摆

* 5:可设置鱼的逗留方位旋转视点

效果图

中心代码

void MagicFish::paintEvent(QPaintEvent *)
{
//制作准备工作,启用反锯齿
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
QPointF middlePos = QPointF(width() / 2, height() / 2);
headPos = calcPoint(middlePos, bodyLen / 1.5, currentAngle);
double angle = currentAngle + qSin(qDegreesToRadians(currentValue * 1.2 * wave)) * 2;
QPointF pos = calcPoint(headPos, bodyLen, angle - 180);
//制作头部
drawHead(&painter);
//制作鱼身
drawBody(&painter, pos, angle);
//制作左边鱼鳍
QPointF leftPos = calcPoint(headPos, headLen * 0.9, angle + 110);
drawFin(&painter, leftPos, true, angle);
//制作右侧鱼鳍
QPointF rightPos = calcPoint(headPos, headLen * 0.9, angle - 110);
drawFin(&painter, rightPos, false, angle);
//制作鱼尾
drawTail(&painter, pos, angle);
}
void MagicFish::drawHead(QPainter *painter)
{
painter->save();
painter->setPen(Qt::NoPen);
painter->setBrush(headColor);
//鱼头关节圆
painter->drawEllipse(headPos, headLen, headLen);
painter->restore();
}
void MagicFish::drawBody(QPainter *painter, const QPointF &pos, double angle)
{
//核算身体部分四个点
QPointF pos1 = calcPoint(headPos, headLen, angle - 80);
QPointF pos2 = calcPoint(pos, headLen * 0.8, angle - 90);
QPointF pos3 = calcPoint(pos, headLen * 0.8, angle + 90);
QPointF pos4 = calcPoint(headPos, headLen, angle + 80);
QPointF leftPos = calcPoint(headPos, bodyLen * 0.56, angle - 130);
QPointF rightPos = calcPoint(headPos, bodyLen * 0.56, angle + 130);
//核算制作途径
QPainterPath path;
path.moveTo(pos1);
path.quadTo(leftPos, pos2);
path.lineTo(pos3);
path.quadTo(rightPos, pos4);
path.lineTo(pos1);
painter->save();
painter->setPen(Qt::NoPen);
painter->setBrush(bodyColor);
painter->drawPath(path);
painter->restore();
}
void MagicFish::drawFin(QPainter *painter, const QPointF &pos, bool left, double angle)
{
double controlAngle = 115;
double finAngle = finMove ? qSin(qDegreesToRadians(currentValue * 16.1 * wave)) * 12.0 : 2;
QPointF endPos = calcPoint(pos, finLen, left ? angle + finAngle + 180 : angle - finAngle - 180);
QPointF controlPos = calcPoint(pos, finLen * 1.8, left ? angle + controlAngle + finAngle : angle - controlAngle - finAngle);
//核算鱼鳍的途径
QPainterPath path;
path.moveTo(pos);
path.quadTo(controlPos, endPos);
path.lineTo(pos);
painter->save();
painter->setPen(Qt::NoPen);
painter->setBrush(finColor);
painter->drawPath(path);
painter->restore();
}
void MagicFish::drawTail(QPainter *painter, const QPointF &pos, double angle)
{
double flag = 0.6;
double length = tailLen * (flag + 1);
double tailAngle = angle + qCos(qDegreesToRadians(currentValue * 1.5 * wave)) * 15;
QPointF endPos = calcPoint(pos, length, tailAngle - 180);
QPointF pos1 = calcPoint(pos, tailLen, tailAngle - 90);
QPointF pos2 = calcPoint(endPos, tailLen * flag, tailAngle - 90);
QPointF pos3 = calcPoint(endPos, tailLen * flag, tailAngle + 90);
QPointF pos4 = calcPoint(pos, tailLen, tailAngle + 90);
QPainterPath path;
path.moveTo(pos1);
path.lineTo(pos2);
path.lineTo(pos3);
path.lineTo(pos4);
painter->save();
painter->setPen(Qt::NoPen);
painter->setBrush(tailColor);
//鱼尾关节大圆
painter->drawEllipse(pos, tailLen, tailLen);
//鱼尾关节小圆
painter->drawEllipse(endPos, tailLen * flag, tailLen * flag);
//鱼尾肉部分途径
painter->drawPath(path);
painter->restore();
//制作鱼尾关节
drawTail1(painter, endPos, tailAngle);
}
void MagicFish::drawTail1(QPainter *painter, const QPointF &pos, double angle)
{
double len = tailLen * 0.6;
double flag = 0.4;
double length = len * (flag + 2.7);
double tailAngle = angle + qSin(qDegreesToRadians(currentValue * 1.7 * wave)) * 35;
QPointF endPos = calcPoint(pos, length, tailAngle - 180);
QPointF pos1 = calcPoint(pos, len, tailAngle - 90);
QPointF pos2 = calcPoint(endPos, len * flag, tailAngle - 90);
QPointF pos3 = calcPoint(endPos, len * flag, tailAngle + 90);
QPointF pos4 = calcPoint(pos, len, tailAngle + 90);
QPainterPath path;
path.moveTo(pos1);
path.lineTo(pos2);
path.lineTo(pos3);
path.lineTo(pos4);
painter->save();
painter->setPen(Qt::NoPen);
painter->setBrush(tailColor);
painter->drawPath(path);
painter->restore();
//制作鱼尾鱼鳍
drawTail2(painter, pos, tailAngle);
}
void MagicFish::drawTail2(QPainter *painter, const QPointF &pos, double angle)
{
double len = tailLen * 0.6;
double flag = 0.4;
double length = len * (flag + 2.7);
double tailWidth = qAbs(qSin(qDegreesToRadians(currentValue * 1.9 * wave)) * len + headLen / 5.0 * 3.0);
QPointF endPos1 = calcPoint(pos, length, angle - 180);
QPointF endPos2 = calcPoint(pos, length - 10, angle - 180);
QPointF pos1 = calcPoint(endPos1, tailWidth, angle - 90);
QPointF pos2 = calcPoint(endPos1, tailWidth, angle + 90);
QPointF pos3 = calcPoint(endPos2, tailWidth - headLen / 1.5, angle - 90);
QPointF pos4 = calcPoint(endPos2, tailWidth - headLen / 1.5, angle + 90);
QPainterPath path1;
path1.moveTo(pos);
path1.lineTo(pos3);
path1.lineTo(pos4);
path1.lineTo(pos);
QPainterPath path2;
path2.moveTo(pos);
path2.lineTo(pos1);
path2.lineTo(pos2);
path2.lineTo(pos);
painter->save();
painter->setPen(Qt::NoPen);
painter->setBrush(tailColor);
painter->drawPath(path1);
painter->drawPath(path2);
painter->restore();
}
QPointF MagicFish::calcPoint(const QPointF &pos, double len, double angle)
{
double x = qCos(qDegreesToRadians(angle)) * len;
double y = qSin(qDegreesToRadians(angle - 180)) * len;
return QPointF(pos + QPointF(x, y));
}

下一篇
快捷导航
最新发布
标签列表