Skip to content

Commit

Permalink
fractusist:0.1.1 (#1815)
Browse files Browse the repository at this point in the history
Co-authored-by: liuguangxi <liuguangxi@ime.ac.cn>
  • Loading branch information
liuguangxi and liuguangxi authored Feb 24, 2025
1 parent 6e3bfa4 commit 30b5c19
Show file tree
Hide file tree
Showing 20 changed files with 1,326 additions and 0 deletions.
21 changes: 21 additions & 0 deletions packages/preview/fractusist/0.1.1/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Guangxi Liu

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
126 changes: 126 additions & 0 deletions packages/preview/fractusist/0.1.1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
# Fractusist

Create a variety of wonderful fractals in Typst.


## Examples

The example below creates a dragon curve of the 12th iteration with the `dragon-curve` function.

![The rendered dragon curve](./examples/dragon-curve-n12.png)

<details>
<summary>Show code</summary>

```typ
#set page(width: auto, height: auto, margin: 0pt)
#dragon-curve(
12,
step-size: 6,
stroke-style: stroke(
paint: gradient.linear(..color.map.crest, angle: 45deg),
thickness: 3pt,
cap: "square"
)
)
```
</details>


## Features

- Use SVG backend for image rendering.
- Generate fractals using [L-system](https://en.wikipedia.org/wiki/L-system).
- The number of iterations, step size, fill and stroke styles, etc. of generated fractals could be customized.


## Usage

Import the latest version of this package with:

```typ
#import "@preview/fractusist:0.1.1": *
```

Each function generates a specific fractal. The input and output arguments of all functions have a similar style. Typical input arguments are as follows:

- `n`: the number of iterations (**the valid range of values depends on the specific function**).
- _`step-size`_: step size (in pt).
- _`fill-style`_: fill style, can be `none` or color or gradient (**exists only when the curve is closed**).
- _`stroke-style`_: stroke style, can be `none` or color or gradient or stroke object.
- _`width`_: the width of the image.
- _`height`_: the height of the image.
- _`fit`_: how the image should adjust itself to a given area, "cover" / "contain" / "stretch".

The content returned is the `image` element.

For more codes with these functions see [tests](./tests).


## Reference

### Dragon

- `dragon-curve`: Generate dragon curve (n: range **[0, 16]**).

```typ
#let dragon-curve(n, step-size: 10, stroke-style: black + 1pt, width: auto, height: auto, fit: "cover") = {...}
```


### Hilbert

- `hilbert-curve`: Generate 2D Hilbert curve. (n: range **[1, 8]**).

```typ
#let hilbert-curve(n, step-size: 10, stroke-style: black + 1pt, width: auto, height: auto, fit: "cover") = {...}
```

- `peano-curve`: Generate 2D Peano curve (n: range **[1, 5]**).

```typ
#let peano-curve(n, step-size: 10, stroke-style: black + 1pt, width: auto, height: auto, fit: "cover") = {...}
```


### Koch

- `koch-curve`: Generate Koch curve (n: range **[0, 6]**).

```typ
#let koch-curve(n, step-size: 10, stroke-style: black + 1pt, width: auto, height: auto, fit: "cover") = {...}
```

- `koch-snowflake`: Generate Koch snowflake (n: range **[0, 6]**).

```typ
#let koch-snowflake(n, step-size: 10, fill-style: none, stroke-style: black + 1pt, width: auto, height: auto, fit: "cover") = {...}
```


### Sierpiński

- `sierpinski-curve`: Generate classic Sierpiński curve (n: range **[0, 7]**).

```typ
#let sierpinski-curve(n, step-size: 10, fill-style: none, stroke-style: black + 1pt, width: auto, height: auto, fit: "cover") = {...}
```

- `sierpinski-square-curve`: Generate Sierpiński square curve (n: range **[0, 7]**).

```typ
#let sierpinski-square-curve(n, step-size: 10, fill-style: none, stroke-style: black + 1pt, width: auto, height: auto, fit: "cover") = {...}
```

- `sierpinski-arrowhead-curve`: Generate Sierpiński arrowhead curve (n: range **[0, 8]**).

```typ
#let sierpinski-arrowhead-curve(n, step-size: 10, stroke-style: black + 1pt, width: auto, height: auto, fit: "cover") = {...}
```

- `sierpinski-triangle`: Generate 2D Sierpiński triangle (n: range **[0, 6]**).

```typ
#let sierpinski-triangle(n, step-size: 10, fill-style: none, stroke-style: black + 1pt, width: auto, height: auto, fit: "cover") = {...}
```
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions packages/preview/fractusist/0.1.1/examples/dragon-curve-n12.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#set document(date: none)


#import "@preview/fractusist:0.1.1": *


#set page(width: auto, height: auto, margin: 0pt)


#dragon-curve(
12,
step-size: 6,
stroke-style: stroke(
paint: gradient.linear(..color.map.crest, angle: 45deg),
thickness: 3pt,
cap: "square"
)
)
82 changes: 82 additions & 0 deletions packages/preview/fractusist/0.1.1/src/dragon.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
//==============================================================================
// The dragon curve
//
// Public functions:
// dragon-curve
//==============================================================================


#import "util.typ": gen-svg


//----------------------------------------------------------
// Public functions
//----------------------------------------------------------

// Generate dragon curve
//
// iterations: [0, 16]
// axiom: "FX"
// rule set:
// "X" -> "X+YF+"
// "Y" -> "-FX-Y"
// angle: 90 deg
//
// Arguments:
// n: the number of iterations
// step-size: step size (in pt), optional
// stroke-style: stroke style, can be none or color or gradient or stroke object, optional
// width: the width of the image, optional
// height: the height of the image, optional
// fit: how the image should adjust itself to a given area, "cover" / "contain" / "stretch", optional
//
// Returns:
// content: generated vector graphic
#let dragon-curve(n, step-size: 10, stroke-style: black + 1pt, width: auto, height: auto, fit: "cover") = {
assert(type(n) == int and n >= 0 and n <= 16, message: "`n` should be in range [0, 16]")
assert(step-size > 0, message: "`step-size` should be positive")

if stroke-style != none {stroke-style = stroke(stroke-style)}
let stroke-width = if (stroke-style == none) {0} else if (stroke-style.thickness == auto) {1} else {calc.abs(stroke-style.thickness.pt())}

let axiom = "FX"
let rule-set = (X: "X+YF+", Y: "-FX-Y")
let dx = (step-size, 0, -step-size, 0)
let dy = (0, step-size, 0, -step-size)
let dx-str = dx.map(i => repr(i))
let dy-str = dy.map(i => repr(i))

let s = axiom
for i in range(n) {s = s.replace(regex("X|Y"), x => rule-set.at(x.text))}

let dir = 0
let path-d = "M0 0 "
for c in s {
if c == "-" {
dir -= 1
if dir < 0 {dir = 3}
} else if c == "+" {
dir += 1
if (dir == 4) {dir = 0}
} else if c == "F" {
path-d += "l" + dx-str.at(dir) + " " + dy-str.at(dir) + " "
}
}

let tbl-x-min = (0, 0, 0, -2, -4, -5, -5, -5, -5, -5, -10, -42, -74, -85, -85, -85, -85)
let tbl-x-max = (0, 1, 1, 1, 1, 1, 2, 10, 18, 21, 21, 21, 21, 21, 42, 170, 298)
let tbl-y-min = (0, 0, 0, 0, -1, -5, -9, -10, -10, -10, -10, -10, -21, -85, -149, -170, -170)
let tbl-y-max = (0, 1, 2, 2, 2, 2, 2, 2, 5, 21, 37, 42, 42, 42, 42, 42, 85)
let margin = calc.max(5, stroke-width)
let x-min = tbl-x-min.at(n) * step-size
let x-max = tbl-x-max.at(n) * step-size
let y-min = tbl-y-min.at(n) * step-size
let y-max = tbl-y-max.at(n) * step-size
x-min = calc.floor(x-min - margin)
x-max = calc.ceil(x-max + margin)
y-min = calc.floor(y-min - margin)
y-max = calc.ceil(y-max + margin)

let svg-code = gen-svg(path-d, (x-min, x-max, y-min, y-max), none, stroke-style)
return image(bytes(svg-code), width: width, height: height, fit: fit)
}
143 changes: 143 additions & 0 deletions packages/preview/fractusist/0.1.1/src/hilbert.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
//==============================================================================
// The Hilbert curve
//
// Public functions:
// hilbert-curve, peano-curve
//==============================================================================


