0
我被要求创建绘制一个圆,2个五边形程序对准圆圈2个五边形在python: 一个圆圈,其顶点都在圆内。 另一个是圆的外侧,其边与圆相切。如何使用龟
我能画出形状相当容易,但我的问题是对准他们。当我被问到时,五角形不在圈内和圈外。
我不知道很多关于蟒蛇,我觉得这样的问题需要一定的数学知识,我没有。
该代码已附加,也是我所得到的屏幕截图。
任何帮助表示赞赏。
import random
import math
import re
import turtle
from turtle import Turtle, mainloop
import sys
class KeysMouseEvents:
def __init__(self):
super().__init__()
global arg
turtle.setup(width=800,height=500,startx=300,starty=250)
self.T=Turtle()
self.screen=self.T.getscreen()
self.T.pensize(4)
self.T.color("black")
self.screen.onclick(self.drawconcfigs)
self.screen.listen()
self.count=0
self.first=(0,0)
self.T.hideturtle()
self.T.up()
self.screen.onkey(self.T.clear,"c")
def drawconcfigs(self,x,y):
#make turtle fastest speed
self.T.speed(3)
#initialize color list
colorlist = ['red', 'green', 'blue', 'yellow', 'white', 'pink', 'brown', 'purple', 'gray', 'orange']
#assign random colors for all three shapes
outersqcolor = random.choice(colorlist)
circlecolor = random.choice(colorlist)
while circlecolor == outersqcolor:
circlecolor = random.choice(colorlist)
innersqcolor = random.choice(colorlist)
extsquarerot = random.randint(0,90)
#echo extsquarerot
intsquarerot = random.randint(0,90)
degToRads = 0.01745329251
extsquarerot *= degToRads
intsquarerot *= degToRads
while (innersqcolor == circlecolor or innersqcolor == outersqcolor):
innersqcolor = random.choice(colorlist)
#process mouse clicks
self.count = (self.count + 1)
if self.count == 1:
self.firstx=x
self.firsty=y
self.T.goto(x,y)
self.T.down()
self.T.dot()
self.T.up()
return
if self.count == 2:
#draw rectangle first
self.secondx=x
self.secondy=y
self.T.goto(x,y)
self.T.down()
self.T.dot()
self.T.up()
self.count=0
X = self.secondx - self.firstx
Y = self.secondy - self.firsty
d = X * X + Y * Y
radius = math.sqrt (d)
#L = radius
L = math.sqrt(2 * radius * radius)
#upperleft = (self.firstx - L * math.cos(extsquarerot), self.firsty + L * math.sin(extsquarerot))
side=1.4*radius*math.sin(36)
self.T.color("black", outersqcolor)
self.T.goto(self.secondx,self.secondy)
self.T.right(extsquarerot/degToRads)
#self.T.goto(upperleft)
self.T.forward(radius)
#self.T.goto(self.firstx,self.firsty)
#self.T.right(135)
self.T.begin_fill()
self.T.down()
self.T.forward(side)
self.T.left(72)
self.T.forward(side)
self.T.left(72)
self.T.forward(side)
self.T.left(72)
self.T.forward(side)
self.T.left(72)
self.T.forward(side)
self.T.up()
self.T.end_fill()
#draw circle
X = self.secondx - self.firstx
Y = self.secondy - self.firsty
d = X * X + Y * Y
radius = math.sqrt (d);
self.T.goto(self.firstx, self.firsty-radius)
self.T.color("black", circlecolor)
self.T.begin_fill()
self.T.down()
self.T.setheading(0)
self.T.circle(radius)
self.T.up()
self.T.end_fill()
#draw square inside
a = math.sqrt (radius*radius/2)
upperleft = (self.firstx-a, self.firsty+a)
side=radius*math.sin(36)
#self.T.goto(upperleft)
self.T.color("black", innersqcolor)
self.T.goto(self.firstx,self.firsty)
self.T.left(intsquarerot/degToRads)
#self.T.goto(upperleft)
self.T.forward(radius)
#self.T.goto(self.firstx,self.firsty)
self.T.right(135)
self.T.begin_fill()
self.T.down()
self.T.forward(side)
self.T.right(72)
self.T.forward(side)
self.T.right(72)
self.T.forward(side)
self.T.right(72)
self.T.forward(side)
self.T.right(72)
self.T.forward(side)
self.T.right(72)
self.T.up()
self.T.end_fill()
self.T.goto(self.firstx,self.firsty)
self.T.down()
self.T.dot()
self.T.up()
def main(self):
mainloop()
def drawconcfigs():
draw=KeysMouseEvents()
draw.main()
if __name__ == '__main__':
arg=sys.argv[0]
drawconcfigs()
内蒙古五角大楼需要圆心和半径,PREC侧面之间的角度(总是相同的角度),你只需要计算侧面宽度。其他五角大楼需要相同的中心和半径,预先确定角度之间的角度 - 您只需计算边宽。使用圆心和半径绘制圆和五边形的函数使代码更具可读性。 – furas
现在你的函数'drawconcfigs'太长了,所以没有人想读它。 – furas