Tabs > Spaces

This commit is contained in:
Arne Keller 2019-04-04 15:11:09 +02:00
parent 142b8c02e6
commit 9ab79445ba
2 changed files with 55 additions and 55 deletions

View File

@ -9,49 +9,49 @@ use std::fs;
use super::*;
pub(crate) fn save_tri(filename: &str, tri: Triangle<f32>) {
fs::write(filename, generate_svg(&[tri])).unwrap();
fs::write(filename, generate_svg(&[tri])).unwrap();
}
pub(crate) fn save_world(filename: &str, world: &World) {
fs::write(
filename,
generate_svg(
&world
.tris
.iter()
.map(|(_idx, tri)| *tri)
.collect::<Vec<_>>(),
),
)
.unwrap();
fs::write(
filename,
generate_svg(
&world
.tris
.iter()
.map(|(_idx, tri)| *tri)
.collect::<Vec<_>>(),
),
)
.unwrap();
}
pub(crate) fn generate_svg(tris: &[Triangle<f32>]) -> String {
let mut document = Document::new()
// view box at (x,y), (w,h)
.set("viewBox", (-350.0, -150.0, 360.0, 150.0))
.set("xmlns:xlink", "http://www.w3.org/1999/xlink");
let mut document = Document::new()
// view box at (x,y), (w,h)
.set("viewBox", (-350.0, -150.0, 360.0, 150.0))
.set("xmlns:xlink", "http://www.w3.org/1999/xlink");
for tri in tris {
let data = Data::new()
.move_to((tri.0.x, -tri.0.y))
.line_to((tri.1.x, -tri.1.y))
.line_to((tri.2.x, -tri.2.y))
.close();
document.append(
Path::new()
.set("fill", "none")
.set("stroke", "black")
.set("stroke-width", 0.4)
.set("d", data),
);
document.append(
Circle::new()
.set("fill", "red")
.set("cx", tri.0.x)
.set("cy", -tri.0.y)
.set("r", 0.3),
);
}
for tri in tris {
let data = Data::new()
.move_to((tri.0.x, -tri.0.y))
.line_to((tri.1.x, -tri.1.y))
.line_to((tri.2.x, -tri.2.y))
.close();
document.append(
Path::new()
.set("fill", "none")
.set("stroke", "black")
.set("stroke-width", 0.4)
.set("d", data),
);
document.append(
Circle::new()
.set("fill", "red")
.set("cx", tri.0.x)
.set("cy", -tri.0.y)
.set("r", 0.3),
);
}
document.to_string()
document.to_string()
}

View File

@ -4,27 +4,27 @@ use std::io;
use std::io::prelude::*;
pub(crate) fn read_input() -> Vec<Triangle<f32>> {
read_stdin()
read_stdin()
}
fn read_stdin() -> Vec<Triangle<f32>> {
let stdin = io::stdin();
let stdin = stdin.lock();
let stdin = io::stdin();
let stdin = stdin.lock();
let mut lines = stdin.lines().map(Result::unwrap);
let tri_count = lines.next().unwrap().parse().unwrap();
let mut tris = Vec::with_capacity(tri_count);
let mut lines = stdin.lines().map(Result::unwrap);
let tri_count = lines.next().unwrap().parse().unwrap();
let mut tris = Vec::with_capacity(tri_count);
for _ in 0..tri_count {
let line = lines.next().unwrap();
let n = line
.trim()
.split(' ')
.skip(1)
.map(|x| x.parse::<f32>().unwrap())
.collect::<Vec<_>>();
tris.push([[n[0], n[1]], [n[2], n[3]], [n[4], n[5]]].into());
}
for _ in 0..tri_count {
let line = lines.next().unwrap();
let n = line
.trim()
.split(' ')
.skip(1)
.map(|x| x.parse::<f32>().unwrap())
.collect::<Vec<_>>();
tris.push([[n[0], n[1]], [n[2], n[3]], [n[4], n[5]]].into());
}
tris
tris
}