python - 如何使用turtle.gotto打印对称六边形
问题描述
我的任务是在一条线上打印对称六边形。
我必须创建一个这样的函数:def hexagone(point, longueur,c):
其中点是 X 轴和 Y 轴。“longueur”是六边形的中心和他的任何一个角之间的距离。我确实使用这个距离来计算和获取我的 turtle.gotto() 的每个 x / y 坐标。“C”代表六边形每个部分的颜色。
到目前为止,我可以在点 0,0 完美地绘制六边形:
import turtle
from math import pi, sin, cos
def hexagone(point, longueur,c):
l= point[0] + longueur
turtle.up()
turtle.goto(point)
turtle.color(c[0]) #black
turtle.down()
turtle.begin_fill()
turtle.goto(l * cos(4 / 3 * pi), l * sin(4 / 3 * pi))
turtle.goto(l * cos(5 / 3 * pi), l * sin(5 / 3 * pi))
turtle.goto(l * cos(0), l * sin(0))
turtle.goto(point)
turtle.end_fill()
turtle.color(c[1]) #blue
turtle.begin_fill()
turtle.goto(l * cos(0), l * sin(0))
turtle.goto(l * cos(pi / 3), l * sin(pi / 3))
turtle.goto(l * cos(pi * 2 / 3), l * sin(pi * 2 / 3))
turtle.goto(point)
turtle.end_fill()
turtle.color(c[2]) #red
turtle.begin_fill()
turtle.goto(l * cos(pi * 2 / 3), l * sin(pi * 2 / 3))
turtle.goto(-l, point[1])
turtle.goto(l * cos(4 / 3 * pi), l * sin(4 / 3 * pi))
turtle.goto(point)
turtle.end_fill()
turtle.up()
return True
hexagone((0,0), 50, ("black",("blue"),("red")))
turtle.done()
第一个六边形是完美的。我在 origin(0,0) 打印它。但是,如果我尝试简单地将 point[0]->(x) 增加,假设 100 : i += 100hexagone((i,0), 50, ("black",("blue"),("red")))
它在六边形上打印六边形并且变得越来越大。我的公式明显有问题,并且缩放不正确。有谁知道我怎样才能完成这项工作以存档示例中的结果?
解决方案
如果你想显示,(100,50)
那么你必须使用(0,0)
所有计算,并且在计算之后你必须添加100
到你使用的x
每50
一个- 所以你创建它然后你移动它y
goto()
(0,0)
(100,50)
在代码中我使用0
而不是point[0]
并且point[1]
因为我计算(0,0)
后来我得到x,y
了point
(这将是100
and 50)
and 用于每个goto()
goto(... +x, ... +y)
完整代码
import turtle
from math import pi, sin, cos
def hexagone(point, longueur, c):
l = 0 + longueur # 0 instead of point[0]
x, y = point
turtle.up()
turtle.goto(point)
turtle.color(c[0]) #black
turtle.down()
turtle.begin_fill()
turtle.goto(l * cos(4 / 3 * pi )+x, l * sin(4 / 3 * pi)+y)
turtle.goto(l * cos(5 / 3 * pi)+x, l * sin(5 / 3 * pi)+y)
turtle.goto(l * cos(0)+x, l * sin(0)+y)
turtle.goto(point)
turtle.end_fill()
turtle.color(c[1]) #blue
turtle.begin_fill()
turtle.goto(l * cos(0)+x, l * sin(0)+y)
turtle.goto(l * cos(pi / 3)+x, l * sin(pi / 3)+y)
turtle.goto(l * cos(pi * 2 / 3)+x, l * sin(pi * 2 / 3)+y)
turtle.goto(point)
turtle.end_fill()
turtle.color(c[2]) #red
turtle.begin_fill()
turtle.goto(l * cos(pi * 2 / 3)+x, l * sin(pi * 2 / 3)+y)
turtle.goto(-l+x, 0+y) # 0 instead of point[1]
turtle.goto(l * cos(4 / 3 * pi)+x, l * sin(4 / 3 * pi)+y)
turtle.goto(point)
turtle.end_fill()
turtle.up()
return True
hexagone((0,0), 50, ("black",("blue"),("red")))
hexagone((100,0), 50, ("black",("blue"),("red")))
hexagone((0,100), 50, ("black",("blue"),("red")))
hexagone((100,100), 50, ("black",("blue"),("red")))
turtle.done()
推荐阅读
- python-3.x - ImportError:无法导入名称“extract_zipped_paths”
- python - 如何使用 google api 和 python 在 google docs 中打开文件?
- reactjs - 使用 axios 仅适用于邮递员
- clojure - Recursively calling function with specific keyword in Clojure
- ruby - 如何在给定全名数组的情况下有效地生成名字和姓氏的唯一组合
- postgresql - 如何在 Postgresql 中获取当前月份的字符串和年份
- matrix - 如何沿主对角线使用渐变格式填充 SSRS 中的矩阵?
- android - Firebase 身份验证:无法验证不再列入测试白名单的手机号码
- javascript - 模拟导入的 Lazy React 组件
- pentaho - 表输入中的参数 INTEGER