package org.mote.wiimote.whiteboard.mouse.smoothing;

import java.awt.Point;
import java.util.LinkedList;

public class SimpleMovingAverage implements MouseSmoothingStrategy {
	
	public final static String NAME = "Simple Moving Average";
	
	private static final int N = 7;
	private LinkedList<Point> points = new LinkedList<Point>();	

	public Point translate(Point p) {
		return getAverage(p);
	}
	
	public void reset() {
		if (!points.isEmpty()) points.clear();
	}

	private Point getAverage(Point p) {
		// add new and remove old
		points.addLast(p);
		if (points.size() > N) points.removeFirst();
		
		// get average of last N points
		int x = 0, y = 0;
		for (Point o : points) {
			x += o.x;
			y += o.y;
		}
		
		return new Point(Math.round((float)x / (float)points.size()), Math.round((float)y / (float)points.size()));
	}	

}
