Reputation: 13
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)
This is my code and the output should look like this:
Upvotes: 0
Views: 204
Reputation: 41872
Below is my (bloated) rework of @JaredWindover's solution with modifications. First, as much graphic object setup as possible is done before the nested loops, taking advantage of Zelle's clone()
method. Second, it fixes a bug, hard to see in the small, where half the outline in the circles is black instead of red. Finally, unlike Jared's solution and the OP's code, it is scalable:
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()
Upvotes: 0
Reputation: 561
Just tested this and it works for me.
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()
We're drawing filled circles, then rectangles over half of them, and then outlined circles to get the outlines back. The if checks which column we're in.
Upvotes: 1