Metadata-Version: 2.4
Name: fractal
Version: 0.5.0
Summary: 分形绘图库，支持Julia集、Mandelbrot集、IFS、L-System等
Home-page: https://github.com/pysrc/fractal
Author: L.Chen
Author-email: 1570184051@qq.com
License: MIT
Keywords: fractal,分形,Julia,Mandelbrot,IFS,L-System
Platform: any
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.6
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Topic :: Scientific/Engineering :: Mathematics
Classifier: Topic :: Multimedia :: Graphics
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: Pillow>=8.0.0
Requires-Dist: numpy>=1.20.0
Dynamic: author
Dynamic: author-email
Dynamic: classifier
Dynamic: description
Dynamic: description-content-type
Dynamic: home-page
Dynamic: keywords
Dynamic: license
Dynamic: license-file
Dynamic: platform
Dynamic: requires-dist
Dynamic: summary

# Draw Fractal Image By Python

A Python fractal drawing library supporting Julia Set, Mandelbrot Set, IFS, L-System and more.

## Install

```bash
pip install fractal
```

Or download the package:

```bash
pip install -e .
```

Dependencies:
- Pillow >= 8.0.0
- numpy >= 1.20.0

## Features

- **Julia Set** - Interactive zoom with mouse
- **Mandelbrot Set** - Classic Mandelbrot fractal
- **IFS** - Iterated Function System (fern, leaf, tree, etc.)
- **L-System** - Lindenmayer System (Koch curve, dragon, tree, etc.)
- **CIFS** - Complex Iterated Function System

## GUI Operations

- **Save**: Click "Save" button or Ctrl+P
- **Save As**: Click "Save As..." to choose format (PNG/JPG/BMP)
- **Zoom In**: Left click to show selection box, drag to move, click again to confirm
- **Zoom Out**: Right click
- **Cancel Selection**: Press ESC or right click

## Examples

### L-System

```python
from fractal import Pen
from math import sqrt
p = Pen([350, 270])
p.setPoint([140, 60])
p.setWidth(1)
p.doD0L(omega="L", P={"L": "L+R", "R": "L-R"},
              delta=90, times=15, length=200, rate=sqrt(2))
p.wait()
```

![](fenxing_images/dragon.jpg)

```python
from fractal import Pen

p = Pen([500, 500], title="Window")
p.setPoint([495, 495])
p.setAngle(90)
p.doD0L(omega="f+f+f+f", P={"f": "ff+f--f+f"},
        delta=90, times=5, length=490, rate=3)
p.wait()
```

![](fenxing_images/Window.jpg)

```python
from fractal import Pen
p = Pen([420,420])
p.setPoint([10,10])
p.doD0L(omega = "L", P = {"L": "LFRFL-FF-RFLFR+FF+LFRFL", "R": "RFLFR+FF+LFRFL-FF-RFLFR"}, delta =  90, times = 4, length = 200 , rate = 3)
p.wait()
```

![](fenxing_images/fass2.jpg)

```python
from fractal import Pen
p = Pen([400, 470])
p.setAngle(90)
p.setPoint([200,470])
p.doD0L(omega = "f", P = {"f": "h[-f][+f]hf", "h": "hh"}, delta = 25.7, times = 7, length = 400, rate = 2.17)
p.wait()
```

![](fenxing_images/tree4.jpg)

```python
from fractal import Pen
p = Pen([400, 470])
p.setAngle(90)
p.setPoint([170, 470])
p.doD0L(omega="f", P={"f": "h+[[f]-f]-h[-hf]+f", "h": "hh"},
        delta=22.5, times=6, length=400, rate=2.3)
p.wait()
```

![](fenxing_images/tree5.jpg)

### IFS

```python
from fractal import IFS
from random import random

def ifsp(x, y):
    p = random()
    if p < 0.01:
        return (0, 0.16 * y)
    elif p < 0.07:
        if random() > 0.5:
            return (0.21 * x - 0.25 * y, 0.25 * x + 0.21 * y + 0.44)
        else:
            return (-0.2 * x + 0.26 * y, 0.23 * x + 0.22 * y + 0.6)
    else:
        return (0.85 * x + 0.1 * y, -0.05 * x + 0.85 * y + 0.6)

ob = IFS([400, 500], title = "Leaf")
ob.setPx(100, 100, 100)
ob.setIfsp(ifsp)
ob.doIFS(200000)
ob.wait()
```

![](fenxing_images/ifern.jpg)

```python
# Box IFS
from fractal import IFS
from random import randint


def ifsp(x, y):
    p = randint(1, 5)
    if p == 1:
        return (x / 3, y / 3)
    elif p == 2:
        return (x / 3 + 2 / 3, y / 3)
    elif p == 3:
        return (x / 3 + 1 / 3, y / 3 + 1 / 3)
    elif p == 4:
        return (x / 3, y / 3 + 2 / 3)
    else:
        return (x / 3 + 2 / 3, y / 3 + 2 / 3)

ob = IFS([500, 500], title="Box")
ob.setPx(490, 5, 5)
ob.setIfsp(ifsp)
ob.doIFS(200000)
ob.wait()
```

![](fenxing_images/ibox.jpg)

```python
from fractal import IFS

ifscode = [
    [0.879, 0.054, -0.051, 0.878, 0.077, 0.123, 0.123],
    [0.1, -0.193, 0.285, 0.224, 0.174, 0.169, 0.169],
    [0.008, 0.135, 0, 0.204, 0.075, 0.074, 0.074],
    [0.402, 0.045, 0.016, -0.197, 0.111, 0.193, 0.193]
]

ifs = IFS([500, 500])
ifs.setPx(700, 0, 0)
ifs.setIfsCode(ifscode)
ifs.doIFS(200000)
ifs.wait()
```

![](fenxing_images/leaf.jpg)

```python
from fractal import IFS

code = [
    [0.195, -0.488, 0.344, 0.443, 0.4431, 0.2452, 0.2],
    [0.462, 0.414, -0.252, 0.361, 0.2511, 0.5692, 0.2],
    [-0.637, 0, 0, 0.501, 0.8562, 0.2512, 0.2],
    [-0.035, 0.07, -0.469, 0.022, 0.4884, 0.5069, 0.2],
    [-0.058, -0.07, -0.453, -0.111, 0.5976, 0.0969, 0.2]
]

ifs = IFS([500,500])
ifs.setCoordinate()
ifs.setPx(500, 0, 0)
ifs.setIfsCode(code)
ifs.doIFS(200000)
ifs.wait()
```

![](fenxing_images/itree.jpg)



### Julia

```python
from fractal import Julia
ju = Julia([500, 500])
ju.setC(0 - 1j)
ju.doJulia(500)
ju.wait()
```

![](fenxing_images/ju2.jpg)

**-1.25 + 0j**

![](fenxing_images/ju6.jpg)

![](fenxing_images/ju62.jpg)



**-0.605-0.45j**

![](fenxing_images/(-0.605-0.45j)1.jpg)

![](fenxing_images/(-0.605-0.45j)2.jpg)

![](fenxing_images/(-0.605-0.45j)3.jpg)

### Mandelbrot



```python
from fractal import Mandelbrot
man = Mandelbrot([500, 500])
man.setRange(5, 5)
man.doMandelbrot(200)
man.wait()
```

![](fenxing_images/mandelbrot.jpg)

![](fenxing_images/mandelbrot2.jpg)

![](fenxing_images/mandelbrot3.jpg)

![](fenxing_images/mandelbrot4.jpg)

## License

MIT License

## Author

L.Chen (pysrc)