#import "util.typ": gen-svg


//----------------------------------------------------------
// Public functions
//----------------------------------------------------------

// Generate 2D Hilbert curve
//
// iterations: [1, 8]
// axiom: "W"
// rule set:
// "V" -> "-WF+VFV+FW-"
// "W" -> "+VF-WFW-FV+"
// angle: 90 deg
//
// Arguments:
// n: the number of iterations
// step-size: step size (in pt), optional
// stroke-style: stroke style, can be none or color or gradient or stroke object, optional
// width: the width of the image, optional
// height: the height of the image, optional
// fit: how the image should adjust itself to a given area, "cover" / "contain" / "stretch", optional
//
// Returns:
// content: generated vector graphic
#let hilbert-curve(n, step-size: 10, stroke-style: black + 1pt, width: auto, height: auto, fit: "cover") = {
assert(type(n) == int and n >= 1 and n <= 8, message: "`n` should be in range [1, 8]")
assert(step-size > 0, message: "`step-size` should be positive")

if stroke-style != none {stroke-style = stroke(stroke-style)}
let stroke-width = if (stroke-style == none) {0} else if (stroke-style.thickness == auto) {1} else {calc.abs(stroke-style.thickness.pt())}

let axiom = "W"
let rule-set = (V: "-WF+VFV+FW-", W: "+VF-WFW-FV+")
let dx = (step-size, 0, -step-size, 0)
let dy = (0, step-size, 0, -step-size)
let dx-str = dx.map(i => repr(i))
let dy-str = dy.map(i => repr(i))

