2016-05-20 6 views
2

入力(半径)に従ってオリンピックリングが描かれているプログラムを入力しています。私の質問は、入力を与えられたら、それに応じて位置座標(x、y)をどのように取得するのですか?サークルの半径(Python)を指定して位置座標を調整しようとしています

私はすべてのマッチアップにそれらのために持っていたデフォルトの半径70は70

を入力している。ここに私のコードです:

radius =input('Enter the radius of your circle: ') #Asking for radius of the circle 
r = float(radius) 

import turtle 
t = turtle.Turtle 
t = turtle.Turtle(shape="turtle") 

#Circle one 
t.pensize(10) 
t.penup() 
t.goto(0,0) 
t.pendown() 
t.color("green") #Adds Green 
t.circle(r) 
#Circle two 
t.penup() 
t.setposition(-160,0) 
t.pendown() 
t.color("yellow") #Adds yellow 
t.circle(r) 
#Circle three 
t.penup() 
t.setposition(110,60) 
t.pendown() 
t.color("red") #Adds red 
t.circle(r) 
#Circle four 
t.penup() 
t.setposition(-70,60) 
t.pendown() 
t.color("black") #Adds black 
t.circle(r) 
#Circle five 
t.penup() 
t.setposition(-240,60) 
t.pendown() 
t.color("blue") #Adds blue 
t.circle(r) 

答えて

1

方法に応じて座標をスケーリングについては?

コード -

radius =input('Enter the radius of your circle: ') #Asking for radius of the circle 
r = float(radius) 

x = r/70 

import turtle 
t = turtle.Turtle 
t = turtle.Turtle(shape="turtle") 

#Circle one 
t.pensize(10) 
t.penup() 
t.goto(0,0) 
t.pendown() 
t.color("green") #Adds Green 
t.circle(r) 
#Circle two 
t.penup() 
t.setposition(-160 * x,0) 
t.pendown() 
t.color("yellow") #Adds yellow 
t.circle(r) 
#Circle three 
t.penup() 
t.setposition(110 * x,60 * x) 
t.pendown() 
t.color("red") #Adds red 
t.circle(r) 
#Circle four 
t.penup() 
t.setposition(-70 * x,60 * x) 
t.pendown() 
t.color("black") #Adds black 
t.circle(r) 
#Circle five 
t.penup() 
t.setposition(-240 * x,60 * x) 
t.pendown() 
t.color("blue") #Adds blue 
t.circle(r) 

また、入力に応じてpensizeを拡張することができます。

+0

助けを借りてくれてありがとう。私は今それをやる方法を理解しています。 – MLW

0

必要なのは乗算器です。あなたのコードは実際には反復的ですが、このようなことがあるときは、たくさんのコマンドの代わりにループを使いたいときは、コードを簡単に保守して読みやすくします。これは私のアプローチのようになります。

radius =input('Enter the radius of your circle: ') #Asking for radius of the 

circle 
r = float(radius) 

import turtle 
t = turtle.Turtle(shape="turtle") 

m = r/70 

colors = ["green", "yellow", "red", "black", "blue"] 
coordinates = [(1,1), (-160,0), (110,60), (-70,60), (-240,60)] 

t.pensize(10) 

for i in range(len(coordinates)): 
    t.penup() 
    t.goto(coordinates[i][0]*m, coordinates[i][1]*m) 
    t.pendown() 
    t.color(colors[i]) 
    t.circle(r) 

ポイントを正規化定数によってそれらを掛けるためにも、より良く、より柔軟になります。

関連する問題