Commit d4c25358 authored by Ari Trachtenberg's avatar Ari Trachtenberg
Browse files

Working version

parent 263a9b09
Loading
Loading
Loading
Loading
+39 −9
Original line number Diff line number Diff line
@@ -14,7 +14,7 @@ public abstract class Canvas implements Iterable<CanvasPoint> {
     * @param theXsize The X dimension of the support plain for points.
     * @param theYsize The Y dimension of the support plain for points.
     */
    public Canvas(float theXsize, float theYsize) {
    public Canvas(double theXsize, double theYsize) {
        Xsize=theXsize; Ysize = theYsize;
        points = new ArrayList<>();
    }
@@ -28,7 +28,7 @@ public abstract class Canvas implements Iterable<CanvasPoint> {
     * @note: You may want to override this method.
     */
    public void addPoint(CanvasPoint pnt) {
        _check(pnt);
        _checkPoint(pnt);
        points.add(pnt); }

    /**
@@ -40,7 +40,7 @@ public abstract class Canvas implements Iterable<CanvasPoint> {
     * @note: You may want to override this method.
     */
    public  boolean deletePoint(CanvasPoint pnt) {
        _check(pnt);
        _checkPoint(pnt);
        return points.remove(pnt); }

    /**
@@ -62,18 +62,48 @@ public abstract class Canvas implements Iterable<CanvasPoint> {
        return points.iterator();
    }

    private void _check(CanvasPoint pnt) {
    /**
     *
     * @param centers
     * @return true iff unit circles centered at the points in <code>centers</code>
     *    cover all the points in this data structure.
     */
    public boolean checkCovering(ArrayList<CanvasPoint> centers) {
        // check each point, one by one
        for (CanvasPoint pnt: this) {
            boolean foundCenter = false;
            // look at each center to see if one of them is of distance <=1 from the point
            for (CanvasPoint center: centers) {
                if (_distance(center,pnt) <= 1.0f) {
                    foundCenter = true;
                    break;
                }
            }
            if (!foundCenter)
                return false;
        }
        return true;
    }

    private void _checkPoint(CanvasPoint pnt) {
        if ((pnt.x<0) || (pnt.x>Xsize) || (pnt.x<0) || (pnt.y>Ysize))
            throw new RuntimeException("Point coordinates off canvas");
    }

    private double _distance(CanvasPoint pnt1, CanvasPoint pnt2) {
        return Math.sqrt(
            Math.pow(pnt1.x - pnt2.x,2) +
            Math.pow(pnt1.y - pnt2.y,2)
        );
    }

    // NESTED SUBCLASSES

    /**
     * A point on the canvas.
     */
    public static class CanvasPoint {
        CanvasPoint(float theX, float theY) {
        CanvasPoint(double theX, double theY) {
            x = theX; y=theY;
        }

@@ -85,13 +115,13 @@ public abstract class Canvas implements Iterable<CanvasPoint> {
                '}';
        }

        final public float x;
        final public float y;
        final public double x;
        final public double y;
    }

    // FIELDS
    protected float Xsize;
    protected float Ysize;
    protected double Xsize;
    protected double Ysize;
    protected ArrayList<CanvasPoint> points;

}
+2 −2
Original line number Diff line number Diff line
@@ -19,8 +19,8 @@ public class GraphicalMain extends JFrame {
  final int prefY = 10;
  final int xScale = 30; // scale all X coordinates on the Canvas by this amount
  final int yScale = 30; // scale all X coordinates on the Canvas by this amount
  final float pointRad = 1f/10f; // radius of a point
  final float coverRad = 1f;    // radius of a covering circle
  final double pointRad = 1d/10d; // radius of a point
  final double coverRad = 1d;    // radius of a covering circle
  final Color pointCol = new Color(255,0,0,255); // color for a point
  final Color coverCol = new Color(0,0,255,50); // color for a covering circle

+16 −2
Original line number Diff line number Diff line
@@ -5,15 +5,29 @@ import java.util.ArrayList;

public class Main {

    /**
     * Run a simple test of the MyCanvas class.
     * @param baseX The x dimension of the underlying canvas.
     * @param baseY The y dimension of the underlying canvas.
     * @return A CoverResult, which contains the canvas produced and its proposed covering.
     */
    static CoverResult testCovering(int baseX, int baseY) {
        Canvas testCanvas = new MyCanvas(baseX,baseY);
        for (int ii=0; ii<Math.min(baseX,baseY); ii++)
            testCanvas.addPoint(new CanvasPoint(ii,ii));
        return new CoverResult(testCanvas, testCanvas.generateCovering());

        // show results
        ArrayList<CanvasPoint> testCovering = testCanvas.generateCovering();
        System.out.println("Produced covering:");
        System.out.println(testCovering);
        System.out.println(" ... covering is "+(testCanvas.checkCovering(testCovering)?" valid!":" invalid"));

        // return results
        return new CoverResult(testCanvas, testCovering);
    }

    public static void main(String[] args) {
        CoverResult result = testCovering(100,100);
        System.out.println(result.centers);
    }

    static public class CoverResult {