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