mirror of https://github.com/fdehau/tui-rs
Improve canvas functionalities and fix clippy warnings
parent
25bb360f13
commit
f0979dfeee
@ -0,0 +1,65 @@
|
|||||||
|
use super::Shape;
|
||||||
|
use style::Color;
|
||||||
|
|
||||||
|
pub struct Line {
|
||||||
|
pub x1: f64,
|
||||||
|
pub y1: f64,
|
||||||
|
pub x2: f64,
|
||||||
|
pub y2: f64,
|
||||||
|
pub color: Color,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct LineIterator {
|
||||||
|
x: f64,
|
||||||
|
y: f64,
|
||||||
|
dx: f64,
|
||||||
|
dy: f64,
|
||||||
|
dir_x: f64,
|
||||||
|
dir_y: f64,
|
||||||
|
current: f64,
|
||||||
|
end: f64,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Iterator for LineIterator {
|
||||||
|
type Item = (f64, f64);
|
||||||
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
|
self.current += 1.0;
|
||||||
|
if self.current < self.end + 1.0 {
|
||||||
|
Some((self.x + (self.current * self.dx) / self.end * self.dir_x,
|
||||||
|
self.y + (self.current * self.dy) / self.end * self.dir_y))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> IntoIterator for &'a Line {
|
||||||
|
type Item = (f64, f64);
|
||||||
|
type IntoIter = LineIterator;
|
||||||
|
fn into_iter(self) -> Self::IntoIter {
|
||||||
|
let dx = self.x1.max(self.x2) - self.x1.min(self.x2);
|
||||||
|
let dy = self.y1.max(self.y2) - self.y1.min(self.y2);
|
||||||
|
let dir_x = if self.x1 <= self.x2 { 1.0 } else { -1.0 };
|
||||||
|
let dir_y = if self.y1 <= self.y2 { 1.0 } else { -1.0 };
|
||||||
|
let end = dx.max(dy);
|
||||||
|
LineIterator {
|
||||||
|
x: self.x1,
|
||||||
|
y: self.y1,
|
||||||
|
dx: dx,
|
||||||
|
dy: dy,
|
||||||
|
dir_x: dir_x,
|
||||||
|
dir_y: dir_y,
|
||||||
|
current: 0.0,
|
||||||
|
end: end,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Shape<'a> for Line {
|
||||||
|
fn color(&self) -> Color {
|
||||||
|
self.color
|
||||||
|
}
|
||||||
|
fn points(&'a self) -> Box<Iterator<Item = (f64, f64)> + 'a> {
|
||||||
|
Box::new(self.into_iter())
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,54 @@
|
|||||||
|
use super::Shape;
|
||||||
|
use style::Color;
|
||||||
|
|
||||||
|
pub struct Points<'a> {
|
||||||
|
pub coords: &'a [(f64, f64)],
|
||||||
|
pub color: Color,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Shape<'a> for Points<'a> {
|
||||||
|
fn color(&self) -> Color {
|
||||||
|
self.color
|
||||||
|
}
|
||||||
|
fn points(&'a self) -> Box<Iterator<Item = (f64, f64)> + 'a> {
|
||||||
|
Box::new(self.into_iter())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Default for Points<'a> {
|
||||||
|
fn default() -> Points<'a> {
|
||||||
|
Points {
|
||||||
|
coords: &[],
|
||||||
|
color: Color::Reset,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> IntoIterator for &'a Points<'a> {
|
||||||
|
type Item = (f64, f64);
|
||||||
|
type IntoIter = PointsIterator<'a>;
|
||||||
|
fn into_iter(self) -> Self::IntoIter {
|
||||||
|
PointsIterator {
|
||||||
|
coords: self.coords,
|
||||||
|
index: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct PointsIterator<'a> {
|
||||||
|
coords: &'a [(f64, f64)],
|
||||||
|
index: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Iterator for PointsIterator<'a> {
|
||||||
|
type Item = (f64, f64);
|
||||||
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
|
let point = if self.index < self.coords.len() {
|
||||||
|
Some(self.coords[self.index])
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
self.index += 1;
|
||||||
|
point
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue