2023年12月9日發(fā)(作者:那一刻我哭了)

如何用python的turtle畫五角星_使用Python的turtle模塊畫五角
星
使用Python中的turtle模塊繪制五角星
代碼
# 畫國旗上的五角星
import turtle
import math
import numpy as np
# 按照下面網(wǎng)址的國旗上五個五角星的相對位置繪制
# HEIGHT 和半徑相關(guān),是網(wǎng)址中矩形的高度
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 關(guān)于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 關(guān)于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):
# 移動和旋轉(zhuǎn)五角星
move_matrix = ([[x], [y]])
# 旋轉(zhuǎn)矩陣
rotation_matrix = ([[(angle), -(angle)], [(angle), (angle)]])
dots_matrix_list = [(dot).reshape(2,1) for dot in dots]
# 旋轉(zhuǎn)后的坐標
rotated_dots = [(rotation_matrix, dot) for dot in dots_matrix_list]
# 根據(jù)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)# 計算出四個小的五角星頂點坐標后,根據(jù)上述網(wǎng)址的相對位置移動和旋轉(zhuǎn)五角星
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 按照國旗上的位置,需要將圖案整體向左上角移動
結(jié)果
本文發(fā)布于:2023-12-09 03:18:10,感謝您對本站的認可!
本文鏈接:http://www.newhan.cn/zhishi/a/1702063090115655.html
版權(quán)聲明:本站內(nèi)容均來自互聯(lián)網(wǎng),僅供演示用,請勿用于商業(yè)和其他非法用途。如果侵犯了您的權(quán)益請與我們聯(lián)系,我們將在24小時內(nèi)刪除。
本文word下載地址:如何用python的turtle畫五角星_使用Python的turtle模塊畫五角星.doc
本文 PDF 下載地址:如何用python的turtle畫五角星_使用Python的turtle模塊畫五角星.pdf
| 留言與評論(共有 0 條評論) |