zoukankan      html  css  js  c++  java
  • python 算法 day3

     谢尔宾斯基三角形

    展示的一个是三向递归的算法:从单个的大三角形开始,取它的各边中点作三条中位线,这样就把它分成了四个新的三角形;剔除掉这四个新三角形中最中间的那个,

    对其余三个角上的三角形重复以上的操作

    设置结束条件可以设置为我们想划分三角形的次数

    import turtle
    import time
    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()
        time.sleep(5)
    def getMid(a,b):
        return ((a[0]+b[0])/2 , (a[1] + b[1])/2)
    
    def sierpinsk(points,degree,myTurtle):
        colormap = ["blue","red","green","white","yellow","orange","violet"]
        drawTriangle(points,colormap[degree],myTurtle)
        if degree > 0:
            sierpinsk([points[0],getMid(points[0],points[1]),getMid(points[0],points[2])],degree-1,myTurtle)
            print("i am ready")
            sierpinsk([points[1],getMid(points[0],points[1]),getMid(points[1],points[2])],degree-1,myTurtle)
            sierpinsk([points[2],getMid(points[2],points[1]),getMid(points[0],points[2])],degree-1,myTurtle)
            print("this is me")
    def main():
        myTurtle = turtle.Turtle()
        mtwin = turtle.Screen()
        mypoints = [[-100,-50],[0,100],[100,-50]]
        sierpinsk(mypoints,3,myTurtle)
        mtwin.exitonclick()
    main()

    Sierpinski函数做的第一件事就是画外面最大的三角形,然后有三个递归调用,递归总是先处理左下角的三角形 然后处理中间的三角形,最后处理右边的三角形  getMId函数提取两个参数作为端点,然后返回两个端点组成的线段的中点

  • 相关阅读:
    HDU2149-Public Sale
    分页和多条件查询功能
    hdu 4691 最长的共同前缀 后缀数组 +lcp+rmq
    BZOJ 2588 Count on a tree (COT) 是持久的段树
    windows 设置脚本IP
    hdu 4912 Paths on the tree(树链拆分+贪婪)
    分散式-ubuntu12.04安装hadoop1.2.1
    struts详细解释拦截器
    Codeforces 459E Pashmak and Graph(dp+贪婪)
    C#中的数据格式转换 (未完待更新)
  • 原文地址:https://www.cnblogs.com/suizhixxie/p/10394530.html
Copyright © 2011-2022 走看看