2016-10-30 73 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