2016-10-30 6 views
0

그래서 내 프로그램이 이미지를 생성하기 위해 특정 좌표를 조작 할 수 있도록 설계되었습니다 : ![image임의의 원 파이썬의 기울기를 그리기

그러니까 기본적으로 내 프로그램이 임의의 원의 무리를 그리고 난의 라인을 조작해야 방정식을 사용하여 빨간색 섹션을 만듭니다. 지금까지 내 이미지는 다음과 같습니다.

enter image description here 다른 빨간색 수식을 추가하여 다른 빨간색 섹션을 만드는 방법을 생각할 수 없습니다. 어떤 도움이라도 대단히 감사하겠습니다!

# using the SimpleGraphics library 
from SimpleGraphics import * 

# tell SimpleGraphics to only draw when I use the update() function 
setAutoUpdate(False) 


# use the random library to generate random numbers 
import random 


# size of the circles drawn 
diameter = 15 

resize(600, 400) 


## 
# returns a vaid color 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): 
    ## 
    #add your code to this method and change the return value 
    slopeOne = (200 - 300)/(0-150) 
    b = 0 - (slopeOne * 200) 
    slopeTwo = (0-300)/(200 - 800) 
    b = 150 - (slopeTwo * 40) 



    lineEquationOne = (slopeOne * x) + b 
    lineEquationTwo = (slopeTwo * x) + b 

    if y > lineEquationOne: 
    return "red" 
    elif y > lineEquationTwo: 
    return "red" 
    else: 
    return 'white' 





###################################################################### 
# 
# Do NOT change anything below this line 
# 
###################################################################### 

# repeat until window is closed 
while not closed(): 
    for i in range(500): 
    # 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) 

    update() 
+0

SimpleGraphics 라이브러리의 출처는 어디입니까? 우리는 어떻게 사본을 얻을 수 있습니까? – martineau

답변

0

거의 다 왔어. 두 번째 기울기와 방정식에 대한 주석 달린 행을 다시 추가하고 변수 이름이 일치하는지 확인하십시오. 그런 다음 if 문에 대한 OR 조건을 추가하여 각 방정식에 따라 색상을 설정하면됩니다.

# using the SimpleGraphics library 
from SimpleGraphics import * 

# tell SimpleGraphics to only draw when I use the update() function 
setAutoUpdate(False) 

# use the random library to generate random numbers 
import random 

# size of the circles drawn 
diameter = 15 

resize(600, 400) 


## 
# returns a valid color 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): 
    slopeOne = (200 - 300)/(0 - 150) 
    b = 0 - (slopeOne * 200) 
    slopeTwo = (200 - 300)/(0 - 150) 
    b2 = -50 - (slopeTwo * 200) 

    lineEquationOne = (slopeOne * x) + b 
    lineEquationTwo = (slopeTwo * x) + b2 

    if (y > lineEquationOne) | (y < lineEquationTwo): 
     return "white" 
    else: 
     return 'red' 


###################################################################### 
# 
# Do NOT change anything below this line 
# 
###################################################################### 

# repeat until window is closed 
while not closed(): 
    for i in range(500): 
     # 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) 

    update() 
+0

그래서 내 의견을 편집했지만 원하는 이미지를 얻지 못했습니다. –

+0

@ JaneDoe2 작업 예제가 포함되도록 내 대답을 편집했습니다. 원하는 이미지를 얻기 위해 방정식을 변경하여 수정할 수 있습니다. – Daniel