import java.awt.*;

/**
 * This mirrors circular areas of the image vertically.
 * (That is to say, the image that is within the 
 * specified boundaries is mirrored across the horizontal
 * axis.)
 * 
 * @author Michael Kolling and David J Barnes
 * @updates and changes by Michael Rivers
 * @version 1.0 May 14th, 2006
 */
public class CircleFilter extends Filter
{
    /**
     * Constructor for objects of class GrayscaleFilter
     */
    public CircleFilter(String name)
    {
        super(name);
    }

    /**
     * Apply this filter to an image.
     * 
     * @param  image  The image to be changed by this filter.
     */
    public void apply(OFImage image)
    {
        int height = image.getHeight();
        int width = image.getWidth();
        int h = Math.round(height/2);
        int w = Math.round(width/2);
        for(int y = 0; y < h; y++) {
            for(int x = 0; x < width; x++) {
                int d = h - y;
                /*
                if ((y < -1*Math.sqrt(h*h*25/16-((x-w)*(x-w)))+h)
                || ((y > -1*Math.sqrt(h*h-((x-w)*(x-w)))+h) 
                    && (y < -1*Math.sqrt(h*h*9/16-((x-w)*(x-w)))+h))
                || ((y > -1*Math.sqrt(h*h/4-((x-w)*(x-w)))+h) 
                    && (y < -1*Math.sqrt(h*h/16-((x-w)*(x-w)))+h)))
                {
                    */
                if (((y < -1*Math.sqrt(h*h*25/16-((x-w)*(x-w)))+h)
                    || (x > w + h*5/4) || (x < w - h*5/4))
                    || ((y > -1*Math.sqrt(h*h-((x-w)*(x-w)))+h) 
                        && ((y < -1*Math.sqrt(h*h*9/16-((x-w)*(x-w)))+h)
                            || (x > w + h*3/4) || (x < w - h*3/4)))
                    || ((y > -1*Math.sqrt(h*h/4-((x-w)*(x-w)))+h) 
                        && ((y < -1*Math.sqrt(h*h/16-((x-w)*(x-w)))+h)
                            || (x > w + h/4) || (x < w - h/4))))
                {
                    Color pixelOne = image.getPixel(x,y);
                    Color pixelTwo = image.getPixel(x,h+d);
                    image.setPixel(x,y,pixelTwo);
                    image.setPixel(x,h+d,pixelOne);
                }
            }
        }
    }
}
