本文实例讲述了Python3使用turtle绘制超立方体图形。分享给大家供大家参考,具体如下:
利用Python3中turtle的绘制超立方体。
绘图思路:
1)求出边长100的超立方体的点坐标;
以竖直线为依据,将点分为上下两组:
a为上边点列表,b为下边点列表:
1
2
|
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)编写主程序:绘制点,绘制六角形直线,绘制竖直直线,绘制斜线
代码片段如下,可能不是最简洁的,如有好的建议,请不吝指正。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
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程序设计有所帮助。
原文链接:https://blog.csdn.net/cccshare/article/details/78170322