2016-11-15 47 views
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() 

謝謝bad result

+0

內蒙古五角大樓需要圓心和半徑,PREC側面之間的角度(總是相同的角度),你只需要計算側面寬度。其他五角大樓需要相同的中心和半徑,預先確定角度之間的角度 - 您只需計算邊寬。使用圓心和半徑繪製圓和五邊形的函數使代碼更具可讀性。 – furas

+0

現在你的函數'drawconcfigs'太長了,所以沒有人想讀它。 – furas

回答

0

我創建功能,因此現在的代碼是更具可讀性,然後我可以創建工作示例。

重要內容:

pentagon()

side = 2*(radius * math.sin(math.radians(36))) 

self.T.setheading(0) 
self.T.right(144) 

和參數

# 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) 

全部工作代碼:

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() 
+0

非常感謝。你是一個傳奇! –