0
def redCircles():
    win = GraphWin("Patch2" ,100,100)
    for x in (10, 30, 50, 70, 90):
        for y in (10, 30, 50, 70, 90):
            c = Circle(Point(x,y), 10)
            c.setFill("red")
            c.draw(win)

这是我的代码,输出应如下所示:

在此处输入图像描述

4

2 回答 2

1

刚刚测试了这个,它对我有用。

from graphics import *

def redCircles():
    win = GraphWin("Patch2" ,100,100)
    for x in (10, 30, 50, 70, 90):
        for y in (10, 30, 50, 70, 90):
            c = Circle(Point(x,y), 10)
            d = Circle(Point(x,y), 10)
            if x in (30, 70):
                r = Rectangle(Point(x - 10, y), Point(x + 10, y + 10))                
            else:
                r = Rectangle(Point(x - 10, y- 10), Point(x, y + 10))
            c.setFill("red")
            d.setOutline("red") 
            r.setFill("white")
            r.setOutline('white')
            c.draw(win)
            r.draw(win)
            d.draw(win)

if __name__=='__main__':
    redCircles()

我们正在绘制实心圆,然后在其中一半上绘制矩形,然后绘制圆形以恢复轮廓。if 检查我们在哪一列。

于 2014-12-03T15:31:03.980 回答
0

下面是我对@JaredWindover 的解决方案的(臃肿的)修改和修改。首先,尽可能多的图形对象设置在嵌套循环之前完成,利用 Zelle 的clone()方法。其次,它修复了一个在小圆圈中很难看到的错误,即圆圈中的一半轮廓是黑色而不是红色。最后,与 Jared 的解决方案和 OP 的代码不同,它是可扩展的:

from graphics import *

RADIUS = 25

def redCircles(win):
    outline_template = Circle(Point(0, 0), RADIUS)
    outline_template.setOutline('red')

    fill_template = outline_template.clone()
    fill_template.setFill('red')

    horizontal_template = Rectangle(Point(0, 0), Point(RADIUS * 2, RADIUS))
    horizontal_template.setFill('white')
    horizontal_template.setOutline('white')

    vertical_template = Rectangle(Point(0, 0), Point(RADIUS, RADIUS * 2))
    vertical_template.setFill('white')
    vertical_template.setOutline('white')

    for parity, x in enumerate(range(RADIUS, RADIUS * 10, RADIUS * 2)):

        for y in range(RADIUS, RADIUS * 10, RADIUS * 2):

            fill = fill_template.clone()
            fill.move(x, y)
            fill.draw(win)

            if parity % 2 == 1:
                rectangle = horizontal_template.clone()
                rectangle.move(x - RADIUS, y)
            else:
                rectangle = vertical_template.clone()
                rectangle.move(x - RADIUS, y - RADIUS)

            rectangle.draw(win)

            outline = outline_template.clone()
            outline.move(x, y)
            outline.draw(win)

if __name__ == '__main__':
    win = GraphWin('Patch2', RADIUS * 10, RADIUS * 10)

    redCircles(win)

    win.getMouse()
    win.close()
于 2017-04-20T00:55:32.803 回答