Advertisement
Guest User

pointset

a guest
Sep 29th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.00 KB | None | 0 0
  1. package s3;
  2.  
  3.  
  4. /****************************************************************************
  5.  *  Compilation:  javac PointSET.java
  6.  *  Execution:    
  7.  *  Dependencies:
  8.  *  Author:
  9.  *  Date:
  10.  *
  11.  *  Data structure for maintaining a set of 2-D points,
  12.  *    including rectangle and nearest-neighbor queries
  13.  *
  14.  *************************************************************************/
  15.  
  16. import java.util.Arrays;
  17.  
  18. import edu.princeton.cs.algs4.In;
  19. import edu.princeton.cs.algs4.Out;
  20. import edu.princeton.cs.algs4.Point2D;
  21. import edu.princeton.cs.algs4.SET;
  22.  
  23. public class PointSET {
  24.     private SET<Point2D> points;
  25.     // construct an empty set of points
  26.     public PointSET() {
  27.         points = new SET<Point2D>();
  28.     }
  29.  
  30.     // is the set empty?
  31.     public boolean isEmpty() {
  32.         return points.isEmpty();
  33.     }
  34.  
  35.     // number of points in the set
  36.     public int size() {
  37.         return points.size();
  38.     }
  39.  
  40.     // add the point p to the set (if it is not already in the set)
  41.     public void insert(Point2D p) {
  42.         points.add(p);
  43.     }
  44.  
  45.     // does the set contain the point p?
  46.     public boolean contains(Point2D p) {
  47.         return points.contains(p);
  48.     }
  49.  
  50.     // draw all of the points to standard draw
  51.     public void draw() {
  52.     }
  53.  
  54.     // all points in the set that are inside the rectangle
  55.     public Iterable<Point2D> range(RectHV rect) {
  56.         SET<Point2D> inRange = new SET<Point2D>();
  57.        
  58.         for (Point2D p : points){
  59.             if (rect.contains(p)){
  60.                 inRange.add(p);
  61.             }
  62.         }
  63.        
  64.         return inRange;
  65.     }
  66.  
  67.     // a nearest neighbor in the set to p; null if set is empty
  68.     public Point2D nearest(Point2D p) {
  69.         Point2D result = null;
  70.         double distance = Double.MAX_VALUE;
  71.         for (Point2D point : points){
  72.             if (p.distanceTo(point) < distance){
  73.                 distance = p.distanceTo(point);
  74.                 result = point;
  75.             }
  76.         }
  77.         return result;
  78.     }
  79.  
  80.     public static void main(String[] args) {
  81.         In in = new In();
  82.         Out out = new Out();
  83.         int nrOfRecangles = in.readInt();
  84.         int nrOfPointsCont = in.readInt();
  85.         int nrOfPointsNear = in.readInt();
  86.         RectHV[] rectangles = new RectHV[nrOfRecangles];
  87.         Point2D[] pointsCont = new Point2D[nrOfPointsCont];
  88.         Point2D[] pointsNear = new Point2D[nrOfPointsNear];
  89.         for (int i = 0; i < nrOfRecangles; i++) {
  90.             rectangles[i] = new RectHV(in.readDouble(), in.readDouble(),
  91.                     in.readDouble(), in.readDouble());
  92.         }
  93.         for (int i = 0; i < nrOfPointsCont; i++) {
  94.             pointsCont[i] = new Point2D(in.readDouble(), in.readDouble());
  95.         }
  96.         for (int i = 0; i < nrOfPointsNear; i++) {
  97.             pointsNear[i] = new Point2D(in.readDouble(), in.readDouble());
  98.         }
  99.         PointSET set = new PointSET();
  100.         for (int i = 0; !in.isEmpty(); i++) {
  101.             double x = in.readDouble(), y = in.readDouble();
  102.             set.insert(new Point2D(x, y));
  103.         }
  104.         for (int i = 0; i < nrOfRecangles; i++) {
  105.             // Query on rectangle i, sort the result, and print
  106.             Iterable<Point2D> ptset = set.range(rectangles[i]);
  107.             int ptcount = 0;
  108.             for (Point2D p : ptset)
  109.                 ptcount++;
  110.             Point2D[] ptarr = new Point2D[ptcount];
  111.             int j = 0;
  112.             for (Point2D p : ptset) {
  113.                 ptarr[j] = p;
  114.                 j++;
  115.             }
  116.             Arrays.sort(ptarr);
  117.             out.println("Inside rectangle " + (i + 1) + ":");
  118.             for (j = 0; j < ptcount; j++)
  119.                 out.println(ptarr[j]);
  120.         }
  121.         out.println("Contain test:");
  122.         for (int i = 0; i < nrOfPointsCont; i++) {
  123.             out.println((i + 1) + ": " + set.contains(pointsCont[i]));
  124.         }
  125.  
  126.         out.println("Nearest test:");
  127.         for (int i = 0; i < nrOfPointsNear; i++) {
  128.             out.println((i + 1) + ": " + set.nearest(pointsNear[i]));
  129.         }
  130.  
  131.         out.println();
  132.     }
  133.  
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement