Code Tidy - Pastebin

New     Fork     Embed     View raw     Report
convex hull - java

Embed

You can embed this paste into a blog or website with this code:

<iframe class="codetidy" type="text/html" width="100%" src="http://codetidy.com/paste/embed/1851" frameborder="0"></iframe>

Add comment

Captcha
  1. private class PixelSection extends RecursiveAction {?
  2.  ?
  3.     /**?
  4.      * ?
  5.      */?
  6.     private static final long serialVersionUID = 281227347488987878L;?
  7.     ArrayList<Point> points;?
  8.     ArrayList<Point> hullPoints;?
  9.     Point longestDistanceA;?
  10.     Point longestDistanceB;?
  11.     int volume;?
  12.  ?
  13.     PixelSection(ArrayList<Point> points) {?
  14.         this.points = points;?
  15.         volume = points.size();?
  16.     }?
  17.  ?
  18.     @Override?
  19.     protected void compute() {?
  20.         ConvexHull convexHull = new ConvexHull(points);?
  21.         hullPoints = convexHull.points;?
  22.         Point prev = null;?
  23.         double longestDistance = 0;?
  24.         for (int i = 0; i < hullPoints.size(); i++) {?
  25.             try {?
  26.                 prev = hullPoints.get(i - 1);?
  27.             } catch (Exception e) {?
  28.                 prev = hullPoints.get(hullPoints.size() - 1);?
  29.             }?
  30.             Point curr = hullPoints.get(i);?
  31.  ?
  32.             double adjacent = Math.abs(curr.x - prev.x);?
  33.             double opposite = Math.abs(curr.y - prev.y);?
  34.             double distance = Math.sqrt(Math.pow(adjacent, 2)?
  35.                     + Math.pow(opposite, 2));?
  36.             if (distance > longestDistance) {?
  37.                 longestDistanceA = new Point(prev.x, prev.y);?
  38.                 longestDistanceB = new Point(curr.x, curr.y);?
  39.                 longestDistance = distance;?
  40.             }?
  41.         }?
  42.     }?
  43.  ?
  44.     private class ConvexHull {?
  45.         ArrayList<Point> points;?
  46.  ?
  47.         public ConvexHull(ArrayList<Point> points) {?
  48.             process(points);?
  49.         }?
  50.  ?
  51.         private int cross(Point o, Point a, Point b) {?
  52.             return (a.x - o.x) * (b.y - o.y) - (a.y - o.y)?
  53.                     * (b.x - o.x);?
  54.         }?
  55.  ?
  56.         private void process(ArrayList<Point> points) {?
  57.             Collections.sort(points, new PointComparator());?
  58.             if (points.size() <= 1) {?
  59.                 return;?
  60.             }?
  61.  ?
  62.             ArrayList<Point> lower = new ArrayList<Point>();?
  63.             for (int i = 0; i < points.size(); i++) {?
  64.                 try {?
  65.                     while (lower.size() >= 2?
  66.                             && cross(lower.get(lower.size() - 2),?
  67.                                     lower.get(lower.size() - 1),?
  68.                                     points.get(i)) <= 0) {?
  69.                         try {?
  70.                             lower.remove(lower.size() - 1);?
  71.                         } catch (Exception e) {?
  72.                         }?
  73.  ?
  74.                     }?
  75.                 } catch (Exception e) {?
  76.                 }?
  77.                 lower.add(points.get(i));?
  78.             }?
  79.  ?
  80.             ArrayList<Point> upper = new ArrayList<Point>();?
  81.             for (int i = points.size() - 1; i > -1; i--) {?
  82.                 try {?
  83.                     while (upper.size() >= 2?
  84.                             && cross(upper.get(upper.size() - 2),?
  85.                                     upper.get(upper.size() - 1),?
  86.                                     points.get(i)) <= 0) {?
  87.                         try {?
  88.                             upper.remove(upper.size() - 1);?
  89.                         } catch (Exception e) {?
  90.                         }?
  91.  ?
  92.                     }?
  93.                 } catch (Exception e) {?
  94.                 }?
  95.                 upper.add(points.get(i));?
  96.             }?
  97.             try {?
  98.                 lower.remove(lower.size() - 1);?
  99.             } catch (Exception e) {?
  100.             }?
  101.             try {?
  102.                 upper.remove(lower.size() - 1);?
  103.             } catch (Exception e) {?
  104.             }?
  105.             lower.addAll(upper);?
  106.             this.points = lower;?
  107.         }?
  108.     }?
  109.  ?
  110.     public class PointComparator implements Comparator<Point> {?
  111.         public int compare(final Point a, final Point b) {?
  112.             if (a.x < b.x) {?
  113.                 return -1;?
  114.             } else if (a.x > b.x) {?
  115.                 return 1;?
  116.             } else {?
  117.                 if (a.y < b.y) {?
  118.                     return -1;?
  119.                 } else if (a.y > b.y) {?
  120.                     return 1;?
  121.                 } else {?
  122.                     return 0;?
  123.                 }?
  124.             }?
  125.         }?
  126.     }?
  127. }?
© 2011 Code Tidy  Terms and conditions