Initial commit

master
Sameer Puri 6 years ago
commit 439f4859b8

2
.gitignore vendored

@ -0,0 +1,2 @@
/target
**/*.rs.bk

116
Cargo.lock generated

@ -0,0 +1,116 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "bitflags"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "cairo-rs"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
"cairo-sys-rs 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"glib 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
"glib-sys 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"gobject-sys 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.51 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "cairo-sys-rs"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"glib-sys 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.51 (registry+https://github.com/rust-lang/crates.io-index)",
"pkg-config 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "glib"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
"glib-sys 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"gobject-sys 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.51 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "glib-sys"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.51 (registry+https://github.com/rust-lang/crates.io-index)",
"pkg-config 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "gobject-sys"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"glib-sys 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.51 (registry+https://github.com/rust-lang/crates.io-index)",
"pkg-config 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "lazy_static"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "libc"
version = "0.2.51"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "lsys"
version = "0.1.0"
dependencies = [
"cairo-rs 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "pkg-config"
version = "0.3.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "winapi"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[metadata]
"checksum bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "228047a76f468627ca71776ecdebd732a3423081fcf5125585bcd7c49886ce12"
"checksum cairo-rs 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9e09d8a818b2ccc8983f04d95a9350c3cf8d24cc456cedca3b88fa3a81fdc0e2"
"checksum cairo-sys-rs 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b3fa13914fdc013387afa771f554f2f71d6ae931f4e5be9246c337d60c3dc484"
"checksum glib 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0c4e8fdc159c196a5dfa53a92929ac4c10c8a6637ffb43951f3fff89c2cd2365"
"checksum glib-sys 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5bda542f3caee39a027638e9644ff89204101ad916fd7370b585ad2c5fc97e61"
"checksum gobject-sys 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "23e05a14290d3dc255223cba51db4b0f3da438d5250657996fa99b2a30faf43e"
"checksum lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bc5729f27f159ddd61f4df6228e827e86643d4d3e7c32183cb30a1c08f604a14"
"checksum libc 0.2.51 (registry+https://github.com/rust-lang/crates.io-index)" = "bedcc7a809076656486ffe045abeeac163da1b558e963a31e29fbfbeba916917"
"checksum pkg-config 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)" = "676e8eb2b1b4c9043511a9b7bea0915320d7e502b0a079fb03f9635a5252b18c"
"checksum winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "f10e386af2b13e47c89e7236a7a14a086791a2b88ebad6df9bf42040195cf770"
"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"

@ -0,0 +1,8 @@
[package]
name = "lsys"
version = "0.1.0"
authors = ["purisame"]
edition = "2018"
[dependencies]
cairo-rs = { version = "^0", features = ["svg"] }

@ -0,0 +1,15 @@
# L-systems in Rust
Renderings of various L-systems using Rust and Cairo.
## Sierpinski Triangle
![sierpinski](out/sierpinski.svg)
## Sierpinski Arrowhead
![arrowhead](out/arrowhead.svg)
## Koch Curve
![koch](out/koch.svg)
## Dragon Curve
![dragon](out/dragon.svg)

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="1024pt" height="1024pt" viewBox="0 0 1024 1024" version="1.1">
<g id="surface11">
<path style="fill:none;stroke-width:0.001;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;" d="M -0.000000000000000028 0.5 L 0.00999832 0.5 L 0.0149994 0.491341 L 0.00999832 0.482681 L 0.0149994 0.474022 L 0.0249977 0.474022 L 0.0299988 0.482681 L 0.0249977 0.491341 L 0.0299988 0.5 L 0.0399971 0.5 L 0.0449982 0.491341 L 0.0549965 0.491341 L 0.0599976 0.5 L 0.0699959 0.5 L 0.0749969 0.491341 L 0.0699959 0.482681 L 0.0599976 0.482681 L 0.0549965 0.474022 L 0.0599976 0.465363 L 0.0549965 0.456703 L 0.0449982 0.456703 L 0.0399971 0.465363 L 0.0299988 0.465363 L 0.0249977 0.456703 L 0.0299988 0.448044 L 0.0399971 0.448044 L 0.0449982 0.439384 L 0.0399971 0.430725 L 0.0449982 0.422066 L 0.0549965 0.422066 L 0.0599976 0.430725 L 0.0699959 0.430725 L 0.0749969 0.422066 L 0.0699959 0.413406 L 0.0599976 0.413406 L 0.0549965 0.404747 L 0.0599976 0.396088 L 0.0699959 0.396088 L 0.0749969 0.387428 L 0.0699959 0.378769 L 0.0749969 0.37011 L 0.0849953 0.37011 L 0.0899963 0.378769 L 0.0849953 0.387428 L 0.0899963 0.396088 L 0.0999947 0.396088 L 0.104996 0.404747 L 0.0999947 0.413406 L 0.0899963 0.413406 L 0.0849953 0.422066 L 0.0899963 0.430725 L 0.0999947 0.430725 L 0.104996 0.422066 L 0.114994 0.422066 L 0.119995 0.430725 L 0.114994 0.439384 L 0.119995 0.448044 L 0.129993 0.448044 L 0.134995 0.456703 L 0.129993 0.465363 L 0.119995 0.465363 L 0.114994 0.456703 L 0.104996 0.456703 L 0.0999947 0.465363 L 0.104996 0.474022 L 0.0999947 0.482681 L 0.0899963 0.482681 L 0.0849953 0.491341 L 0.0899963 0.5 L 0.0999947 0.5 L 0.104996 0.491341 L 0.114994 0.491341 L 0.119995 0.5 L 0.129993 0.5 L 0.134995 0.491341 L 0.129993 0.482681 L 0.134995 0.474022 L 0.144993 0.474022 L 0.149994 0.482681 L 0.144993 0.491341 L 0.149994 0.5 L 0.159992 0.5 " transform="matrix(1024,-0.000000000000056843,0.000000000000056843,1024,0,0)"/>
</g>
</svg>

After

Width:  |  Height:  |  Size: 2.1 KiB

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 23 KiB

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 12 KiB

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="1024pt" height="1024pt" viewBox="0 0 1024 1024" version="1.1">
<g id="surface6">
<path style="fill:none;stroke-width:0.001;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;" d="M -0.433013 -0.25 L -0.438012 -0.241341 L -0.443011 -0.250002 L -0.44801 -0.241343 L -0.43801 -0.241345 L -0.443009 -0.232686 L -0.453007 -0.250007 L -0.458006 -0.241348 L -0.463004 -0.250009 L -0.468004 -0.24135 L -0.458004 -0.241352 L -0.463003 -0.232693 L -0.443003 -0.232696 L -0.448003 -0.224037 L -0.453001 -0.232698 L -0.458 -0.224039 L -0.448001 -0.22404 L -0.453 -0.215381 L -0.472995 -0.250024 L -0.477994 -0.241365 L -0.482992 -0.250026 L -0.487992 -0.241367 L -0.477992 -0.241369 L -0.482991 -0.23271 L -0.492988 -0.250031 L -0.497988 -0.241373 L -0.502986 -0.250033 L -0.507985 -0.241375 L -0.497986 -0.241376 L -0.502985 -0.232717 L -0.482985 -0.23272 L -0.487984 -0.224061 L -0.492983 -0.232722 L -0.497982 -0.224063 L -0.487983 -0.224064 L -0.492982 -0.215406 L -0.452983 -0.215411 L -0.457982 -0.206752 L -0.46298 -0.215413 L -0.46798 -0.206754 L -0.45798 -0.206756 L -0.462979 -0.198097 L -0.472976 -0.215418 L -0.477976 -0.206759 L -0.482974 -0.21542 L -0.487973 -0.206761 L -0.477974 -0.206763 L -0.482973 -0.198104 L -0.462973 -0.198107 L -0.467972 -0.189448 L -0.472971 -0.198109 L -0.47797 -0.18945 L -0.467971 -0.189451 L -0.47297 -0.180792 L -0.512959 -0.250078 L -0.517958 -0.241419 L -0.522957 -0.25008 L -0.527956 -0.241421 L -0.517957 -0.241423 L -0.522956 -0.232764 L -0.532953 -0.250085 L -0.537952 -0.241427 L -0.542951 -0.250087 L -0.54795 -0.241429 L -0.53795 -0.24143 L -0.54295 -0.232771 L -0.52295 -0.232774 L -0.527949 -0.224115 L -0.532948 -0.232776 L -0.537947 -0.224117 L -0.527947 -0.224118 L -0.532946 -0.21546 L -0.552941 -0.250102 L -0.55794 -0.241444 L -0.562939 -0.250104 L -0.567938 -0.241446 L -0.557938 -0.241447 L -0.562938 -0.232788 L -0.572935 -0.25011 L -0.577934 -0.241451 L -0.582933 -0.250112 L -0.587932 -0.241453 L -0.577932 -0.241454 L -0.582931 -0.232795 L -0.562932 -0.232798 L -0.567931 -0.224139 L -0.57293 -0.2328 L -0.577929 -0.224141 L -0.567929 -0.224143 L -0.572928 -0.215484 L -0.532929 -0.215489 L -0.537928 -0.20683 L -0.542927 -0.215491 L -0.547926 -0.206832 L -0.537926 -0.206834 L -0.542926 -0.198175 L -0.552923 -0.215496 L -0.557922 -0.206838 L -0.562921 -0.215498 L -0.56792 -0.20684 L -0.55792 -0.206841 L -0.562919 -0.198182 L -0.54292 -0.198185 L -0.547919 -0.189526 L -0.552918 -0.198187 L -0.557917 -0.189528 L -0.547917 -0.189529 L -0.552916 -0.180871 L -0.472918 -0.180882 L -0.477917 -0.172223 L -0.482916 -0.180883 L -0.487915 -0.172225 L -0.477916 -0.172226 L -0.482915 -0.163567 L -0.492912 -0.180889 L -0.497911 -0.17223 L -0.50291 -0.180891 L -0.507909 -0.172232 L -0.497909 -0.172233 L -0.502908 -0.163574 L -0.482909 -0.163577 L -0.487908 -0.154918 L -0.492907 -0.163579 L -0.497906 -0.15492 L -0.487906 -0.154922 L -0.492905 -0.146263 L -0.5129 -0.180906 L -0.517899 -0.172247 L -0.522898 -0.180908 L -0.527897 -0.172249 L -0.517897 -0.17225 L -0.522897 -0.163592 L -0.532894 -0.180913 L -0.537893 -0.172254 L -0.542892 -0.180915 L -0.547891 -0.172256 L -0.537891 -0.172257 L -0.54289 -0.163599 L -0.522891 -0.163601 L -0.52789 -0.154943 L -0.532889 -0.163603 L -0.537888 -0.154945 L -0.527888 -0.154946 L -0.532887 -0.146287 L -0.492888 -0.146293 L -0.497887 -0.137634 L -0.502886 -0.146295 L -0.507885 -0.137636 L -0.497885 -0.137637 L -0.502885 -0.128978 L -0.512882 -0.1463 L -0.517881 -0.137641 L -0.52288 -0.146302 L -0.527879 -0.137643 L -0.517879 -0.137644 L -0.522878 -0.128985 L -0.502879 -0.128988 L -0.507878 -0.120329 L -0.512877 -0.12899 L -0.517876 -0.120331 L -0.507876 -0.120333 L -0.512875 -0.111674 L -0.592854 -0.250246 L -0.432858 -0.250268 " transform="matrix(-512,-886.810013,886.810013,-512,0,0)"/>
</g>
</svg>

After

Width:  |  Height:  |  Size: 3.9 KiB

@ -0,0 +1,119 @@
extern crate cairo;
use cairo::{svg, Context};
use std::fs::File;
use std::io::Write;
fn main() {
koch();
sierpinski();
arrowhead();
dragon();
}
fn koch() {
let mut out = File::create("koch.svg").unwrap();
run(
"F",
&['F'],
|c: char| match c {
'F' => "F+F-F-F+F".chars().collect(),
other => vec![other],
},
std::f64::consts::PI / 2.,
4,
&mut out,
);
}
fn sierpinski() {
let mut out = File::create("sierpinski.svg").unwrap();
run(
"F-G-G",
&['F', 'G'],
|c: char| match c {
'F' => "F-G+F+G-F".chars().collect(),
'G' => "GG".chars().collect(),
other => vec![other],
},
std::f64::consts::PI * 2. / 3.,
4,
&mut out,
);
}
fn arrowhead() {
let mut out = File::create("arrowhead.svg").unwrap();
run(
"A",
&['A', 'B'],
|c: char| match c {
'A' => "B-A-B".chars().collect(),
'B' => "A+B+A".chars().collect(),
other => vec![other],
},
std::f64::consts::PI * 1. / 3.,
4,
&mut out,
);
}
fn dragon() {
let mut out = File::create("dragon.svg").unwrap();
run(
"FX",
&['F'],
|c: char| match c {
'X' => "X+YF+".chars().collect(),
'Y' => "-FX-Y".chars().collect(),
other => vec![other],
},
std::f64::consts::PI / 2.,
10,
&mut out,
);
}
fn run<F, W>(axiom: &str, variables: &[char], rules: F, angle: f64, iterations: usize, writer: W)
where
F: Fn(char) -> Vec<char> + Copy,
W: Write,
{
let surf = svg::Writer::new(1024.0, 1024.0, writer);
let ctx = Context::new(&surf);
ctx.scale(1024., 1024.);
ctx.set_line_width(0.001);
ctx.set_source_rgb(0., 0., 0.);
let mut state = axiom.to_string();
for _ in 0..iterations {
state = state.chars().map(rules).flatten().collect();
}
// let segment_count = state.chars().filter(|c| variables.contains(&c)).count();
ctx.move_to(0.0, 0.5);
for c in state.chars() {
match c {
'+' => {
ctx.rotate(-angle);
}
'-' => {
ctx.rotate(angle);
}
other => {
if variables.contains(&other) {
ctx.rel_line_to(0.01, 0.0);
}
}
}
}
ctx.stroke();
// let mut fout = File::create("out.png").unwrap();
// surf.write_all(&mut fout).unwrap();
// println!("{}", axiom);
}
Loading…
Cancel
Save