利用cairo画Fractal Tree

利用cairo画分形树

Fractal Tree

pip install cairocffi

在windows下,还需下载GTK+。 http://win32builder.gnome.org/ 然后解压缩在比如c:/gtk文件夹下。 将其中bin文件夹,加入进系统环境变量 。 再cmd运行:gtk3-demo 运行成功就说明没问题了,如下:

demo

代码如下:

import numpy as np
import cairocffi as cairo


ITERATIONS = 16  # total number of iterations
ROOT_COLOR = np.array([0.0, 0.0, 0.0])  # root branch color
LEAF_COLOR = np.array([1.0, 1.0, 0.2])  # leaf color
TRUNK_LEN = 200  # initial length of the trunk
TRUNK_RAD = 3.0  # initial radius of the trunk
THETA = np.pi / 2  # initial angle of the branch
ANGLE = np.pi / 4.5  # angle between branches in the same level
PERTURB = 6.0  # perturb the angle a little to make the tree look random
RATIO = 0.8  # contraction factor between successive trunks
WIDTH = 600  # image width
HEIGHT = 600  # image height
ROOT = (WIDTH / 2.0, HEIGHT + 50)  # pixel position of the root


def get_color(level):
    """
    Return an interpolation of the two colors `ROOT_COLOR` and `LEAF_COLOR`.
    """
    a = float(level) / ITERATIONS
    return a * ROOT_COLOR + (1 - a) * LEAF_COLOR


def get_line_width(level):
    """Return the line width of a given level."""
    return max(1, TRUNK_RAD * level / ITERATIONS)


def fractal_tree(ctx,         # a cairo context to draw on
                 level,       # current level in the iterations
                 start,       # (x, y) coordinates of the start of this trunk
                 t,           # current trunk length
                 r,           # factor to contract the trunk in each iteration
                 theta,       # orientation of current trunk
                 angle,       # angle between branches in the same level
                 perturb,     # perturb the angle
                 ):
    if level == 0:
        return

    x0, y0 = start
    # randomize the length
    randt = np.random.random() * t
    x, y = x0 + randt * np.cos(theta), y0 - randt * np.sin(theta)

    color = get_color(level)
    ctx.move_to(x0, y0)
    ctx.line_to(x, y)
    ctx.set_line_width(get_line_width(level))
    ctx.set_source_rgb(*color)
    ctx.stroke()

    theta1 = theta + np.random.random() * (perturb / level) * angle
    theta2 = theta - np.random.random() * (perturb / level) * angle
    # recursively draw the next branches
    fractal_tree(ctx, level - 1, (x, y), t * r,
                 r, theta1, angle, perturb)
    fractal_tree(ctx, level - 1, (x, y), t * r,
                 r, theta2, angle, perturb)


def main():
    surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, WIDTH, HEIGHT)
    ctx = cairo.Context(surface)
    ctx.set_line_cap(cairo.LINE_CAP_ROUND)
    ctx.set_line_join(cairo.LINE_JOIN_ROUND)
    ctx.set_source_rgb(1, 1, 1)
    ctx.paint()
    fractal_tree(ctx, ITERATIONS, ROOT, TRUNK_LEN,
                 RATIO, THETA, ANGLE, PERTURB)
    surface.write_to_png("random_fractal_tree.png")


if __name__ == "__main__":
    main()

代码来源:https://github.com/neozhaoliang/pywonderland/blob/master/src/misc/fractaltree.py

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏高性能服务器开发

(三) 服务器端的程序架构介绍2

下面我们以pc端登录为例来具体看一个数据包在服务器端各个服务之间走过的流程: 步骤1:login_server初始化侦听socket,设置新连接到来的回调函数。...

4215
来自专栏PPV课数据科学社区

【学习】七天搞定SAS(一):数据的导入、数据结构

SAS的数据类型 ? 首先,sas的编程大概就两块:Data和PROC,这个倒是蛮清晰的划分。然后目前关注data部分。 SAS的数据类型还真的只有两种:数字和...

40312
来自专栏debugeeker的专栏

sedna进行xquery查询

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xuzhina/article/detai...

861
来自专栏曾大稳的博客

使用SoudTouch实现变速变调

3032
来自专栏Netkiller

Spring boot · 链接池配置

本文节选自《Netkiller Java 手札》 http://www.netkiller.cn/java/spring/boot/index.html 10....

3604
来自专栏用户2442861的专栏

Convert URL to image with Python and OpenCV(根据URL下载图片)

http://www.pyimagesearch.com/2015/03/02/convert-url-to-image-with-python-and-op...

1291
来自专栏Jerry的SAP技术分享

使用SAPGUI画图

国内80后上的编程课应该都学过Logo(https://en.wikipedia.org/wiki/Logo_(programming_language)这门编...

1444
来自专栏杂烩

分布式服务框架之Dubbo整合Spring项目(二)

1092
来自专栏一棹烟波

speex库音频降噪(含代码)

speex库中音频降噪效果不错,应该是应用最广泛的吧,speex库下载地址https://www.speex.org/downloads/,可以直接下载二进制代...

5806
来自专栏黑白安全

来做个Google Hack吗?

storemanager/contents/item.php?page_code=

6676

扫码关注云+社区

领取腾讯云代金券