Advertisement
Guest User

Untitled

a guest
Mar 14th, 2012
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.18 KB | None | 0 0
  1. /*
  2.  * Copyright (c) 2007 Alexander Hristov.
  3.  * http://www.ahristov.com
  4.  *
  5.  * Feel free to use this code as you wish, as long as you keep this copyright
  6.  * notice. The only limitation on use is that this code cannot be republished
  7.  * on other web sites.
  8.  *
  9.  * As usual, this code comes with no warranties of any kind.
  10.  *
  11.  *
  12.  */
  13.  
  14.   public ArrayList<Point> quickHull(ArrayList<Point> points) {
  15.     ArrayList<Point> convexHull = new ArrayList<Point>();
  16.     if (points.size() < 3) return (ArrayList)points.clone();
  17.     // find extremals
  18.     int minPoint = -1, maxPoint = -1;
  19.     int minX = Integer.MAX_VALUE;
  20.     int maxX = Integer.MIN_VALUE;
  21.     for (int i = 0; i < points.size(); i++) {
  22.       if (points.get(i).x < minX) {
  23.         minX = points.get(i).x;
  24.         minPoint = i;
  25.       }
  26.       if (points.get(i).x > maxX) {
  27.         maxX = points.get(i).x;
  28.         maxPoint = i;      
  29.       }
  30.     }
  31.     Point A = points.get(minPoint);
  32.     Point B = points.get(maxPoint);
  33.     convexHull.add(A);
  34.     convexHull.add(B);
  35.     points.remove(A);
  36.     points.remove(B);
  37.    
  38.     ArrayList<Point> leftSet = new ArrayList<Point>();
  39.     ArrayList<Point> rightSet = new ArrayList<Point>();
  40.    
  41.     for (int i = 0; i < points.size(); i++) {
  42.       Point p = points.get(i);
  43.       if (pointLocation(A,B,p) == -1)
  44.         leftSet.add(p);
  45.       else
  46.         rightSet.add(p);
  47.     }
  48.     hullSet(A,B,rightSet,convexHull);
  49.     hullSet(B,A,leftSet,convexHull);
  50.    
  51.     return convexHull;
  52.   }
  53.  
  54.   /*
  55.    * Computes the square of the distance of point C to the segment defined by points AB
  56.    */
  57.   public int distance(Point A, Point B, Point C) {
  58.     int ABx = B.x-A.x;
  59.     int ABy = B.y-A.y;
  60.     int num = ABx*(A.y-C.y)-ABy*(A.x-C.x);
  61.     if (num < 0) num = -num;
  62.     return num;
  63.   }
  64.  
  65.   public void hullSet(Point A, Point B, ArrayList<Point> set, ArrayList<Point> hull) {
  66.     int insertPosition = hull.indexOf(B);
  67.     if (set.size() == 0) return;
  68.     if (set.size() == 1) {
  69.       Point p = set.get(0);
  70.       set.remove(p);
  71.       hull.add(insertPosition,p);
  72.       return;
  73.     }
  74.     int dist = Integer.MIN_VALUE;
  75.     int furthestPoint = -1;
  76.     for (int i = 0; i < set.size(); i++) {
  77.       Point p = set.get(i);
  78.       int distance  = distance(A,B,p);
  79.       if (distance > dist) {
  80.         dist = distance;
  81.         furthestPoint = i;
  82.       }
  83.     }
  84.     Point P = set.get(furthestPoint);
  85.     set.remove(furthestPoint);
  86.     hull.add(insertPosition,P);
  87.    
  88.     // Determine who's to the left of AP
  89.     ArrayList<Point> leftSetAP = new ArrayList<Point>();
  90.     for (int i = 0; i < set.size(); i++) {
  91.       Point M = set.get(i);
  92.       if (pointLocation(A,P,M)==1) {
  93.         leftSetAP.add(M);
  94.       }
  95.     }
  96.    
  97.     // Determine who's to the left of PB
  98.     ArrayList<Point> leftSetPB = new ArrayList<Point>();
  99.     for (int i = 0; i < set.size(); i++) {
  100.       Point M = set.get(i);
  101.       if (pointLocation(P,B,M)==1) {
  102.         leftSetPB.add(M);
  103.       }
  104.     }
  105.     hullSet(A,P,leftSetAP,hull);
  106.     hullSet(P,B,leftSetPB,hull);
  107.    
  108.   }
  109.  
  110.   public int pointLocation(Point A, Point B, Point P) {
  111.     int cp1 = (B.x-A.x)*(P.y-A.y) - (B.y-A.y)*(P.x-A.x);
  112.     return (cp1>0)?1:-1;
  113.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement