Tabs > Spaces
This commit is contained in:
parent
142b8c02e6
commit
9ab79445ba
@ -9,49 +9,49 @@ use std::fs;
|
|||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
pub(crate) fn save_tri(filename: &str, tri: Triangle<f32>) {
|
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) {
|
pub(crate) fn save_world(filename: &str, world: &World) {
|
||||||
fs::write(
|
fs::write(
|
||||||
filename,
|
filename,
|
||||||
generate_svg(
|
generate_svg(
|
||||||
&world
|
&world
|
||||||
.tris
|
.tris
|
||||||
.iter()
|
.iter()
|
||||||
.map(|(_idx, tri)| *tri)
|
.map(|(_idx, tri)| *tri)
|
||||||
.collect::<Vec<_>>(),
|
.collect::<Vec<_>>(),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn generate_svg(tris: &[Triangle<f32>]) -> String {
|
pub(crate) fn generate_svg(tris: &[Triangle<f32>]) -> String {
|
||||||
let mut document = Document::new()
|
let mut document = Document::new()
|
||||||
// view box at (x,y), (w,h)
|
// view box at (x,y), (w,h)
|
||||||
.set("viewBox", (-350.0, -150.0, 360.0, 150.0))
|
.set("viewBox", (-350.0, -150.0, 360.0, 150.0))
|
||||||
.set("xmlns:xlink", "http://www.w3.org/1999/xlink");
|
.set("xmlns:xlink", "http://www.w3.org/1999/xlink");
|
||||||
|
|
||||||
for tri in tris {
|
for tri in tris {
|
||||||
let data = Data::new()
|
let data = Data::new()
|
||||||
.move_to((tri.0.x, -tri.0.y))
|
.move_to((tri.0.x, -tri.0.y))
|
||||||
.line_to((tri.1.x, -tri.1.y))
|
.line_to((tri.1.x, -tri.1.y))
|
||||||
.line_to((tri.2.x, -tri.2.y))
|
.line_to((tri.2.x, -tri.2.y))
|
||||||
.close();
|
.close();
|
||||||
document.append(
|
document.append(
|
||||||
Path::new()
|
Path::new()
|
||||||
.set("fill", "none")
|
.set("fill", "none")
|
||||||
.set("stroke", "black")
|
.set("stroke", "black")
|
||||||
.set("stroke-width", 0.4)
|
.set("stroke-width", 0.4)
|
||||||
.set("d", data),
|
.set("d", data),
|
||||||
);
|
);
|
||||||
document.append(
|
document.append(
|
||||||
Circle::new()
|
Circle::new()
|
||||||
.set("fill", "red")
|
.set("fill", "red")
|
||||||
.set("cx", tri.0.x)
|
.set("cx", tri.0.x)
|
||||||
.set("cy", -tri.0.y)
|
.set("cy", -tri.0.y)
|
||||||
.set("r", 0.3),
|
.set("r", 0.3),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
document.to_string()
|
document.to_string()
|
||||||
}
|
}
|
||||||
|
34
src/input.rs
34
src/input.rs
@ -4,27 +4,27 @@ use std::io;
|
|||||||
use std::io::prelude::*;
|
use std::io::prelude::*;
|
||||||
|
|
||||||
pub(crate) fn read_input() -> Vec<Triangle<f32>> {
|
pub(crate) fn read_input() -> Vec<Triangle<f32>> {
|
||||||
read_stdin()
|
read_stdin()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_stdin() -> Vec<Triangle<f32>> {
|
fn read_stdin() -> Vec<Triangle<f32>> {
|
||||||
let stdin = io::stdin();
|
let stdin = io::stdin();
|
||||||
let stdin = stdin.lock();
|
let stdin = stdin.lock();
|
||||||
|
|
||||||
let mut lines = stdin.lines().map(Result::unwrap);
|
let mut lines = stdin.lines().map(Result::unwrap);
|
||||||
let tri_count = lines.next().unwrap().parse().unwrap();
|
let tri_count = lines.next().unwrap().parse().unwrap();
|
||||||
let mut tris = Vec::with_capacity(tri_count);
|
let mut tris = Vec::with_capacity(tri_count);
|
||||||
|
|
||||||
for _ in 0..tri_count {
|
for _ in 0..tri_count {
|
||||||
let line = lines.next().unwrap();
|
let line = lines.next().unwrap();
|
||||||
let n = line
|
let n = line
|
||||||
.trim()
|
.trim()
|
||||||
.split(' ')
|
.split(' ')
|
||||||
.skip(1)
|
.skip(1)
|
||||||
.map(|x| x.parse::<f32>().unwrap())
|
.map(|x| x.parse::<f32>().unwrap())
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
tris.push([[n[0], n[1]], [n[2], n[3]], [n[4], n[5]]].into());
|
tris.push([[n[0], n[1]], [n[2], n[3]], [n[4], n[5]]].into());
|
||||||
}
|
}
|
||||||
|
|
||||||
tris
|
tris
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user