2023年12月9日发(作者:那一刻我哭了)
如何用python的turtle画五角星_使用Python的turtle模块画五角
星
使用Python中的turtle模块绘制五角星
代码
# 画国旗上的五角星
import turtle
import math
import numpy as np
# 按照下面网址的国旗上五个五角星的相对位置绘制
# HEIGHT 和半径相关,是网址中矩形的高度
HEIGHT = 200
width = HEIGHT * 1.5
mid_star_radius = HEIGHT * (3/10)
small_star_radius = mid_star_radius * (1/3)
# 四个小的五角星相对于中心五角星的位置
move_vectors = [(5, 3), (7, 1), (7, -2), (5, -4)]
move_vectors = [(item[0] * small_star_radius, item[1] * small_star_radius) for item in move_vectors]
def calculate_coordinate(radius):
# 默认圆心在坐标原点
# 给半出径计算五角星顶点的坐标
RADIUS = radius
# 顶点dot0为坐标为(0, RADIUM)
dots = [None for x in range(0, 5)]
dots[0] = (0, RADIUS)
# 顶点编号dot0, dot1, dot2, dot3, dot4为顺时针顺序
# 两个点之间的夹角为
angle = 2 * / 5
# dot1与x轴正向(1, 0)的夹角
angle_dot1_1_0 = / 2 - angle
dots[1] = (RADIUS * (angle_dot1_1_0), RADIUS * (angle_dot1_1_0))
# dot4 与 dot1 关于y轴对称
dots[4] = (-dots[1][0], dots[1][1])
# dot2 与 Y轴负方向的夹角angle_dot2_0_negative_1 = - 2 * angle
dots[2] = (RADIUS * (angle_dot2_0_negative_1), - RADIUS * (angle_dot2_0_negative_1))
# dot3 与 dot2 关于y轴对称
dots[3] = (- dots[2][0], dots[2][1])
return dots
def draw_five_point_star(dots):
# 传入五个点的坐标,绘制五角星
()
(dots[0])
n()
_fill()
for i in [2, 4, 1, 3, 0]:
(dots[i])
_fill()
rtle()
def move_rotate_star(dots, x, y, angle):
# 移动和旋转五角星
move_matrix = ([[x], [y]])
# 旋转矩阵
rotation_matrix = ([[(angle), -(angle)], [(angle), (angle)]])
dots_matrix_list = [(dot).reshape(2,1) for dot in dots]
# 旋转后的坐标
rotated_dots = [(rotation_matrix, dot) for dot in dots_matrix_list]
# 根据x,y移动后的坐标
dots_new = [dot + move_matrix for dot in rotated_dots]
dots_new = [tuple(e(1,2).tolist()[0]) for dot in dots_new]
return dots_new
star_center = calculate_coordinate(mid_star_radius)
star_small = calculate_coordinate(small_star_radius)
(width*2, HEIGHT *2 )
r("red")
("yellow", "yellow")
# 画出中间的五角星
draw_five_point_star(star_center)# 计算出四个小的五角星顶点坐标后,根据上述网址的相对位置移动和旋转五角星
star_num = 1
for move_args in move_vectors:
if star_num < 3:
angle = - (move_args[0]/move_args[1])
el:
angle = - (move_args[0]/move_args[1])
star_small_move = move_rotate_star(star_small, *move_args, angle)
draw_five_point_star(star_small_move)
star_num += 1
()
# todo 按照国旗上的位置,需要将图案整体向左上角移动
结果
本文发布于:2023-12-09 03:18:10,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/zhishi/a/1702063090115655.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:如何用python的turtle画五角星_使用Python的turtle模块画五角星.doc
本文 PDF 下载地址:如何用python的turtle画五角星_使用Python的turtle模块画五角星.pdf
留言与评论(共有 0 条评论) |