1966年,Seymour Papert 和 Wally Feurzig 发明了一种专门给儿童学习编程的语言——LOGO 语言,它的特色就是通过编程指挥一个小海龟(turtle)在屏幕上绘图。海龟绘图(Turtle Graphics)后来被移植到各种高级语言中,Python内置了 turtle 库,基本上完整复制了原始 Turtle Graphics 的所有功能。

海龟绘图的特点就是以动画的形式绘制出图案,这比直接展示最终图案更有趣。

要在 Qt 中实现这种动画效果,我的思路就是通过定时器刷新当前进度,然后根据进度来决定绘制哪一部分。趁着假期无聊就写了个简易版本的,可以画直线和圆弧,以及填充路径,后面有空再完善下。

代码链接:https://github.com/gongjianbo/QtTurtle

效果展示:

调用代码:

    drawPainter.begin(QPointF(300,200));drawPainter.beginFill(QColor(0,100,0));drawPainter.setPenWidth(2);drawPainter.setPenColor(QColor(0,150,0));drawPainter.arc(QPointF(200,200),100,0,180);drawPainter.arc(QPointF(400,100),318,-161,53);drawPainter.arc(QPointF(200,100),318,-72,53);drawPainter.arc(QPointF(400,200),100,0,180);drawPainter.endFill();drawPainter.end();

核心部分就是每一步操作就 new 一个对象,绘制时遍历这些对象,遍历过程中超出当前进度就 break,等定时器 timeout 时进度会增加并再次遍历绘制。

部分代码:

