0

I'm trying to achieve the pattern below.
Got as far as doing the first line, then I have no clue how to code the rest of the pattern.

pattern image

Here's what I've done so far:

#Timothy Shek from graphics import* #open Graph Window def main(): win = GraphWin("Example",100,100) x = 7 y = 7 radius = 5 while x<=30 : centre = Point(x,y) circle1 = Circle(centre,radius) circle1.setFill("red") circle1.draw(win) x = x+10 while x>=35 and x<=65 : centre = Point(x+5,y) circle2 = Circle(centre,radius) circle2.setFill("red") circle2.draw(win) x = x+10 print(x) while x>=67: centre = Point(x+10,y) circle1 = Circle(centre,radius) circle1.setFill("red") circle1.draw(win) x = x+10 main() 
4
  • 4
    This really isn't what is meant by design patterns. Commented Dec 1, 2015 at 21:36
  • Thanks, I've changed the title, hopefully thats better explained Commented Dec 1, 2015 at 21:37
  • 1
    You can also draw lines 3,4,6,7 and 9 correctly already. For the missing lines, just think about how you could describe it to somebody who's using pen and paper. Commented Dec 1, 2015 at 21:53
  • 1
    It might help to delegate the sub-patterns (the one white circle and the 8 red circles around it) to a separate function. Then you just need to call that function with appropriate arguments 9 times. Commented Dec 1, 2015 at 22:00

2 Answers 2

1

I got it guys, thanks Heres the solution

#Timothy Shek from graphics import* #open Graph Window def main(): win = GraphWin("Patch2" ,100,100) for x in (5, 15, 25, 40,50,60,75,85,95): for y in (5, 15, 25, 40,50,60,75,85,95): c = Circle(Point(x+2,y), 5) d = Circle(Point(x+2,y), 5) c.draw(win) d.draw(win) c.setFill("Red") d.setFill("Red") if x==15 or x==50 or x== 85: if y==15 or y==50 or y== 85: c2 = Circle(Point(x+2,y),5) c2.draw(win) c2.setFill("White") main() 
Sign up to request clarification or add additional context in comments.

1 Comment

Aren't c and d the exact same Circle?
0

While there is nothing wrong with your solution, this is a bit more performant

from graphics import * def main(): win = GraphWin("Patch2" ,100,100) coords = [5, 15, 25, 40, 50, 60, 75, 85, 95] centers = set([coords[i] for i in range(1, len(coords), 3)]) for i in xrange(len(coords)): for j in xrange(i+1): x, y = (coords[i], coords[j]) c1 = Circle(Point(x+2,y), 5) c2 = Circle(Point(y+2,x), 5) c1.draw(win) c2.draw(win) if x in centers and y in centers: c1.setFill("White") c2.setFill("White") else: c1.setFill("Red") c2.setFill("Red") main() 

Update: "Better" version

And since I got bored and I liked this problem (yes, I program when I'm bored) I made a fully parameter-ized version which you can do some fun stuff with, like.

Blue and white

Probably over your head :) But maybe you learn something from it, so I'm posting it.

from graphics import * def drawPattern(scale): # Inner method: Draw a square of circles given the top-left point def drawSquare(win, xCoord, yCoord, squareSize=30, numCircles=3, scale=1, scaleCircles=False, outer_color="Red", inner_color="White"): # Overwrite the default scaling if scale > 1: squareSize *= scale if scaleCircles: numCircles *= scale radius = squareSize/(numCircles*2) # Divide by 2 since it's the radius from math import sqrt, floor centerDiff = (2*radius) * floor(sqrt(numCircles)) # Used for drawing off-color circles # xrange uses an exclusive stop value, so go one value past to make inclusive for x in xrange(radius, squareSize+radius, radius*2): for y in xrange(squareSize-radius, x-radius, -radius*2): c1 = Circle(Point(x+xCoord+2,y+yCoord), radius) c2 = Circle(Point(y+yCoord+2,x+xCoord), radius) c1.draw(win) c2.draw(win) if (centerDiff < x < squareSize - centerDiff) and (centerDiff < y < squareSize - centerDiff): c1.setFill(inner_color) c2.setFill(inner_color) else: c1.setFill(outer_color) c2.setFill(outer_color) win = GraphWin("Patch2 (x{})".format(scale), 100*scale,100*scale) coords = [0, 35, 70] for x in coords: for y in coords: drawSquare(win, x*scale, y*scale, scale=scale) # normal (boring) version # drawSquare(win, x*scale, y*scale, scale=scale, scaleCircles=True, outer_color="Blue") # Picture version def main(): drawPattern(3) main() 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.