Hi,

I am trying to create some patterns in python. I am really struggling as to be quite fair, I don't have a clue what I am doing

these are the 3 patterns I am trying to create. any help would be greatly appreciated

thank you

this is some code I have so far although it has an entirely different pattern to what I need. it also has to have graphics import and range

def main():
    colour = raw_input("Enter the patch colour: ")
    win = GraphWin("Patch", 200, 200)
    drawPatch(win, 50, 50, colour)

def drawPatch(win, x, y, colour):
    for i in range(5):
        for j in range(5):
            if (i + j) % 2 == 0:
                topLeftX = x + i * 20
                topLeftY = y + j * 20
                rectangle = Rectangle(Point(topLeftX, topLeftY),
                                      Point(topLeftX + 20, topLeftY + 20))
                rectangle.setFill(colour)
                rectangle.draw(win)
                
main()

thanks again

Draw it out first, then program each part individually and then put them together. To get you started, here is part of the right-most picture. Continue with a separate function to draw the other half (columns side), and then look at a way to combine them. It you can not combine them, then so be it. The old saying is, first you make it work, then you make it fast, then you make it pretty.

from graphics import *

def draw_patch(win, colour, side_length):
    ## 5 red rectangles and 5 white rectangles = skip this much
    ## to go from one red row to the next, i.e. skip one red+white combo
    width = side_length / 5
    for row in range(5):
        ## divide row by 2 because skip one row = red + white 
        ## divide width by 4 because it prints only 1/2 of the square (left
        ##     or right half), and 1/2 of red+white block

        ## left side
        draw_rows(width*row/2, win, colour, side_length, width/4)
        ## right side
        draw_rows(side_length-width*row/2, win, colour, side_length, width/4*-1)
     
def draw_rows(offset, win, colour, side_length, width):
    bl = Point(offset, offset)                   ## bottom left
    tr = Point(offset+width, side_length-offset) ## top right
    rectangle = Rectangle(bl, tr)
    rectangle.setFill(colour)
    rectangle.draw(win)

win = GraphWin("Patch", 200, 200)
draw_patch(win, 'red', 200)
 
raw_input("Press Enter")

That is one style of looking that picture, I see set of overlapping squares with same center location (+-x, +-y), alternating color overlapping drawn biggest first. So take your pick.

im still struggling to do the top and bottom sections and blend them together, just cannot do it

thanks for your help

Ok here is my alternative with couple lines left for you to fill up, not to give all solution:

from graphics import *

def draw_patch(number, win, colour, side_length, width):
    orig = colour
    for count in range(number):
        bl = ## sensored :)
        tr = ## sensored
        rectangle = Rectangle(bl, tr)
        rectangle.setFill(colour)
        rectangle.draw(win)
        colour = 'white' if colour == orig else orig
            
win = GraphWin("Patch", 200, 200)
draw_patch(10, win, 'red', 200, 10)
 
win.mainloop()

i have this code so far

from graphics import *

def main():

    win = GraphWin("patch", 100, 100)
    shape = Rectangle(Point(100, 100), Point(0,0))
    shape.setOutline("red")
    shape.setFill("red")
    shape.draw(win)

    shape2 = Rectangle(Point(90, 90), Point (10,10))
    shape2.setOutline("white")
    shape2.setFill("white")
    shape2.draw(win)

    shape3 = Rectangle(Point(80, 80), Point (20,20))
    shape3.setOutline("red")
    shape3.setFill("red")
    shape3.draw(win)

    shape4 = Rectangle(Point(70, 70), Point (30,30))
    shape4.setOutline("white")
    shape4.setFill("white")
    shape4.draw(win)

    shape5 = Rectangle(Point(60, 60), Point (40,40))
    shape5.setOutline("red")
    shape5.setFill("red")
    shape5.draw(win)

    shape6 = Rectangle(Point(50, 50), Point (50,50))
    shape6.setOutline("white")
    shape6.setFill("white")
    shape6.draw(win)

main()

how can i change this into a loop?

thank you
x

What can you say about sum of Point coordinates, put that change to for loop variable and use the variable instead of the constants, incorporating the change of white and red in also.

Or call a function that draws each square.

from graphics import *

def draw_square(win, points, color, change):
    shape = Rectangle(Point(points[0], points[1]), Point(points[2], points[3]))
    shape.setOutline(color)
    shape.setFill(color)
    shape.draw(win) 

    for j in range(2):
        points[j] -= change
        points[j+2] += change

width = 200
win = GraphWin("patch", width, width)
squares = 10
change = width/(squares*2)        ## size change for each smaller square
points = [width, width, 0, 0]     ## coordinates for the opposite corners
color = 0
color_list = ["red", "white"]
for j in range(squares):
    draw_square(win, points, color_list[color], change)
    color = [1,0][color]
raw_input("Press Enter")
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.