Circle.java

/**
A simple <code>Circle</code> class for homework #2.

@author Walt Leipold
*/
public class Circle extends Shape implements PrinterPlottable
{
    /**
    Construct a <code>Circle</code> object.

    @param x x-coordinate of center
    @param y y-coordinate of center
    @param r radius of circle
    (<code>x</code>,<code>y</code>) and radius (<code>r</code>).
    */
    public Circle(double x,double y,double r)
    {
        super(x,y);
        this.radius = r;
    }


    /** 
    @return the radius of this <code>Circle</code>
    */
    public double getRadius()
    {
        return radius;
    }


    /** 
    @return a <code>String</code> describing this <code>Circle</code>
    */
    public String toString()
    {
        return getClass().getName() + "[" +
            "x=" + formatNum(getX()) + "," +
            "y=" + formatNum(getY()) + "," +
            "r=" + formatNum(radius) + "]";
    }


    /**
    Draw this <code>Circle</code> on the given plot context
    using a specified character.

    @param pc the <code>PlotContext</code> to draw on
    @param drawChar the character to draw with
    */
    public void draw(PlotContext pc,char drawChar)
    {
        // Test all points in a rectangular region for distance
        // from center of Circle, draw if < radius.
        int i1 = (int)Math.round(getX()-radius-0.5);
        int i2 = (int)Math.round(getX()+radius+0.5);
        int j1 = (int)Math.round(getY()-radius-0.5);
        int j2 = (int)Math.round(getY()+radius+0.5);
        for (int i=i1; i<i2; i++) {
            for (int j=j1; j<j2; j++) {
                double xx = (double)i + 0.5;
                double yy = (double)j + 0.5;
                double r = Math.sqrt(
                    (getX()-xx)*(getX()-xx) + 
                    (getY()-yy)*(getY()-yy));
                if (r <= radius)
                    pc.setPoint(i,j,drawChar);
                }
            }
    }


    private double radius;
}