pub struct JitterReducer {
alpha: f32,
last_val_x: f32,
last_val_y: f32,
initialized: bool,
}
impl JitterReducer {
pub fn new(alpha: f32) -> Self {
Self {
alpha: alpha.clamp(0.01, 1.0),
last_val_x: 0.0,
last_val_y: 0.0,
initialized: false,
}
}
pub fn process(&mut self, x: f32, y: f32) -> (f32, f32) {
if !self.initialized {
self.last_val_x = x;
self.last_val_y = y;
self.initialized = true;
return (x, y);
}
self.last_val_x = self.last_val_x + self.alpha * (x - self.last_val_x);
self.last_val_y = self.last_val_y + self.alpha * (y - self.last_val_y);
(self.last_val_x, self.last_val_y)
}
}