简体   繁体   中英

How do I modify this python code?

This python code draws a sierpinski triangle. It starts by drawing the main triangle and then draws a bunch of smaller characters

import turtle

def drawTriangle(points,color,myTurtle):
    myTurtle.fillcolor(color)
    myTurtle.up()
    myTurtle.goto(points[0][0],points[0][1])
    myTurtle.down()
    myTurtle.begin_fill()
    myTurtle.goto(points[1][0],points[1][1])
    myTurtle.goto(points[2][0],points[2][1])
    myTurtle.goto(points[0][0],points[0][1])
    myTurtle.end_fill()

def getMid(p1,p2):
    return ( (p1[0]+p2[0]) / 2, (p1[1] + p2[1]) / 2)

def sierpinski(points,degree,myTurtle):
    colormap = ['red','turquoise','green','purple','Antique White 4',
                'white','black']
    drawTriangle(points,colormap[degree],myTurtle)
    if degree > 0:
        sierpinski([points[0],
                        getMid(points[0], points[1]),
                        getMid(points[0], points[2])],
                   degree-1, myTurtle)
        sierpinski([points[1],
                        getMid(points[0], points[1]),
                        getMid(points[1], points[2])],
                   degree-1, myTurtle)
        sierpinski([points[2],
                        getMid(points[2], points[1]),
                        getMid(points[0], points[2])],
                   degree-1, myTurtle)

def main():
   myTurtle = turtle.Turtle()
   myWin = turtle.Screen()
   myPoints = [[-100,-50],[0,100],[100,-50]]
   sierpinski(myPoints,3,myTurtle)
   myWin.exitonclick()

main()

What I want to do is modify my code so that the entire triangle bigger. I'm just not sure what I have to change in order to change the size.

Try the myPoints variable in your main() method. its set to

myPoints = [[-100,-50],[0,100],[100,-50]]

maybe change to

myPoints = [[-200, -100],[0,200],[200,-100]]

or something along those lines. i didnt run it but im pretty sure those values dictate the size of your triangle.

Within your main method, you would want to change the value of myPoints . This list of lists is passed to sierpinski() as the second argument. For example, defining myPoints as below makes the triangle twice as big as it is in your code:

myPoints = [[-200, -50], [0, 200], [200, -50]]
sierpinski(myPoints, 2, myTurtle)

Each of the three lists within the myPoints list indicates the distance any given point of the triangle will be from the center of the screen. For the above myPoints , the points of the triangle are drawn as follows:

  1. 200 pixels to the left (-X) of the center and 50 pixels below (-Y) the center
  2. 200 pixels above the center
  3. 200 pixels to the right of the center and 50 pixels below the center

If you wanted, you could also modify this so that the triangle is no longer equilateral - you could create an equilateral triangle with [[-200, -50], [0, 250], [200, -50]] , or a scalene triangle with [[-200, -50], [0, 50], [150, -50]] . Try loads of different values and see what happens!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM