Advertisement
Guest User

Untitled

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