From 727b1c2ee98d395429ec1e667279fc3f5fec3a5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Manuel=20Tom=C3=A1s?= Date: Sun, 17 Jan 2021 17:51:07 -0300 Subject: Fix fragment shader bug on empty curves --- src/lerp.rs | 6 ++++-- src/main.rs | 6 +++--- src/poly.rs | 69 +------------------------------------------------------------ 3 files changed, 8 insertions(+), 73 deletions(-) (limited to 'src') diff --git a/src/lerp.rs b/src/lerp.rs index 222671f..1501d0f 100644 --- a/src/lerp.rs +++ b/src/lerp.rs @@ -4,6 +4,7 @@ use crate::poly::Poly; pub enum Lerp { Node(Box, Box), Leaf(Number, Number), + Just(Number) } impl Lerp { @@ -13,8 +14,8 @@ impl Lerp { fn new_s(v: &[Number]) -> Box { match v.len() { - 0 => Box::new(Lerp::Leaf(0.0, 0.0)), - 1 => Box::new(Lerp::Leaf(v[0], v[0])), + 0 => Box::new(Lerp::Just(0.0)), + 1 => Box::new(Lerp::Just(v[0])), 2 => Box::new(Lerp::Leaf(v[0], v[1])), _ => Box::new(Lerp::Node( Lerp::new_s(&v[0..v.len() - 1]), @@ -26,6 +27,7 @@ impl Lerp { pub fn lp(l: Box) -> Poly { match *l { + Lerp::Just(a) => Poly::new(vec![a]), Lerp::Leaf(a, b) => Poly::new(vec![a, b - a]), Lerp::Node(a, b) => { let a = lp(a); diff --git a/src/main.rs b/src/main.rs index 242a1ef..9f9ad9f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -26,8 +26,8 @@ fn shader_from_source(source: &CStr, shader_type: GLenum) -> GLuint { } fn main() { - let a = lerp::lp(Lerp::new(vec![100.0, 200.0, 800.0, 400.0, 800.0, 100.0])); - let b = lerp::lp(Lerp::new(vec![100.0, 550.0, 500.0, 300.0, 200.0, 800.0])); + let a = lerp::lp(Lerp::new(vec![200.0, 500.0, 500.0])); + let b = lerp::lp(Lerp::new(vec![200.0, 500.0, 200.0])); let sdl_context = sdl2::init().unwrap(); let video_subsystem = sdl_context.video().unwrap(); @@ -110,7 +110,7 @@ float inside(vec2 pos) {{ }} t = step; }} - return 1 - pow(closest / thr, 2); + return 1 - closest / thr; }} void main() {{ diff --git a/src/poly.rs b/src/poly.rs index 34df7a0..bae3a15 100644 --- a/src/poly.rs +++ b/src/poly.rs @@ -3,7 +3,7 @@ mod iter; use crate::number::Number; use iter::Iter; use std::fmt; -use std::ops::{Add, Div, Mul, Sub}; +use std::ops::{Add, Mul, Sub}; #[derive(PartialEq, Debug, Clone)] pub struct Poly { @@ -34,22 +34,10 @@ impl Poly { self.degree } - fn lc(&self) -> Number { - self.data[self.degree()] - } - fn iter(&self) -> Iter { Iter::new(self.data.clone(), self.degree()) } - pub fn eval(&self, n: Number) -> Number { - let mut r = 0.0; - for i in 0..self.data.len() { - r += self.data[i] * n.powi(i as i32); - } - r - } - fn is_zero(&self) -> bool { for i in 0..self.data.len() { if self.data[i] != 0.0 { @@ -95,21 +83,6 @@ impl Mul for &Poly { } } -impl Div for &Poly { - type Output = (Poly, Poly); - - fn div(self, divisor: Self) -> (Poly, Poly) { - let mut q = Poly::new(vec![0.0]); - let mut r = self.clone(); - while !r.is_zero() && r.degree() >= divisor.degree() { - let s = Poly::mono(r.degree() - divisor.degree(), r.lc() / divisor.lc()); - q = &q + &s; - r = &r - &(&s * divisor); - } - (q, r) - } -} - impl fmt::Display for &Poly { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let mut s = String::new(); @@ -127,39 +100,6 @@ impl fmt::Display for &Poly { mod tests { use super::*; - #[test] - fn derivative_test() { - let p = Poly::new(vec![1.0, 2.0, 3.0, 4.0, 5.0]); - assert_eq!(derivative(&p), Poly::new(vec![2.0, 6.0, 12.0, 20.0])); - } - - #[test] - fn div_equal() { - let a = Poly::new(vec![6.0, 7.0, 1.0]); - let b = Poly::new(vec![-6.0, -5.0, 1.0]); - assert_eq!( - &a / &b, - (Poly::new(vec![1.0]), Poly::new(vec![12.0, 12.0, 0.0])) - ); - } - - #[test] - fn div_greater() { - let a = Poly::new(vec![-6.0, -5.0, 1.0]); - let b = Poly::new(vec![12.0, 12.0]); - assert_eq!( - &a / &b, - (Poly::new(vec![-0.5, 1.0 / 12.0]), Poly::new(vec![0.0, 0.0])) - ); - } - - #[test] - fn div_less() { - let a = Poly::new(vec![12.0, 12.0]); - let b = Poly::new(vec![-6.0, -5.0, 1.0]); - assert_eq!(&a / &b, (Poly::new(vec![0.0]), a)); - } - #[test] fn mul_test() { let a = Poly::new(vec![1.0, 2.0, 3.0]); @@ -204,11 +144,4 @@ mod tests { let p = Poly::new(vec![0.0; 6]); assert_eq!(p.degree(), 0); } - - #[test] - fn gcd_test() { - let a = Poly::new(vec![2.0, -8.0, 8.0]); - let b = Poly::new(vec![-1.0, 4.0, -1.0]); - assert_eq!(gcd(&a, &b), Poly::new(vec![-0.0625, 0.0])); - } } -- cgit v1.2.3