2016-10-11 2 views
2

높은 곳에서 조건을 조정하여 서클을 만들 필요가 있습니다.이 프로그램은 무작위 서클을 많이 사용하지만 여기에서 어디로 가야할지 확실하지 않습니다. 다음 방정식 d = (sqrt) ((x1-x2)^2 + (y1 - y2)^2)을 사용하려고합니다. 지금 프로그램은 많은 무작위 원형을 그리기 때문에 수식을 조정하면 특정 원이 중앙에 빨간색으로 표시되도록 조작 할 수 있어야합니다 (예 : japan flag).무작위 서클 SimpleGraphics를 사용하여

# using the SimpleGraphics library 
from SimpleGraphics import * 

# use the random library to generate random numbers 
import random 

diameter = 15 


## 
# returns a valid colour based on the input coordinates 
# 
# @param x is an x-coordinate 
# @param y is a y-coordinate 
# @return a colour based on the input x,y values for the given flag 
## 
def define_colour(x,y): 
## 

if y < (((2.5 - 0)**2) + ((-0.5 - 0)**2)**(1/2)): 
c = 'red' 
else: 
c = 'white' 

return c 


return None 


# repeat until window is closed 
while not closed(): 

# generate random x and y values 
x = random.randint(0, getWidth()) 
y = random.randint(0, getHeight()) 

# set colour for current circle 
setFill(define_colour(x,y)) 

# draw the current circle 
ellipse(x, y, diameter, diameter) 
+0

'define_colour (x, y)'함수는별로 의미가 없습니다. 당신이해야 할 일을 설명해 주시겠습니까? 말로 설명 할 수없는 경우 몇 가지 일반적인 입력과 각 입력에 대한 예상 출력을 제공하십시오. –

+0

@ PM2Ring 중간과 흰색 바탕에 빨간색 동그라미가 있도록 만들어야합니다. 이 프로그램은 현재 많은 임의의 원을 그립니다 –

+0

당신이하려는 일은 아직 명확하지 않습니다. 화면의 중앙에 빨간 원을 하나만 그려야한다고 말하는 것입니까? 그렇다면 왜 무작위로 무작위로 서클을 그리는 루프가 있습니까? –

답변

0

여기 서클을 끝없이 그립니다. 화면 가운데에있는 원은 빨간색으로 그려지고 다른 모든 원은 흰색으로 그려집니다. 결국, 이것은 내부의 빨간색 "원형"의 가장자리가 부드럽 지 않지만 일본의 국기와 비슷한 이미지를 만듭니다.

SimpleGraphics 모듈이 없기 때문에이 코드를 테스트하지 않았으며 Google이나 pip를 통해 찾기가 쉽지 않습니다.

from SimpleGraphics import * 
import random 

diameter = 15 

width, height = getWidth(), getHeight() 
cx, cy = width // 2, height // 2 

# Adjust the multiplier (10) to control the size of the central red portion 
min_dist_squared = (10 * diameter) ** 2 

def define_colour(x, y): 
    #Calculate distance squared from screen centre 
    r2 = (x - cx) ** 2 + (y - cy) ** 2 
    if r2 <= min_dist_squared: 
     return 'red' 
    else: 
     return 'white' 

# repeat until window is closed 
while not closed(): 
    # generate random x and y values 
    x = random.randrange(0, width) 
    y = random.randrange(0, height) 

    # set colour for current circle 
    setFill(define_colour(x, y)) 

    # draw the current circle 
    ellipse(x, y, diameter, diameter) 
+0

감사합니다! 그것은 작동합니다! 원을 더 크게 만드는 방법에 대한 아이디어가 있습니까? –

+0

신경 쓰지 마세요! 나는 그것을 알아 냈어! –