2016-11-15 2 views
0

Ich werde gebeten, ein Programm zu schaffen, das einen Kreis mit 2 Fünfecken zeichnet: man innerhalb des Kreises mit seinen Vertices sind auf dem Kreis. der andere ist außerhalb des Kreises mit seinen Seiten tangential zum Kreis.Wie man den Kreis mit 2 Fünfecken in Python unter Verwendung der Schildkröte ausrichtet

Ich konnte die Formen ziemlich einfach zeichnen, aber mein Problem ist es, sie auszurichten. Das heißt, die Fünfecke sind nicht innerhalb und außerhalb des Kreises, wie ich gefragt wurde.

Ich weiß nicht viel über Python, und ich fühle, dass diese Art von Problem benötigt Mathe-Kenntnisse, die ich nicht habe.

Der Code ist beigefügt und auch ein Screenshot von dem, was ich bekomme.

Jede Hilfe wird geschätzt.

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

Danke, bad result

+0

Inner Pentagon Kreismitte und Radius benötigt, prec Winkel zwischen den Seiten (immer die gleichen Winkel) und Sie müssen nur die Seitenbreite berechnen. Andere Pentagons benötigen den gleichen Mittelpunkt und Radius, vorberechnete Winkel zwischen den Seiten - Sie müssen nur die Seitenbreite berechnen. Machen Sie Funktionen, die Kreis und Fünfeck mit Kreismittelpunkt und Radius zeichnen, so wird Code besser lesbar sein. – furas

+0

jetzt ist deine Funktion 'drawconcfigs' zu lang, also will niemand sie lesen. – furas

Antwort

0

I Funktionen erstellt so jetzt Code ist besser lesbar und dann konnte ich arbeiten Beispiel erstellen.

Wichtige Elemente:

In pentagon()

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

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

und Argumente

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

Vollarbeitscode:

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

Vielen Dank. Du bist eine Legende! –

Verwandte Themen