Python3使用turtle绘制超立方体图形示例 本文实例讲述了Python3使用turtle绘制超立方体图形。分享给大家供大家参考,具体如下: 利用Python3中turtle的绘制超立方体。 绘图思路: 1)求出边长100的超立方体的点坐标; 以竖直线为依据,将点分为上下两组: a为上边点列表,b为下边点列表: a = [[120.71, 50], [50, 120.71], [-50, 120.71], [-120.71, 50], [-50, -20.71], [50, -20.71], [20.71, 50],[-20.71, 50]] b = [[120.71, -50], [50, 20.71], [-50, 20.71], [-120.71, -50], [-50, -120.71], [50, -120.71], [20.71, -50],[-20.71, -50]] 2)编写绘制直线函数; 3)编写主程序:绘制点,绘制六角形直线,绘制竖直直线,绘制斜线 代码片段如下,可能不是最简洁的,如有好的建议,请不吝指正。 import turtle # 创建绘制直线函数 def drawLine(p1, p2, size=3, color="black"): turtle.penup() turtle.goto(p1) turtle.pensize(size) turtle.pencolor(color) turtle.pendown() turtle.goto(p2) def main(): # 求取点后,将超立方体点分为上下两个部分,两个列表 a = [[120.71, 50], [50, 120.71], [-50, 120.71], [-120.71, 50], [-50, -20.71], [50, -20.71], [20.71, 50], [-20.71, 50]] b = [[120.71, -50], [50, 20.71], [-50, 20.71], [-120.71, -50], [-50, -120.71], [50, -120.71], [20.71, -50], [-20.71, -50]] # 绘制点 turtle.pencolor("red") turtle.penup() for i in range(len(a)): turtle.goto(a[i]) turtle.down() turtle.dot(10, "red") turtle.penup() for i in range(len(b)): turtle.goto(b[i]) turtle.down() turtle.dot(10, "red") turtle.penup() # 绘制六边形直线 for i in range(6): if i <= 4: drawLine(a[i], a[i + 1]) drawLine(b[i], b[i + 1]) else: drawLine(a[i], a[0]) drawLine(b[i], b[0]) # 绘制竖直线 for i in range(len(a)): drawLine(a[i], b[i]) # 绘制斜线 drawLine(a[6], a[0]) drawLine(a[6], a[2]) drawLine(a[6], a[4]) drawLine(a[7], a[1]) drawLine(a[7], a[3]) drawLine(a[7], a[5]) drawLine(b[6], b[0]) drawLine(b[6], b[2]) drawLine(b[6], b[4]) drawLine(b[7], b[1]) drawLine(b[7], b[3]) drawLine(b[7], b[5]) turtle.done() if __name__ == '__main__': main() pass 运行结果: 注:使用eclipse+pyDev结合Python3环境开发时,会提示:Undefined variable from import: penup及Undefined variable from import: goto等错误,无需理会直接运行仍可绘制出该图形。 更多关于Python相关内容可查看本站专题:《Python数学运算技巧总结》、《Python图片操作技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程》 希望本文所述对大家Python程序设计有所帮助。