简体   繁体   English

如何修改此python代码?

[英]How do I modify this python code?

This python code draws a sierpinski triangle. 此python代码绘制了一个sierpinski三角形。 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. 在main()方法中尝试myPoints变量。 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 . 在您的main方法中,您想更改myPoints的值。 This list of lists is passed to sierpinski() as the second argument. 该列表列表作为第二个参数传递给sierpinski() For example, defining myPoints as below makes the triangle twice as big as it is in your code: 例如,如下定义myPoints会使三角形变成代码中三角形的两倍:

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. myPoints列表中的三个列表中的每一个都指示三角形的任何给定点到屏幕中心的距离。 For the above myPoints , the points of the triangle are drawn as follows: 对于上述myPoints ,三角形的点绘制如下:

  1. 200 pixels to the left (-X) of the center and 50 pixels below (-Y) the center 中心左侧(-X)200像素,中心(-Y)下方50像素
  2. 200 pixels above the center 中心上方200像素
  3. 200 pixels to the right of the center and 50 pixels below the center 中心右侧200像素,中心下方50像素

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]] . 如果需要,还可以修改它,使三角形不再是等边的-您可以使用[[-200, -50], [0, 250], [200, -50]]或a创建等边三角形[[-200, -50], [0, 50], [150, -50]]斜角三角形。 Try loads of different values and see what happens! 尝试加载不同的值,看看会发生什么!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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