InteractiveConvexHull.java


Below is the syntax highlighted version of InteractiveConvexHull.java from §9.9 Convex Hull.


/******************************************************************************
 *  Compilation:  javac InteractiveConvexHull.java
 *  Execution:    java InteractiveConvexHull
 *  Dependencies: GrahamScan.java Point2D.java StdDraw.java Bag.java
 *
 *  Interactive convex hull visualization.
 *
 ******************************************************************************/

public class InteractiveConvexHull {

    public static void main(String[] args) {
        StdDraw.setCanvasSize(800, 800);
        StdDraw.setXscale(0, 10000);
        StdDraw.setYscale(0, 10000);
        StdDraw.enableDoubleBuffering();

        Bag<Point2D> bag = new Bag<Point2D>();

        while (true) {
            if (StdDraw.mousePressed()) {
                // mouse pressed so add point to list of points
                int x = (int) (Math.round(StdDraw.mouseX()));
                int y = (int) (Math.round(StdDraw.mouseY()));
                bag.add(new Point2D(x, y));

                // extract array of points
                int N = bag.size();
                Point2D[] points = new Point2D[N];
                int n = 0;
                for (Point2D p : bag) {
                    points[n++] = p;
                }

                // compute convex hull
                GrahamScan graham = new GrahamScan(points);


                StdDraw.clear();

                // draw the points in black
                StdDraw.setPenRadius(.01);
                StdDraw.setPenColor(StdDraw.BLACK);
                for (int i = 0; i < N; i++)
                    points[i].draw();

                // draw the hull points in red
                StdDraw.setPenColor(StdDraw.RED);
                for (Point2D p : graham.hull())
                    p.draw();

                // draw the hull line segments in blue
                StdDraw.setPenRadius();
                StdDraw.setPenColor(StdDraw.BLUE);
                Point2D prev = null;
                for (Point2D p : graham.hull()) {
                    if (prev != null) prev.drawTo(p);
                    prev = p;
                }
                // hack to connect first and last points
                for (Point2D p : graham.hull()) {
                    prev.drawTo(p);
                    break;
                }
            }
            StdDraw.show();
            StdDraw.pause(20);
        }

    }

}


Copyright © 2000–2017, Robert Sedgewick and Kevin Wayne.
Last updated: Fri Oct 20 12:50:46 EDT 2017.