#include "MyPainter.h"#include <QtMath>
#include <QDebug>MyPainter::MyPainter(QObject *parent) : QObject(parent)
{timer=new QTimer(this);connect(timer,&QTimer::timeout,this,[=]{drawLength+=10;update();//时长超过表示线条绘制完了if(drawEnd)timer->stop();});
}MyPainter::~MyPainter()
{qDeleteAll(dataList);dataList.clear();
}void MyPainter::draw(QPaintDevice *device)
{if(drawLength<=0)return;QPainter painter(device);//目前没有设置路径填充规则 TODOqint64 progress_temp=0;int i=0;for(i=0;i<dataList.count()&&progress_temp<drawLength;i++){double len=drawLength-progress_temp;AbstractElement* item=dataList[i];//判断begin和end fill,执行到endfill才开始填充begin中的路径switch (item->type()) {case 3:{const int ele_index=static_cast<BeginFillElement*>(item)->index();if(drawEndIndex>=ele_index){item->draw(&painter,0);}}break;case 4:drawEndIndex=static_cast<EndFillElement*>(item)->index();break;default:item->draw(&painter,len>item->length()?1:len/item->length());break;}//len不够就是还没画完,后面的就不画了if(len<item->length())break;progress_temp+=item->length();}drawEnd=bool(i>=dataList.count()&&progress_temp>=dataLength);
}void MyPainter::start()
{timer->start(timerInterval);drawLength=0;drawEndIndex=-1;drawEnd=false;startTime=QTime::currentTime();
}void MyPainter::begin(const QPointF &pos)
{//begin为路径规划开始,状态复位currentPos=pos;dataLength=0;dataList.clear();
}void MyPainter::end()
{//end暂无操作
}void MyPainter::setPenColor(const QColor &color)
{PenColorElement *ele=new PenColorElement(color);dataList.push_back(ele);
}void MyPainter::setPenWidth(int width)
{PenWidthElement *ele=new PenWidthElement(width);dataList.push_back(ele);
}void MyPainter::beginFill(const QColor &color)
{//开始填充后,beginElement!=null,path在别的函数判断中就能追加路径fillBeginIndex++;fillBeginPath=QPainterPath();fillBeginPath.moveTo(currentPos);fillBeginElement=new BeginFillElement(fillBeginIndex,color);dataList.push_back(fillBeginElement);
}void MyPainter::endFill()
{if(fillBeginElement){//end后路径就完成了fillBeginElement->setFillPath(fillBeginPath);EndFillElement *ele=new EndFillElement(fillBeginIndex);dataList.push_back(ele);fillBeginElement=nullptr;}
}void MyPainter::moveTo(const QPointF &pos)
{if(fillBeginElement){fillBeginPath.moveTo(pos);}currentPos=pos;
}void MyPainter::lineTo(const QPointF &pos)
{if(fillBeginElement){fillBeginPath.lineTo(pos);}LineElement *ele=new LineElement(QLineF(currentPos,pos));dataList.push_back(ele);dataLength+=ele->length();currentPos=pos;
}void MyPainter::arc(const QPointF &center, int radius, int startAngle, int spanAngle)
{QRectF ele_rect=QRectF(center.x()-radius,center.y()-radius,radius*2,radius*2);if(fillBeginElement){//const int len=std::ceil(std::abs(spanAngle)*M_PI*radius/180.0);fillBeginPath.arcTo(ele_rect,startAngle,spanAngle);}ArcElement *ele=new ArcElement(ele_rect,startAngle,spanAngle);dataList.push_back(ele);dataLength+=ele->length();//currentPos=stop;
}

Qt 实现类似 Python turtle 海龟绘图的动画效果相关推荐

  1. Python Turtle 海龟绘图详解官方文档中文版

    Python Turtle 海龟绘图详解 (官方文档中文版)-安徽省太湖中学陈晓中整理 概述 海龟绘图很适合用来引导孩子学习编程. 最初来自于 Wally Feurzeig, Seymour Pape ...

  2. python turtle 海龟绘图详解(官方文档中文版)

    源码: Lib/turtle.py 概述 海龟绘图很适合用来引导孩子学习编程. 最初来自于 Wally Feurzeig, Seymour Papert 和 Cynthia Solomon 于 196 ...

  3. 一篇文章学会 python turtle海龟绘图

    海龟绘图python2.6版本中后引入的一个简单的绘图工具,叫做海龟绘图(Turtle Graphics),出现在1966年的Logo计算机语言. 海龟绘图(turtle库)是python的内部模块, ...

  4. python turtle 海龟绘图,绘制小猪佩奇

    项目介绍: 瞎玩的,要用Python来画小猪佩奇. 其实这个实现并不难,只要使用Python的内置模块turtle进行绘图即可.但是,如要完成一个好的作品,还是需要耗费一定时间的,因为你要提前布置好所 ...

  5. 小V的母亲节礼物揭秘,Python Turtle海龟绘图

    ​一份问卷调查,期待听到您的声音 母亲节,小V同学送给他妈妈的礼物成了全场最秀!鼓掌 小V同学的妈妈收到了礼物,直言"理工小男生制造的母亲节小浪漫直击老母亲内心最柔软的部分". 母 ...

  6. python turtle 海龟绘图小恐龙(《小猪佩奇》里的)

    python零基础 turtle库绘制<小猪佩奇>里的小恐龙 python代码 #dinosaur.pyimport turtle as t#龙身 t.setup(1000,600) # ...

  7. 每日一练:Python代码绘制航海王草帽路飞,打饭阿姨也能跟着学会的Turtle海龟绘图系列

    Turtle 海龟绘图系列 - Python 代码绘制航海王草帽路飞 第一章:程序运行 ① 效果展示 - 轮廓描绘 ② 效果展示 - 颜色填充 第二章:实现过程 ① 绘图数据下载 ② 海龟绘图配置项 ...

  8. Python 代码绘制航海王四皇大妈(BIG MOM) - 门卫大爷也能跟着学会的Turtle海龟绘图系列

    Turtle 海龟绘图系列 - Python 代码绘制航海王四皇大妈 BIG MOM 第一章:程序运行 ① 效果展示1 ② 效果展示2 第二章:实现过程 ① 绘图数据下载 ② 海龟绘图配置项 ③ 本地 ...

  9. Python Turtle 海龟画图 官方文档

    turtle --- 海龟绘图 源码: Lib/turtle.py 概述 海龟绘图很适合用来引导孩子学习编程. 最初来自于 Wally Feurzeig, Seymour Papert 和 Cynth ...

  10. turtle --- 海龟绘图

    源码: Lib/turtle.py 概述 海龟绘图很适合用来引导孩子学习编程. 最初来自于 Wally Feurzeig, Seymour Papert 和 Cynthia Solomon 于 196 ...

最新文章

  1. Mysql:查询当天、今天、本周、上周、本月、上月、本季度、本年的数据
  2. 云计算之路-阿里云上:一场暴风雨的袭击
  3. AgileEAS.NET平台视频会议培训第二辑-简单插件开发应用演练(速度下载)
  4. 什么?在SAP中国研究院里还需要会PHP开发?
  5. 编译原理课程作业-Cminus语言的词法及语法分析器实现
  6. 057 生成器和生成器表达式
  7. 局域网管理工具_局域网vnc远程控制软件,七款免费又好用的局域网vnc远程控制...
  8. 人工智能顶会顶刊以及SCI,IF,核心,分区
  9. matlab 贝叶斯回归,贝叶斯向量自回归MATLAB代码 使用matlab实现贝叶斯向量自回归模型 - 下载 - 搜珍网...
  10. 代写品牌故事-品牌故事如何写才感人
  11. JS手写面试题 --- 数组扁平化
  12. activiti5.21 + SVG 绘制流程图 高亮显示已完成节点
  13. matlab 残差 dw,Eviews中的自相关检验与修正操作(一):残差图与DW检验
  14. windows 防火墙解除或禁止ping方法
  15. three.js中jsm文件夹的使用
  16. 计算机网络(一)网络分层及协议
  17. 无线连接世界 创新驱动中国 《微波射频技术》杂志发布
  18. 少儿编程入门应该从机器人Scratch编程开始
  19. 带有详细书签的IT电子书大全
  20. 2010年《杨卫华谈微博架构》视频摘抄

热门文章

  1. oracle 创建.dmp文件,oracle创建表空间和导入dmp文件命令
  2. 西安网络安全周 | 联软科技:论端点安全在零信任体系中的重要性
  3. 智课雅思短语---一、be no exception
  4. 计算机科学技术的广告语,赞美科技的句子-十大经典深入人心科技类广告语
  5. 计算机32还是64位操作系统,电脑系统32位好还是64位好 哪个快?
  6. 洛谷 P3987 我永远喜欢珂朵莉~(Splay+BIT+无限卡常)
  7. 一个封锁操作被对wsacancelblockingcall_突破封锁再进一步,华为鸿蒙OS成功登上手机...
  8. crypto405-grasshopper(网鼎杯2022)
  9. tp无线路由器设置打印服务器,tplink路由器wdr7500怎么设置usb打印服务器
  10. 32767+1=-32768 补码