谢尔宾斯基三角反演

时间:2014-11-15 04:20:32

标签: python geometry turtle-graphics

嘿,我试图在Python中反转一个Sierpinski的三角形课程,我是python和turtle api的新手。我想知道是否有人可以解释这些点在goto函数中是如何工作的,因为我无法理解如何正确地映射倒三角形。 (这个代码是给我的,我只是略微修改了它。)

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 = ['cyan','purple','orange','navy','gold',
                'firebrick','lawn green']
    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 = [[-200,-100],[0,200],[200,-100]]
   sierpinski(myPoints,3,myTurtle)
   myWin.exitonclick()

main()

1 个答案:

答案 0 :(得分:1)

这是coords的外观(当你运行代码时): enter image description here

main()中,只需更改原始myPoints

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

为:

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

enter image description here