-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Lawrence Niu
committed
Sep 24, 2022
1 parent
c38606d
commit 7a12007
Showing
5 changed files
with
166 additions
and
148 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
use crate::geometry; | ||
use std::collections::hash_map; | ||
use std::collections::HashMap; | ||
|
||
pub struct Canvas { | ||
mapping: HashMap<geometry::Point, u32>, | ||
} | ||
|
||
impl Canvas { | ||
pub fn new() -> Self { | ||
Canvas { | ||
mapping: HashMap::new(), | ||
} | ||
} | ||
|
||
fn count_point(&mut self, point: geometry::Point) { | ||
self.mapping | ||
.entry(point) | ||
.and_modify(|count| { | ||
*count += 1; | ||
}) | ||
.or_insert(1); | ||
} | ||
|
||
pub fn count_line(&mut self, line: &geometry::Line) { | ||
line.trace().into_iter().for_each(|p| self.count_point(p)); | ||
} | ||
|
||
pub fn iter(&self) -> hash_map::Iter<'_, geometry::Point, u32> { | ||
self.mapping.iter() | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
use std::cmp; | ||
use std::error; | ||
use std::fmt; | ||
use std::str; | ||
|
||
#[derive(Debug)] | ||
pub struct ParsePointError(String); | ||
|
||
impl fmt::Display for ParsePointError { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!(f, "failed to parse point from string.\n {}", self.0) | ||
} | ||
} | ||
|
||
impl error::Error for ParsePointError {} | ||
|
||
#[derive(Eq, Hash, PartialEq)] | ||
pub struct Point { | ||
pub x: u32, | ||
pub y: u32, | ||
} | ||
|
||
impl str::FromStr for Point { | ||
type Err = ParsePointError; | ||
|
||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
let mut tokens = s.split(','); | ||
let x = tokens | ||
.next() | ||
.ok_or(ParsePointError(format!("missing x in \"x,y\""))) | ||
.and_then(|x_str| { | ||
x_str.trim().parse().map_err(|parse_err| { | ||
ParsePointError(format!("failed to parse x.\n {}", parse_err)) | ||
}) | ||
})?; | ||
let y = tokens | ||
.next() | ||
.ok_or(ParsePointError(format!("missing y in \"x,y\""))) | ||
.and_then(|y_str| { | ||
y_str.trim().parse().map_err(|parse_err| { | ||
ParsePointError(format!("failed to parse y.\n {}", parse_err)) | ||
}) | ||
})?; | ||
Ok(Point { x, y }) | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct ParseLineError(String); | ||
|
||
impl fmt::Display for ParseLineError { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!(f, "failed to parse line from string.\n {}", self.0) | ||
} | ||
} | ||
|
||
impl error::Error for ParseLineError {} | ||
|
||
pub struct Line { | ||
pub from: Point, | ||
pub to: Point, | ||
} | ||
|
||
impl str::FromStr for Line { | ||
type Err = ParseLineError; | ||
|
||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
let mut tokens = s.split_whitespace(); | ||
let from = tokens | ||
.next() | ||
.ok_or(ParseLineError(format!("could not find \"from\" point."))) | ||
.and_then(|from_str| { | ||
from_str.parse().map_err(|parse_err| { | ||
ParseLineError(format!("could not parse \"from\" point.\n {}", parse_err)) | ||
}) | ||
})?; | ||
tokens.next(); // -> symbol which is ignored | ||
let to = tokens | ||
.next() | ||
.ok_or(ParseLineError(format!("could not find \"to\" point."))) | ||
.and_then(|to_str| { | ||
to_str.parse().map_err(|parse_err| { | ||
ParseLineError(format!("could not parse \"to\" point.\n {}", parse_err)) | ||
}) | ||
})?; | ||
Ok(Line { from, to }) | ||
} | ||
} | ||
|
||
impl Line { | ||
pub fn is_diagonal(&self) -> bool { | ||
self.from.x != self.to.x && self.from.y != self.to.y | ||
} | ||
|
||
pub fn trace(&self) -> Vec<Point> { | ||
if self.is_diagonal() { | ||
unimplemented!(); | ||
} | ||
let is_vertical = self.from.x == self.to.x; | ||
let ((a, b), pivot_value) = if is_vertical { | ||
((self.from.y, self.to.y), self.from.x) | ||
} else { | ||
((self.from.x, self.to.x), self.from.y) | ||
}; | ||
|
||
(cmp::min(a, b)..=cmp::max(a, b)) | ||
.map(|value| { | ||
if is_vertical { | ||
Point { | ||
x: pivot_value, | ||
y: value, | ||
} | ||
} else { | ||
Point { | ||
x: value, | ||
y: pivot_value, | ||
} | ||
} | ||
}) | ||
.collect() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters