利用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 条评论
登录 后参与评论

相关文章

来自专栏何俊林

FFmpeg总结(十一)用ffmpeg进行转格式,Android下播放网络音频流

图:杭州西湖 思路: 1、mp3转成pcm(音频数据),ffmpeg做的事 2、OpenSL ES引擎创建AudioPlayer,实际调用了AudioTra...

5665
来自专栏xingoo, 一个梦想做发明家的程序员

MFC 随机矩形

问题描述:   简单地使用随即的尺寸和颜色不停的绘制一系列的图像。 一种古老的方式:   设置一个向窗口函数发送WM_TIMER消息的windows计时器。  ...

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

使用SAPGUI画图

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

1134
来自专栏上善若水

004-golang 正则表达式的使用

3228
来自专栏菩提树下的杨过

ExtJs学习笔记(4)_EditorGridPanel(可编辑的网格控件)

这一节,我们将看到ExtJs功能强大的可编辑网格控件,几乎与VS.Net的GridView功能一样了,但是ExtJs的可是纯JS的UI 一.静态示例(改自Ext...

6455
来自专栏DT乱“码”

连接数据库操作

package com.chendongj.dbUtil; import java.sql.Connection; import java.sql.Drive...

2289
来自专栏用户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...

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

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

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

3854
来自专栏黑白安全

来做个Google Hack吗?

storemanager/contents/item.php?page_code=

3676
来自专栏曾大稳的博客

使用SoudTouch实现变速变调

2182

扫码关注云+社区