I am asked to create a program that draws a circle with 2 pentagons: one inside the circle with its vertices are on the circle. the other is outside the circle with its sides tangent to the circle.
I was able to draw the shapes fairly easy, but my problem is aligning them up. Meaning the pentagons are not inside and outside the circle as I was asked.
I don't know much about python, and I feel that this kind of problem needs some math knowledge I do not have.
The code is attached and also a screen shot of what I am getting.
Any help is appreciated.
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()
Thank you, bad result
I created functions so now code is more readable and then I could create working example.
Important elements:
In pentagon()
side = 2*(radius * math.sin(math.radians(36)))
self.T.setheading(0)
self.T.right(144)
and arguments
# draw outer figure
r = radius / math.cos(math.radians(36))
self.pentagon(self.first_x, self.first_y, r, self.outer_color)
# draw circle
self.circle(self.first_x, self.first_y, radius, self.circle_color)
# draw inner figure
r = radius
self.pentagon(self.first_x, self.first_y, r, self.inner_color)
Full working code:
import random
import math
import turtle
class KeysMouseEvents:
def __init__(self):
super().__init__()
turtle.setup(width=800, height=500, startx=300, starty=250)
self.T = turtle.Turtle()
self.T.pensize(4)
self.T.color("black")
self.T.hideturtle()
self.T.up()
self.T.speed(3)
self.screen = self.T.getscreen()
self.screen.listen()
self.screen.onkey(self.T.clear, "c")
self.screen.onclick(self.on_click)
self.color_list = ['red', 'green', 'blue', 'yellow', 'white', 'pink', 'brown', 'purple', 'gray', 'orange']
#print('init')
self.reset()
self.select_colors()
def main(self):
turtle.mainloop()
def reset(self):
self.count = 0
self.first_x, self.first_y = (0,0)
self.second_x, self.second_y = (0,0)
def select_colors(self):
self.outer_color = random.choice(self.color_list)
self.circle_color = random.choice(self.color_list)
while self.circle_color == self.outer_color:
self.circle_color = random.choice(self.color_list)
self.inner_color = random.choice(self.color_list)
while self.inner_color in (self.circle_color, self.outer_color):
inner_color = random.choice(self.color_list)
def distance(self, x1, y1, x2, y2):
dx = x2 - x1
dy = y2 - y1
d = math.sqrt(dx*dx + dy*dy);
return d
def dot(self, x, y, color="black"):
self.T.goto(x, y)
self.T.color(color, color)
self.T.down()
self.T.dot()
self.T.up()
def circle(self, x, y, radius, color):
self.T.goto(x, y-radius)
self.T.color("black", color)
self.T.setheading(0)
self.T.begin_fill()
self.T.down()
self.T.circle(radius)
self.T.up()
self.T.end_fill()
def pentagon(self, x, y, radius, color):
side = 2*(radius * math.sin(math.radians(36)))
self.T.goto(x, y+radius)
self.T.color("black", color)
self.T.setheading(0)
self.T.right(144)
self.T.begin_fill()
self.T.down()
for x in range(5):
self.T.forward(side)
self.T.left(72)
self.T.up()
self.T.end_fill()
def distance(self, x1, y1, x2, y2):
dx = x2 - x1
dy = y2 - y1
d = math.sqrt(dx*dx + dy*dy);
return d
def on_click(self, x, y):
#process mouse clicks
self.count += 1
if self.count == 1:
self.first_x = x
self.first_y = y
self.dot(x,y)
elif self.count == 2:
self.second_x = x
self.second_y = y
self.dot(x, y)
# find radius
radius = self.distance(self.first_x, self.first_y, self.second_x, self.second_y)
# draw outer figure
r = radius / math.cos(math.radians(36))
self.pentagon(self.first_x, self.first_y, r, self.outer_color)
# draw circle
self.circle(self.first_x, self.first_y, radius, self.circle_color)
# draw inner figure
r = radius
self.pentagon(self.first_x, self.first_y, r, self.inner_color)
# draw points
self.dot(self.first_x, self.first_y, "red")
self.dot(self.second_x, self.second_y, "red")
# reset data
self.reset()
self.select_colors()
if __name__ == '__main__':
#draw = KeysMouseEvents()
#draw.main()
KeysMouseEvents().main()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.