2016-10-30 3 views
0

So ist mein Programm entwickelt, um bestimmte Koordinaten zu manipulieren, um dieses Bild zu schaffen: ![imageeine Steigung in zufälligen Kreisen Python Zeichnung

mein Programm Also im Grunde eine Reihe von zufälligen Kreisen ziehen, und ich habe die Linie zu manipulieren Gleichung, um die roten Abschnitte zu erstellen. Bis jetzt ist mein Bild das folgende:

enter image description here Ich kann nicht scheinen, herauszufinden, wie man eine andere Liniengleichung hinzufügt, um den anderen roten Abschnitt zu schaffen. Jede Hilfe würde sehr geschätzt werden!

# 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

Woher kommt die SimpleGraphics-Bibliothek? Wie bekommen wir eine Kopie? – martineau

Antwort

0

Sie sind fast da. Fügen Sie die kommentierten Zeilen für Ihre zweite Steigung und Gleichung hinzu und stellen Sie sicher, dass Ihre Variablennamen übereinstimmen. Dann müssen Sie nur eine ODER-Bedingung für Ihre if-Anweisung hinzufügen, um die Farbe basierend auf jeder Gleichung festzulegen.

# 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

So habe ich meine Kommentare bearbeitet, aber ich bekomme immer noch nicht das Bild, das ich will –

+0

@ JaneDoe2 Ich habe meine Antwort bearbeitet, um ein Arbeitsbeispiel aufzunehmen - Sie können es ändern, indem Sie die Gleichungen ändern, um das gewünschte Bild zu erhalten. – Daniel