Skip to content

Commit

Permalink
d0fractal init.
Browse files Browse the repository at this point in the history
  • Loading branch information
d0t451 committed Apr 17, 2019
0 parents commit 65cfd92
Show file tree
Hide file tree
Showing 46 changed files with 628 additions and 0 deletions.
50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# d0fractal
绘制分形图像

## Mandelbrot分形图像
根据Mandelbrot集绘制分形图像。Mandelbrot集由一个复变函数**f(z) = z<sup>2</sup> + c**生成,其中c为当前坐标点,z从0开始迭代。

Draw fractal image using Mandelbrot set. The Mandelbrot set is generated by a complex function **f(z) = z<sup>2</sup> + c**, where c is current coordinate point, and z iterates from 0.
![](https://github.com/d0t451/fractal/blob/master/images/mandelbrot.png)

## Julia分形图像
根据Julia集绘制分形图像。Julia集由一个复变函数**f(z) = z<sup>2</sup> + c**生成,其中c为常数,z从当前坐标点开始迭代。

Draw fractal images using Julia set. The Julia set is generated by a complex function **f(z) = z<sup>2</sup> + c**, where c is a constant, and z iterates from current coordinate point.
### c = (0.34-0.05i)
![](https://github.com/d0t451/fractal/blob/master/images/(0.34-0.05j).png)
### c = (-0.52+0.62i)
![](https://github.com/d0t451/fractal/blob/master/images/(-0.52+0.62j).png)
### c = (-0.54-0.5255i)
![](https://github.com/d0t451/fractal/blob/master/images/(-0.54-0.5255j).png)
### c = (-0.55+0.64i)
![](https://github.com/d0t451/fractal/blob/master/images/(-0.55+0.64j).png)
### c = (0.338+0.489i)
![](https://github.com/d0t451/fractal/blob/master/images/(0.338+0.489j).png)
### c = (0.365-0.37i)
![](https://github.com/d0t451/fractal/blob/master/images/(0.365-0.37j).png)
### c = (0.3593+0.5103i)
![](https://github.com/d0t451/fractal/blob/master/images/(0.3593+0.5103j).png)
### c = (0.42413+0.20753i)
![](https://github.com/d0t451/fractal/blob/master/images/(0.42413+0.20753j).png)
### c = -1.38
![](https://github.com/d0t451/fractal/blob/master/images/-1.38.png)
### c = 1i
![](https://github.com/d0t451/fractal/blob/master/images/1j.png)

## Koch曲线分形图像
一个边长为1的等边三角形,取每边中间的三分之一,接上去一个形状完全相似的但边长为其三分之一的三角形,结果是一个六角形。取六角形的每个边做同样的变换,即在中间三分之一接上更小的三角形,以此重复,直至无穷。

The Koch snowflake can be constructed by starting with an equilateral triangle, then recursively altering each line segment as follows:
1. divide the line segment into three segments of equal length.
2. draw an equilateral triangle that has the middle segment from step 1 as its base and points outward.
3. remove the line segment that is the base of the triangle from step 2.

![](https://github.com/d0t451/fractal/blob/master/images/koch.png)

## 谢尔宾斯基地毯分形图像
将一个实心正方形划分为的9个小正方形,去掉中间的小正方形,再对余下的小正方形重复这一操作便能得到谢尔宾斯基地毯。

Divide a solid square into 9 small squares, remove the middle square, and repeat the operation for remaining small squares to get the Sierpinski carpet.

![](https://github.com/d0t451/fractal/blob/master/images/sierpinski_carpet.png)
132 changes: 132 additions & 0 deletions fractal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
# -*- coding: utf-8 -*-
"""
@author: d0t451
@date: 2019/4/17
@desc: draw fractal images
"""
from fractal_utils import *


def draw_julia_image(width, height, is_save_to_file=False):
"""
绘制Julia分形图像
:param width:
:param height:
:param is_save_to_file: 是否保存为文件
:return:
"""
julia_c_list = [0, -0.2 + 0.2j, -0.77 - 0.22j, 0.365 - 0.37j, -0.6358 + 0.682j, -0.55 + 0.64j, -0.52 + 0.62j, -0.51251 - 0.521296j,
-0.5264 - 0.5255j, -0.534 - 0.5255j, -0.54 - 0.5255j, -0.62 - 0.44j, -0.69 - 0.31j, -0.691 + 0.312j, -0.6984 + 0.31j, 0.26,
0.34 - 0.05j, 0.375 - 0.083j, 0.42413 + 0.20753j, 0.3593 + 0.5103j, 0.338 + 0.489j, 0 + 1j, -1.75488, -1.38, 0.3 - 0.015j,
0.47 - 0.1566j, 0.02 - -0.66j, -0.6843 - 0.3944j, -0.0471 - 0.656j, -0.015 - 0.66j, ] # 几个Julia分形函数

print('{} Julia fractal images will be shown...'.format(len(julia_c_list)))
for c in julia_c_list:
points = get_julia_set(c, width, height)
draw_fx_image(points, width, height, bg_color=(0, 64, 64), image_show_time=50)
if is_save_to_file:
filename = 'tmp/{}.png'.format(c)
render_and_save_fx_image(filename, points, width, height, bg_color=(0, 64, 64))


def draw_multi_color_julia_image(width, height, begin_color=(0, 0, 0), end_color=(255, 255, 255), is_save_to_file=False):
"""
绘制渐变色Julia分形图像
:param width:
:param height:
:param begin_color:
:param end_color:
:param is_save_to_file: 是否保存为文件
:return:
"""
julia_c_list = [0, -0.2 + 0.2j, -0.77 - 0.22j, 0.365 - 0.37j, -0.6358 + 0.682j, -0.55 + 0.64j, -0.52 + 0.62j, -0.51251 - 0.521296j,
-0.5264 - 0.5255j, -0.534 - 0.5255j, -0.54 - 0.5255j, -0.62 - 0.44j, -0.69 - 0.31j, -0.691 + 0.312j, -0.6984 + 0.31j, 0.26,
0.34 - 0.05j, 0.375 - 0.083j, 0.42413 + 0.20753j, 0.3593 + 0.5103j, 0.338 + 0.489j, 0 + 1j, -1.75488, -1.38, 0.3 - 0.015j,
0.47 - 0.1566j, 0.02 - -0.66j, -0.6843 - 0.3944j, -0.0471 - 0.656j, -0.015 - 0.66j, ] # 几个Julia分形函数

print('{} Julia fractal images will be shown...'.format(len(julia_c_list)))
for c in julia_c_list:
screen_points_with_escape_time = get_julia_set_with_escape_points(c, width, height)
image_matrix = draw_multi_color_fx_image(screen_points_with_escape_time, width, height, begin_color, end_color, image_show_time=50)
if is_save_to_file:
save_fx_image('tmp/{}.png'.format(c), image_matrix)


def draw_mandelbrot_image(width, height, is_save_to_file=False):
"""
绘制Mandelbrot分形图像
:param width:
:param height:
:param is_save_to_file: 是否保存为文件
:return:
"""
points = get_mandelbrot_set(width, height)
draw_fx_image(points, width, height, bg_color=(0, 64, 64), image_show_time=0)
if is_save_to_file:
render_and_save_fx_image('images/mandelbrot.png', points, width, height, bg_color=(0, 64, 64))


def draw_multi_color_mandelbrot_image(width, height, begin_color=(0, 0, 0), end_color=(255, 255, 255), is_save_to_file=False):
"""
绘制渐变色Mandelbrot分形图像
:param width:
:param height:
:param begin_color:
:param end_color:
:param is_save_to_file: 是否保存为文件
:return:
"""
screen_points_with_escape_time = get_mandelbrot_set_with_escape_points(width, height)
image_matrix = draw_multi_color_fx_image(screen_points_with_escape_time, width, height, begin_color, end_color, image_show_time=0)
if is_save_to_file:
save_fx_image('images/mandelbrot.png', image_matrix)


def draw_koch_image(width, height, is_save_to_file=False):
"""
绘制Koch曲线分形图像
:param width:
:param height:
:param is_save_to_file: 是否保存为文件
:return:
"""
points = get_koch_set(width, height)
draw_fx_image(points, width, height, color=(255, 255, 255), bg_color=(0, 64, 64), image_show_time=0)
if is_save_to_file:
render_and_save_fx_image('images/koch.png', points, width, height, color=(255, 255, 255), bg_color=(0, 64, 64))


def draw_sierpinski_carpet_image(width, height, is_save_to_file=False):
"""
绘制谢尔宾斯基地毯分形图像
:param width:
:param height:
:param is_save_to_file: 是否保存为文件
:return:
"""
points = get_sierpinski_carpet_set(width, height)
draw_fx_image(points, width, height, color=(0, 0, 0), bg_color=(255, 255, 255), image_show_time=0)
if is_save_to_file:
render_and_save_fx_image('images/sierpinski_carpet.png', points, width, height, color=(0, 0, 0), bg_color=(255, 255, 255), )


if __name__ == '__main__':
# 绘制Julia分形图像
# draw_julia_image(800, 800)

# 绘制渐变色Julia分形图像
draw_multi_color_julia_image(800, 800, begin_color=(36, 87, 193), end_color=(104, 230, 226), is_save_to_file=True)

# 绘制Mandelbrot分形图像
# draw_mandelbrot_image(800, 800)

# 绘制渐变色Mandelbrot分形图像
# draw_multi_color_mandelbrot_image(800, 800, begin_color=(0, 0, 0), end_color=(255, 255, 255), is_save_to_file=True)

# 绘制Koch曲线分形图像
# draw_koch_image(600, 600, False)

# 绘制谢尔宾斯基地毯分形图像
# draw_sierpinski_carpet_image(729, 729, False)

print('OK.')
Loading

0 comments on commit 65cfd92

Please sign in to comment.