let s = axiom
for i in range(n) {s = s.replace(regex("V|W"), x => rule-set.at(x.text))}

let dir = 0
let path-d = "M0 0 "
for c in s {
if c == "-" {
dir -= 1
if dir < 0 {dir = 3}
} else if c == "+" {
dir += 1
if (dir == 4) {dir = 0}
} else if c == "F" {
path-d += "l" + dx-str.at(dir) + " " + dy-str.at(dir) + " "
}
}

let margin = calc.max(5, stroke-width)
let x-min = 0
let x-max = (calc.pow(2, n) - 1) * step-size
let y-min = 0
let y-max = x-max
x-min = calc.floor(x-min - margin)
x-max = calc.ceil(x-max + margin)
y-min = calc.floor(y-min - margin)
y-max = calc.ceil(y-max + margin)

let svg-code = gen-svg(path-d, (x-min, x-max, y-min, y-max), none, stroke-style)
return image(bytes(svg-code), width: width, height: height, fit: fit)
}


// Generate 2D Peano curve (Hilbert II curve)
//
// iterations: [1, 5]
// axiom: "X"
// rule set:
// "X" -> "XFYFX+F+YFXFY-F-XFYFX"
// "Y" -> "YFXFY-F-XFYFX+F+YFXFY"
// angle: 90 deg
//
// Arguments:
// n: the number of iterations
// step-size: step size (in pt), optional
// stroke-style: stroke style, can be none or color or gradient or stroke object, optional
// width: the width of the image, optional
// height: the height of the image, optional
// fit: how the image should adjust itself to a given area, "cover" / "contain" / "stretch", optional
//
// Returns:
// content: generated vector graphic
#let peano-curve(n, step-size: 10, stroke-style: black + 1pt, width: auto, height: auto, fit: "cover") = {
assert(type(n) == int and n >= 1 and n <= 5, message: "`n` should be in range [1, 5]")
assert(step-size > 0, message: "`step-size` should be positive")

if stroke-style != none {stroke-style = stroke(stroke-style)}
let stroke-width = if (stroke-style == none) {0} else if (stroke-style.thickness == auto) {1} else {calc.abs(stroke-style.thickness.pt())}

let axiom = "X"
let rule-set = (X: "XFYFX+F+YFXFY-F-XFYFX", Y: "YFXFY-F-XFYFX+F+YFXFY")
let dx = (step-size, 0, -step-size, 0)
let dy = (0, step-size, 0, -step-size)
let dx-str = dx.map(i => repr(i))
let dy-str = dy.map(i => repr(i))

let s = axiom
for i in range(n) {s = s.replace(regex("X|Y"), x => rule-set.at(x.text))}

let dir = 0
let path-d = "M0 0 "
for c in s {
if c == "-" {
dir -= 1
if dir < 0 {dir = 3}
} else if c == "+" {
dir += 1
if (dir == 4) {dir = 0}
} else if c == "F" {
path-d += "l" + dx-str.at(dir) + " " + dy-str.at(dir) + " "
}
}

let margin = calc.max(5, stroke-width)
let x-min = 0
let x-max = (calc.pow(3, n) - 1) * step-size
let y-min = 0
let y-max = x-max
x-min = calc.floor(x-min - margin)
x-max = calc.ceil(x-max + margin)
y-min = calc.floor(y-min - margin)
y-max = calc.ceil(y-max + margin)

let svg-code = gen-svg(path-d, (x-min, x-max, y-min, y-max), none, stroke-style)
return image(bytes(svg-code), width: width, height: height, fit: fit)
}
Loading

0 comments on commit 30b5c19

Please sign in to comment.