2014-12-03 49 views
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) 

这是我的代码和输出应该是这样的:我怎样才能让我的圈子变成白色?

enter image description here

+0

而不是使用'Circle'的,创建两个'Polygon'对象每个代表半圈,并填写一个红色和其他与白。或者,您可以用红色填充整个“圆形”,然后创建一个白色的“多边形”对象,表示您想要填充白色的一半。 – martineau 2014-12-03 15:24:47

+0

@martineau这是一个任务,[这里是最近的一个上一个问题](http://stackoverflow.com/q/27137304/2749397) – gboffi 2014-12-03 15:29:15

+0

@gboffi,我应该删除我的答案吗?我刚刚看到您的评论。 – 2014-12-03 15:31:50

回答

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检查我们所在的列。

+0

谢谢你,我已经在python(y) – 2014-12-03 19:24:29

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()