在随机圆圈python中绘制一个斜率

时间:2016-10-30 15:52:06

标签: python python-3.x random graphics

所以我的程序设计用于操作某些坐标以创建此图像: ![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()

1 个答案:

答案 0 :(得分: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()