Koch curve: Difference between revisions
Content added Content deleted
m (Minor edit to C++ code) |
m (Rust - reformatted with rustfmt) |
||
Line 1,801: | Line 1,801: | ||
// svg = "0.8.0" |
// svg = "0.8.0" |
||
⚫ | |||
⚫ | |||
use svg::node::element::path::Data; |
use svg::node::element::path::Data; |
||
⚫ | |||
⚫ | |||
const SQRT3_2 |
const SQRT3_2: f64 = 0.86602540378444; |
||
fn koch_curve(mut data: Data, x0 |
fn koch_curve(mut data: Data, x0: f64, y0: f64, x1: f64, y1: f64, order: usize) -> Data { |
||
x1 : f64, y1 : f64, order : usize) -> Data { |
|||
if order == 0 { |
if order == 0 { |
||
data = data.line_to((x1, y1)); |
data = data.line_to((x1, y1)); |
||
Line 1,814: | Line 1,813: | ||
let dx = x1 - x0; |
let dx = x1 - x0; |
||
let dy = y1 - y0; |
let dy = y1 - y0; |
||
let x2 = x0 + dx/3.0; |
let x2 = x0 + dx / 3.0; |
||
let y2 = y0 + dy/3.0; |
let y2 = y0 + dy / 3.0; |
||
let x3 = x0 + dx/2.0 - dy * SQRT3_2/3.0; |
let x3 = x0 + dx / 2.0 - dy * SQRT3_2 / 3.0; |
||
let y3 = y0 + dy/2.0 + dx * SQRT3_2/3.0; |
let y3 = y0 + dy / 2.0 + dx * SQRT3_2 / 3.0; |
||
let x4 = x0 + 2.0 * dx/3.0; |
let x4 = x0 + 2.0 * dx / 3.0; |
||
let y4 = y0 + 2.0 * dy/3.0; |
let y4 = y0 + 2.0 * dy / 3.0; |
||
data = koch_curve(data, x0, y0, x2, y2, order - 1); |
data = koch_curve(data, x0, y0, x2, y2, order - 1); |
||
data = koch_curve(data, x2, y2, x3, y3, order - 1); |
data = koch_curve(data, x2, y2, x3, y3, order - 1); |
||
Line 1,828: | Line 1,827: | ||
} |
} |
||
fn write_koch_snowflake(file |
fn write_koch_snowflake(file: &str, size: usize, order: usize) -> std::io::Result<()> { |
||
order : usize) -> std::io::Result<()> { |
|||
let length = (size as f64) * SQRT3_2 * 0.95; |
let length = (size as f64) * SQRT3_2 * 0.95; |
||
let x0 = ((size as f64) - length)/2.0; |
let x0 = ((size as f64) - length) / 2.0; |
||
let y0 = (size as f64)/2.0 - length * SQRT3_2/3.0; |
let y0 = (size as f64) / 2.0 - length * SQRT3_2 / 3.0; |
||
let x1 = x0 + length/2.0; |
let x1 = x0 + length / 2.0; |
||
let y1 = y0 + length * SQRT3_2; |
let y1 = y0 + length * SQRT3_2; |
||
let x2 = x0 + length; |
let x2 = x0 + length; |