Add circle fill mode

This commit is contained in:
Nicolas 2022-04-08 21:11:00 +02:00
parent 64933b7213
commit 363909bf1c
3 changed files with 72 additions and 14 deletions

40
src/fill/circle.rs Normal file
View File

@ -0,0 +1,40 @@
use crate::fill::FillMode;
use crate::vec::Vector;
const INTERVAL: f32 = 4.0;
pub struct CircleFillMode {
center: Vector,
interval: f32
}
impl CircleFillMode {
pub fn new(size: Vector) -> Self {
Self {
center: size.center(),
interval: size.smaller() / INTERVAL,
}
}
}
impl FillMode for CircleFillMode {
fn sample(&self, _: f32, pos: Vector) -> f32 {
((pos - self.center).length() % self.interval) / self.interval
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn sample() {
let fill = CircleFillMode::new(Vector::new(10.0, 8.0));
let sample_1 = fill.sample(0.0, Vector::new(5.0, 3.0));
let sample_2 = fill.sample(0.0, Vector::new(8.5, 4.0));
assert!(0.4 < sample_1 && sample_1 < 0.6);
assert!(0.7 < sample_2 && sample_2 < 0.8);
}
}

View File

@ -1,4 +1,5 @@
mod level;
mod circle;
use crate::vec::Vector;

View File

@ -22,6 +22,10 @@ impl Vector {
(self.x * self.x + self.y * self.y).sqrt()
}
pub fn smaller(self) -> f32 {
self.x.min(self.y)
}
/// Creates a vector with the on screen coordinates based on the terminal coordinates.
/// # Arguments
/// * `x`: The x axis of the terminal character.
@ -51,6 +55,27 @@ mod test {
assert_eq!(5.0, vec.y);
}
#[test]
fn center() {
let vec = Vector::new(3.0, 8.0);
assert_eq!(1.5, vec.center().x);
assert_eq!(4.0, vec.center().y);
}
#[test]
fn length() {
let vec = Vector::new(3.0, 6.0);
assert!(6.7 < vec.length() && vec.length() < 6.8);
}
#[test]
fn smaller() {
assert_eq!(4.0, Vector::new(7.0, 4.0).smaller());
assert_eq!(2.0, Vector::new(2.0, 9.0).smaller());
}
#[test]
fn from_terminal() {
let vec = Vector::from_terminal(2, 4);
@ -60,20 +85,12 @@ mod test {
}
#[test]
fn copy() {
let vec = Vector::new(2.0, 4.0);
let copy = vec;
fn sub() {
let left = Vector::new(8.0, 15.0);
let right = Vector::new(2.0, 4.0);
let result = left - right;
assert_eq!(vec.x, copy.x);
assert_eq!(vec.y, copy.y);
}
#[test]
fn clone() {
let vec = Vector::new(2.0, 4.0);
let clone = vec.clone();
assert_eq!(vec.x, clone.x);
assert_eq!(vec.y, clone.y);
assert_eq!(6.0, result.x);
assert_eq!(11.0, result.y);
}
}