美文网首页
画分形图的一个例子

画分形图的一个例子

作者: 水之心 | 来源:发表于2020-08-31 10:38 被阅读0次
    import random
    import numpy as np
    from matplotlib import pyplot as plt
    # set point coordinates
    x = [1, 1.5, 2]
    y = [1, 1+np.sqrt(.75), 1]
    ry = 1
    rx = np.random.rand(1,) + 1
    start = [ry, rx]
    a, b, c = zip(x, y)
    
    # set list of vertices for random choice
    direction = [a, b, c]
    
    
    def rand_dir(dirc):
        return np.array(random.choice(dirc))
    
    
    def next_point(array, array2):
        return (array + array2) * .5
    
    
    plt.figure(figsize=(10, 10))
    
    # plot triangle
    plt.scatter(x, y)
    
    # plot initial random point
    plt.scatter(rx, ry)
    
    
    n = 100000
    for i in range(n):
        tri = rand_dir(direction)
        start = next_point(tri, start)
        point = plt.scatter(start[0], start[1], s=5)
    
    plt.savefig('ChaosGameTriangle'+str(n)+'.png')
    
    plt.show()
    

    效果:

    相关文章

      网友评论

          本文标题:画分形图的一个例子

          本文链接:https://www.haomeiwen.com/subject/zfkusktx.html