我需要通过调整高度条件来制作一个圆圈,这个程序使用了很多随机圆圈,但我不确定从哪里开始?我试图使用以下等式d =(sqrt)((x1-x2)^ 2 +(y1-y2)^ 2)。现在该程序绘制了许多随机圆圈,因此调整公式我应该能够操纵它,以便某些圆圈在中心是红色的(如日本国旗)。
# 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 :(得分: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)