001package algs9; // section 9.8
002import stdlib.*;
003import algs12.Point2D;
004import algs13.Bag;
005/* ***********************************************************************
006 *  Compilation:  javac InteractiveConvexHull.java
007 *  Execution:    java InteractiveConvexHull
008 *  Dependencies: GrahamScan.java Point2D.java StdDraw.java Bag.java
009 *
010 *************************************************************************/
011
012public class XInteractiveConvexHull {
013
014        public static void main(String[] args) {
015                StdDraw.setCanvasSize(800, 800);
016                StdDraw.setXscale(0, 10000);
017                StdDraw.setYscale(0, 10000);
018                StdDraw.show(0);
019                Bag<Point2D> bag = new Bag<>();
020
021                while (true) {
022                        if (StdDraw.mousePressed()) {
023                                // mouse pressed so add point to list of points
024                                int x = (int) (Math.round(StdDraw.mouseX()));
025                                int y = (int) (Math.round(StdDraw.mouseY()));
026                                bag.add(new Point2D(x, y));
027
028                                // extract array of points
029                                int N = bag.size();
030                                Point2D[] points = new Point2D[N];
031                                int n = 0;
032                                for (Point2D p : bag) {
033                                        points[n++] = p;
034                                }
035
036                                // compute convex hull
037                                GrahamScan graham = new GrahamScan(points);
038
039
040                                StdDraw.clear();
041
042                                // draw the points in black
043                                StdDraw.setPenRadius(.01);
044                                StdDraw.setPenColor(StdDraw.BLACK);
045                                for (int i = 0; i < N; i++)
046                                        points[i].draw();
047
048                                // draw the hull points in red
049                                StdDraw.setPenColor(StdDraw.RED);
050                                for (Point2D p : graham.hull())
051                                        p.draw();
052
053                                // draw the hull line segments in blue
054                                StdDraw.setPenRadius();
055                                StdDraw.setPenColor(StdDraw.BLUE);
056                                Point2D prev = null;
057                                for (Point2D p : graham.hull()) {
058                                        if (prev != null) prev.drawTo(p);
059                                        prev = p;
060                                }
061                                // hack to connect first and last points
062                                for (Point2D p : graham.hull()) {
063                                        prev.drawTo(p);
064                                        break;
065                                }
066                        }
067                        StdDraw.show(20);
068                }
069
070        }
071
072}
073