Read input from stdin

This commit is contained in:
Arne Keller 2019-01-24 19:13:19 +01:00
parent a926986127
commit 698346e1c6
4 changed files with 64 additions and 5 deletions

3
lisarennt1.txt Normal file
View File

@ -0,0 +1,3 @@
1
3 535 410 610 70 460 70
633 189

View File

@ -11,7 +11,7 @@ use super::*;
pub(crate) fn dump_svg(points: &[(Point, &'static str)], lines: &[(Line, &'static str)], polys: &[Polygon]) { pub(crate) fn dump_svg(points: &[(Point, &'static str)], lines: &[(Line, &'static str)], polys: &[Polygon]) {
let mut document = Document::new() let mut document = Document::new()
// view box at (0, -200), w*h (200, 200) // view box at (0, -200), w*h (200, 200)
.set("viewBox", (0, -200, 200, 200)); .set("viewBox", (0, -1100, 2100, 2100));
for circle in points.iter().map(|(p, color)| { for circle in points.iter().map(|(p, color)| {
Circle::new() Circle::new()
@ -31,7 +31,7 @@ pub(crate) fn dump_svg(points: &[(Point, &'static str)], lines: &[(Line, &'stati
Path::new() Path::new()
.set("fill", "none") .set("fill", "none")
.set("stroke", *color) .set("stroke", *color)
.set("stroke-width", 0.5) .set("stroke-width", 0.25)
.set("d", data) .set("d", data)
}) { }) {
document.append(path); document.append(path);

View File

@ -1,11 +1,42 @@
use super::*; use super::*;
use std::io;
use std::io::prelude::*;
pub(crate) struct InputData { pub(crate) struct InputData {
pub start: Point, pub start: Point,
pub polys: Vec<Polygon> pub polys: Vec<Polygon>
} }
pub(crate) fn read_input() -> InputData { pub(crate) fn read_input() -> InputData {
//custom_input()
read_stdin()
}
fn read_stdin() -> InputData {
let stdin = io::stdin();
let stdin = stdin.lock();
let mut lines = stdin.lines().map(Result::unwrap);
let polygon_count = lines.next().unwrap().parse().unwrap();
let mut polygons = Vec::with_capacity(polygon_count);
for _ in 0..polygon_count {
let line = lines.next().unwrap();
let numbers = line.trim().split(' ').map(|x| x.parse::<usize>().unwrap()).collect::<Vec<_>>();
let corner_count = numbers[0];
polygons.push(Polygon::new((0..corner_count).map(|idx| ((numbers[idx*2+1] as f64).into(), (numbers[idx*2+2] as f64).into())).collect::<Vec<_>>().into(), vec![]));
}
let home = lines.next().unwrap().trim().split(' ').map(|x| x.parse::<f64>().unwrap().into()).collect::<Vec<_>>();
InputData {
start: Point::new(home[0], home[1]),
polys: polygons
}
}
fn custom_input() -> InputData {
let mut polys = vec![]; let mut polys = vec![];
polys.push(Polygon::new(vec![[2.0.into(), 0.0.into()], [6.0.into(), 0.0.into()], [6.0.into(), 6.0.into()], [2.0.into(), 5.0.into()]].into(), vec![])); polys.push(Polygon::new(vec![[2.0.into(), 0.0.into()], [6.0.into(), 0.0.into()], [6.0.into(), 6.0.into()], [2.0.into(), 5.0.into()]].into(), vec![]));
polys.push(Polygon::new(vec![[1.0.into(), 20.0.into()], [2.0.into(), 20.0.into()], [2.0.into(), 23.0.into()], [1.0.into(), 23.0.into()]].into(), vec![])); polys.push(Polygon::new(vec![[1.0.into(), 20.0.into()], [2.0.into(), 20.0.into()], [2.0.into(), 23.0.into()], [1.0.into(), 23.0.into()]].into(), vec![]));

View File

@ -39,11 +39,17 @@ impl cmp::PartialOrd for RunState {
} }
fn main() { fn main() {
let bus = Point::new(0.0.into(), 0.0.into()); let bus = Point::new(0.0.into(), (-1300.0).into());
let data = input::read_input(); let data = input::read_input();
let house = data.start; let house = data.start;
let polys = data.polys; let polys = data.polys;
eprintln!("{:?}", house);
eprintln!("{:?}", polys);
for p in &polys {
assert!(p.interiors.is_empty());
//assert!(p.is_convex());
}
let points = polys.iter().map(|x| x.exterior.0.clone()).flatten().collect::<Vec<_>>(); let points = polys.iter().map(|x| x.exterior.0.clone()).flatten().collect::<Vec<_>>();
let start = RunState { let start = RunState {
@ -125,6 +131,10 @@ fn main() {
} }
eprintln!("d = {:?}", best_delay); eprintln!("d = {:?}", best_delay);
let route = best; let route = best;
eprintln!("Route:");
for s in &route {
eprintln!("{:.02},{:.02}", s.pos.x(), s.pos.y());
}
let first = route.first().unwrap(); let first = route.first().unwrap();
let last = route.last().unwrap(); let last = route.last().unwrap();
let points = vec![(house, "red"), (first.bus.translate(0.0.into(), best_delay), "yellow"), (last.bus, "orange")]; let points = vec![(house, "red"), (first.bus.translate(0.0.into(), best_delay), "yellow"), (last.bus, "orange")];
@ -143,11 +153,26 @@ fn float_range(a: f64, b: f64) -> impl Iterator<Item = f64> {
} }
fn none_intersect(polys: &[Polygon], line: &Line) -> bool { fn none_intersect(polys: &[Polygon], line: &Line) -> bool {
/*
if line.end.x > 0.0 {
eprintln!("{:?}", line);
}
*/
for p in polys { for p in polys {
for l in p.exterior.lines() { for l in p.exterior.lines().chain(vec![Line::new(p.exterior.0[0], *p.exterior.0.last().unwrap())]) {
if l.start == line.start || l.end == line.end || l.start == line.end || l.end == line.start { if l.start == line.start || l.end == line.end || l.start == line.end || l.end == line.start {
/*
if line.end.x > 0.0 {
eprintln!("skipping {:?}", l);
}
*/
continue; // would always intersect with itself continue; // would always intersect with itself
} }
/*
if line.end.x > 0.0 {
eprintln!("intersect with {:?}", l);
}
*/
if l.intersects(line) { if l.intersects(line) {
return false; return false;
} }