diff options
Diffstat (limited to 'src/lib.rs')
-rw-r--r-- | src/lib.rs | 61 |
1 files changed, 33 insertions, 28 deletions
@@ -16,29 +16,6 @@ pub struct Bezier { pub dpy: Poly, } -pub fn to_poly(v: &Vec<f32>) -> Poly { - match v.len() { - 0 => Poly::new(vec![0.0]), - 1 => Poly::new(vec![v[0]]), - 2 => Poly::new(vec![v[0], v[1] - v[0]]), - _ => { - let mut pv = Vec::new(); - for i in 0..v.len() - 1 { - pv.push(Poly::new(vec![v[i], v[i + 1] - v[i]])); - } - while pv.len() > 1 { - let mut npv = Vec::new(); - for i in 0..pv.len() - 1 { - let c = &pv[i + 1] - &pv[i]; - npv.push(&pv[i] + &c.shift()); - } - pv = npv; - } - pv[0].clone() - } - } -} - impl Bezier { pub fn new() -> Bezier { Bezier { @@ -169,10 +146,9 @@ impl Bezier { float inside(vec2 pos) {{ float closest = inf; float t = 0.0; - float dstep = 0.0; - while (t < 1.0 - dstep) {{ - dstep = step_size / length(vec2(eval_dpx(t), eval_dpy(t))); - float step = t + dstep; + while (t < 1.0) {{ + float dstep = step_size / length(vec2(eval_dpx(t), eval_dpy(t))); + float step = min(t + dstep, 1.0); vec2 a = vec2(eval_px(t), eval_py(t)); vec2 b = vec2(eval_px(step), eval_py(step)); float c = pow(length(a - b), 2); @@ -226,6 +202,29 @@ impl Bezier { } } +fn to_poly(v: &Vec<f32>) -> Poly { + match v.len() { + 0 => Poly::new(vec![0.0]), + 1 => Poly::new(vec![v[0]]), + 2 => Poly::new(vec![v[0], v[1] - v[0]]), + _ => { + let mut pv = Vec::new(); + for i in 0..v.len() - 1 { + pv.push(Poly::new(vec![v[i], v[i + 1] - v[i]])); + } + while pv.len() > 1 { + let mut npv = Vec::new(); + for i in 0..pv.len() - 1 { + let c = &pv[i + 1] - &pv[i]; + npv.push(&pv[i] + &c.shift()); + } + pv = npv; + } + pv[0].clone() + } + } +} + fn shader_from_source(source: &CStr, shader_type: GLenum) -> GLuint { unsafe { let shader: GLuint = gl::CreateShader(shader_type); @@ -234,7 +233,13 @@ fn shader_from_source(source: &CStr, shader_type: GLenum) -> GLuint { let mut success: gl::types::GLint = 1; gl::GetShaderiv(shader, gl::COMPILE_STATUS, &mut success); if success == 0 { - println!("Compilation error: {}", source.to_str().unwrap()); + let mut length: GLint = 0; + gl::GetShaderiv(shader, gl::INFO_LOG_LENGTH, &mut length); + let mut buffer: Vec<u8> = Vec::with_capacity(length as usize + 1); + buffer.extend([b' '].iter().cycle().take(length as usize)); + let error: CString = CString::from_vec_unchecked(buffer); + gl::GetShaderInfoLog(shader, length, std::ptr::null_mut(), error.as_ptr() as *mut GLchar); + println!("Compilation error: {}", error.to_str().unwrap()); } shader } |