use super::{Direction, TextAlignment};
fn distance(x1: isize, y1: isize, x2: isize, y2: isize) -> f64 {
let x = ((x2-x1).pow(2) + (y2-y1).pow(2)) as f64;
x.sqrt()
}
pub fn pixel(x: isize, y: isize, c: char) {
if x >= 0 && y >= 0 {
super::cursorto(x as usize, y as usize);
print!("{}", c);
}
}
pub fn straight_line(mut x: isize, mut y: isize, mut length: isize, dir: Direction, c: char) {
let mut addx: isize = 0;
let mut addy: isize = 0;
match dir {
Direction::Left => addx = -1,
Direction::Right => addx = 1,
Direction::Up => addy = -1,
Direction::Down => addy = 1
}
if length < 0 {
length = -length;
addx = -addx;
addy = -addy;
}
for _ in 0..length {
pixel(x, y, c);
x += addx;
y += addy;
}
}
pub fn rectangle(x: isize, y: isize, width: usize, height: usize, c: char, fill: bool) {
let w = width as isize;
let h = height as isize;
if fill {
for i in 0..width {
straight_line(x+i as isize, y, height as isize, Direction::Down, c);
}
return;
}
straight_line(x, y, w, Direction::Right, c);
straight_line(x, y+h-1, w, Direction::Right, c);
straight_line(x, y+1, h-2, Direction::Down, c);
straight_line(x+w-1, y+1, h-2, Direction::Down, c);
}
pub fn line(x1: isize, y1: isize, x2: isize, y2: isize, c: char) {
if x1 == x2 {
straight_line(x1, y1, y2-y1, Direction::Down, c);
}
if y1 == y2 {
straight_line(x1, y1, x2-x1, Direction::Right, c);
}
let dist = distance(x1, y1, x2, y2);
let dx = (x2-x1) as f64 / dist;
let dy = (y2-y1) as f64 / dist;
let mut x = x1 as f64;
let mut y = y1 as f64;
for _ in 0..dist.round() as isize {
pixel(x.round() as isize, y.round() as isize, c);
x += dx;
y += dy;
}
pixel(x2, y2, c);
}
pub fn ellipse(h: isize, k: isize, a: usize, b: usize, c: char, fill: bool) {
for x in 0..a*2+1 {
let shiftx: isize = x as isize + h - a as isize;
let inside_y = ((a*a) as isize - (shiftx-h).pow(2)).abs() as f64;
let y: f64 = (b as f64) / (a as f64) * inside_y.sqrt() + k as f64;
if fill {
let ydist: isize = 2 * (k - y.round() as isize).abs();
straight_line(shiftx as isize, y.round() as isize, ydist+1, Direction::Up, c);
continue;
}
let ydist: isize = 2 * (k - y.round() as isize);
pixel(shiftx, y.round() as isize, c);
pixel(shiftx, (y.round() as isize + ydist) as isize, c);
}
}
pub fn text(x: isize, y: isize, text: &str) {
for (i, c) in text.chars().enumerate() {
pixel(x+i as isize, y, c);
}
}
pub fn text_aligned(x: isize, y: isize, text: &str, align: TextAlignment) {
let mut x = x;
match align {
TextAlignment::Left => {},
TextAlignment::Center => x -= text.len() as isize / 2,
TextAlignment::Right => x -= text.len() as isize
}
for (i, c) in text.chars().enumerate() {
pixel(x+i as isize, y, c);
}
}