Guest User

Untitled

a guest
Feb 25th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Collections;
  3. import java.util.Comparator;
  4. import java.util.List;
  5.  
  6. public class NearesPoint {
  7.  
  8. List<Double> latList;
  9.  
  10. List<Double> lonList;
  11.  
  12. List<Point> pointList;
  13.  
  14. public NearesPoint(){
  15. Collections.sort(pointList);
  16. }
  17.  
  18.  
  19. /**
  20. * Find nearest point from the given point p
  21. * @param p
  22. * @return
  23. */
  24. public Point findNearest(Point p){
  25.  
  26. //Find nearest point in List using lat
  27. int pivot = Collections.binarySearch(pointList,p);
  28.  
  29. Point p1 = nearest(0,pivot,p);
  30. Point p2 = nearest(pivot+1,pointList.size()-1,p);
  31.  
  32. if(p1==null && p2!=null){
  33. return p2;
  34. }
  35. if(p2==null && p1!=null){
  36. return p1;
  37. }
  38.  
  39. Point res = new Point(); //Find p1 or p2 which is nearest to p
  40. return res;
  41.  
  42.  
  43.  
  44. }
  45.  
  46. private Point nearest(int low, int high, Point p){
  47. if(low>high){
  48. return null;
  49. }
  50. if(low ==high){
  51. return pointList.get(low);
  52. }
  53.  
  54. int m = (low+high)/2;
  55.  
  56. Point p1 = nearest(low,m,p);
  57. Point p2 = nearest(m+1,high,p);
  58.  
  59. if(p1==null && p2!=null){
  60. return p2;
  61. }
  62. if(p2==null && p1!=null){
  63. return p1;
  64. }
  65.  
  66. Point res = new Point(); //Find p1 or p2 which is nearest to p
  67. return res;
  68. }
  69.  
  70.  
  71. static class Point implements Comparable<Point>{
  72. Double lat;
  73. Double lon;
  74.  
  75. @Override
  76. public int compareTo(Point o) {
  77. //Points are sorted based on latitude, if lats are equal than based on lon
  78. int d = this.lat.compareTo(o.lat);
  79. if(d==0){
  80. return this.lon.compareTo(o.lon);
  81. }
  82. return d;
  83. }
  84. }
  85. }
Add Comment
Please, Sign In to add comment