2017-05-28 35 views

答えて

0
になるまで、同じ中心を持つ別の円を描くように進めるが、10ピクセルより小さく、その後の円を描きます

まず、36面/セグメントの正多角形として円を近似しましょう。 半径がrのこの形状を描画するには、

  1. 各セグメントの長さ
  2. 長さを計算するために、各セグメント

間オンにする角度は、まず、2πR円周を、(我々は3.1415としてPIを近似する)必要私たち

circumference = 2 * 3.1415 * radius 

次を与え、我々は与えて、我々が近似しているセグメントの数によって、これを割る

circumference = 2 * 3.1415 * radius 
seg_lenght = circumferece/36 

ここで、セグメント間の角度差または外角が必要です。これは、単に360/N正n角形(n個の辺を有する多角形)のためのものであるので、我々は36分の360は10

を=現在のセグメント長を生成し、円を描くように関数を定義することができない。

def circle_around_point(radius): 
    circumference = 2 * 3.1415 * radius 
    seg_length = circumference/36 

    penup()   
    fd(radius) #Move from the centre to the circumference 
    right(90) #Face ready to start drawing the circle 
    pendown() 

    for i in range(36): #Draw each segment 
     fd(seg_length) 
     right(10) 

    penup() 
    right(90) #Face towards the centre of the circle 
    fd(radius) #Go back to the centre of the circle 
    right(180) #Restore original rotation 
    pendown() 

今同心円のために:@IbraheemRodriguesはあなたの問題の説明に基づいて、亀のcircle()機能を再コーディングする必要性を感じた理由

def concentric_circles(radius): 
    while radius > 0: 
     circle_around_point(radius) 
     radius -= 10 
+0

:のみ黒丸を描画し、この特定の運動に対する当社の最善の解決策です'circle()'メソッド?私はOPの質問に何も表示されていないので、追加の努力が必要です。 – cdlane

+0

@cdlane私の悪い、その機能を認識していませんでした。私が使った演習では、私自身の機能を作りました。 –

0

それは明らかではないが、我々は車輪の再発明しないことによって、彼のソリューションを簡素化することができます。

def circle_around_point(turtle, radius): 
    is_down = turtle.isdown() 

    if is_down: 
     turtle.penup() 
    turtle.forward(radius) # move from the center to the circumference 
    turtle.left(90) # face ready to start drawing the circle 
    turtle.pendown() 

    turtle.circle(radius) 

    turtle.penup() 
    turtle.right(90) # face awary from the center of the circle 
    turtle.backward(radius) # go back to the center of the circle 

    if is_down: 
     turtle.pendown() # restore original pen state 

def concentric_circles(turtle, radius): 
    for r in range(radius, 0, -10): 
     circle_around_point(turtle, r) 

circle()への鍵は、あなたが特定のポイントに円の中心を作るために、半径によって自分の位置をシフトする必要があるので、現在位置が円の端にあるということです。

しかし、この問題を解決するために、私はスタンプを描くから切り替えると、それにそれをスピードアップし、コードを簡素化するために、この方法を行う可能性があります:

import turtle 

STAMP_SIZE = 20 

radius = int(input("Please input a radius: ")) 

turtle.shape('circle') 
turtle.fillcolor('white') 

for r in range(radius, 0, -10): 
    turtle.shapesize(r * 2/STAMP_SIZE) 
    turtle.stamp() 

turtle.mainloop() 

しかし、これは、粗円を描きます小さなものを爆破だ:

enter image description here

はそれを修正するには、私は上記の二つのソリューションの間で妥協して行う可能性があります:

import turtle 

radius = int(input("Please input a radius: ")) 

turtle.penup() 
turtle.forward(radius) 
turtle.left(90) 
turtle.pendown() 

turtle.begin_poly() 
turtle.circle(radius) 
turtle.penup() 
turtle.end_poly() 

turtle.addshape('round', turtle.get_poly()) # 'circle' is already taken 

turtle.right(90) 
turtle.backward(radius) 

turtle.shape('round') 
turtle.fillcolor('white') 

for r in range(radius - 10, 0, -10): 
    turtle.shapesize(r/radius) 
    turtle.stamp() 

turtle.mainloop() 

これは、大規模なものを縮小する代わりに、小さなものを拡大して円の質を向上させます。

円の質がcircle()への呼び出しにsteps=引数を使用して制御することができ

enter image description here

しかし、高品質とスピードの高速を維持しながら、私は本当にコードを最小限にしたい場合は、私が行う可能性があります:

import turtle 

radius = int(input("Please input a radius: ")) 

for diameter in range(radius * 2, 0, -20): 
    turtle.dot(diameter, 'black') 
    turtle.dot(diameter - 2, 'white') 

turtle.hideturtle() 

turtle.mainloop() 

dot()方法ではなく、半径の直径を使用して、中央の代わりに、端から引きます、 「なぜあなたは亀を改革でした

enter image description here

-1
import turtle 

####  ##### #### Below class draws concentric circles. 

class Circle: 

    def __init__(self, pen, cx, cy, radius): 
     self.pen = pen 
     self.cx = cx 
     self.cy = cy 
     self.radius = radius 

    def drawCircle(self): 
     self.pen.up() 
     self.pen.setposition(self.cx, self.cy - self.radius) 
     self.pen.down() 
     self.pen.circle(self.radius) 

    def drawConCircle(self, minRadius = 10, delta = 10): 
     if(self.radius > minRadius) : 
      self.drawCircle() 
      self.radius -= delta # reduce radius of next circle 
      self.drawConCircle() 
#### End class circle ####### 

win = turtle.Screen() 
win.bgcolor("white") 
s = Circle(turtle.Turtle(), 0, 0, 200) 
s.drawConCircle() 
win.exitonclick() 
関連する問題