001package algs32.kdtree;
002import algs12.Point2D;
003import stdlib.*;
004/* ***********************************************************************
005 *  Compilation:  javac KdTreeVisualizer.java
006 *  Execution:    java KdTreeVisualizer
007 *  Dependencies: StdDraw.java Point2D.java KdTree.java
008 *
009 *  Add the points that the user clicks in the standard draw window
010 *  to a kd-tree and draw the resulting kd-tree.
011 *
012 *************************************************************************/
013
014public class KdTreeVisualizer {
015
016        private static double round (double d) {
017                return Math.round(d * 100.0) / 100.0;
018        }
019        public static void main(String[] args) {
020                //args = new String [] { "src/algs32/kdtree/kdtree-input10K.txt" };
021                //args = new String [] { "src/algs32/kdtree/kdtree-circle4.txt" };
022                //args = new String [] { "src/algs32/kdtree/kdtree-circle10.txt" };
023                //args = new String [] { "src/algs32/kdtree/kdtree-circle100.txt" };
024                //args = new String [] { "src/algs32/kdtree/kdtree-horizontal8.txt" };
025                //args = new String [] { "src/algs32/kdtree/kdtree-vertical7.txt" };
026
027
028                KdTree kdtree = new KdTree();
029                StdDraw.show(0);
030
031                if (args.length > 0) {
032                        String filename = args[0];
033                        In in = new In(filename);
034                        while (!in.isEmpty()) {
035                                double x = in.readDouble();
036                                double y = in.readDouble();
037                                Point2D p = new Point2D(round (x), round (y));
038                                kdtree.insert(p);
039                                StdDraw.setPenRadius ();
040                                StdDraw.setPenColor ();
041                                StdDraw.rectangle (.5, .5, .5, .5);
042                                kdtree.draw();
043                                StdDraw.show(100);
044                        }
045                } else {
046                        while (true) {
047                                if (StdDraw.mousePressed()) {
048                                        double x = StdDraw.mouseX();
049                                        double y = StdDraw.mouseY();
050                                        System.out.format("%8.6f %8.6f\n", x, y);
051                                        Point2D p = new Point2D(round (x), round (y));
052                                        kdtree.insert(p);
053                                        StdDraw.clear();
054                                        StdDraw.setPenRadius ();
055                                        StdDraw.setPenColor ();
056                                        StdDraw.rectangle (.5, .5, .5, .5);
057                                        kdtree.draw();
058                                        kdtree.toGraphviz ();
059                                }
060                                StdDraw.setPenRadius ();
061                                StdDraw.setPenColor ();
062                                StdDraw.rectangle (.5, .5, .5, .5);
063                                kdtree.draw();
064                                StdDraw.show(50);
065                        }
066                }
067        }
068}