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>
private class PixelSection
extends RecursiveAction
{
/**
* 
*/
private static final long serialVersionUID
= 281227347488987878L
;
ArrayList
<Point
> points
;
ArrayList
<Point
> hullPoints
;
int volume
;
PixelSection
(ArrayList
<Point
> points
) {
this.
points = points
;
volume
= points.
size();
}
@Override

protected void compute
() {
ConvexHull convexHull
= new ConvexHull
(points
);
hullPoints
= convexHull.
points;
double longestDistance
= 0;
for (int i
= 0; i
< hullPoints.
size(); i
++) {
try {
prev
= hullPoints.
get(i
- 1);
prev
= hullPoints.
get(hullPoints.
size() - 1);
}
Point curr
= hullPoints.
get(i
);
double adjacent
= Math.
abs(curr.
x - prev.
x);
double opposite
= Math.
abs(curr.
y - prev.
y);
double distance
= Math.
sqrt(Math.
pow(adjacent,
2)
+ Math.
pow(opposite,
2));
if (distance
> longestDistance
) {
longestDistanceA
= new Point(prev.
x, prev.
y);
longestDistanceB
= new Point(curr.
x, curr.
y);
longestDistance
= distance
;
}
}
}
private class ConvexHull
{
ArrayList
<Point
> points
;
public ConvexHull
(ArrayList
<Point
> points
) {
process
(points
);
}
return (a.
x - o.
x) * (b.
y - o.
y) - (a.
y - o.
y)
* (b.
x - o.
x);
}
private void process
(ArrayList
<Point
> points
) {
if (points.
size() <= 1) {
return;
}
ArrayList
<Point
> lower
= new ArrayList
<Point
>();
for (int i
= 0; i
< points.
size(); i
++) {
try {
while (lower.
size() >= 2
&& cross
(lower.
get(lower.
size() - 2),

lower.
get(lower.
size() - 1),

points.
get(i
)) <= 0) {
try {
lower.
remove(lower.
size() - 1);
}
}
}
lower.
add(points.
get(i
));
}
ArrayList
<Point
> upper
= new ArrayList
<Point
>();
for (int i
= points.
size() - 1; i
> -1; i
--) {
try {
while (upper.
size() >= 2
&& cross
(upper.
get(upper.
size() - 2),

upper.
get(upper.
size() - 1),

points.
get(i
)) <= 0) {
try {
upper.
remove(upper.
size() - 1);
}
}
}
upper.
add(points.
get(i
));
}
try {
lower.
remove(lower.
size() - 1);
}
try {
upper.
remove(lower.
size() - 1);
}
lower.
addAll(upper
);
this.
points = lower
;
}
}
public class PointComparator
implements Comparator
<Point
> {
public int compare
(final Point a,
final Point b
) {
if (a.
x < b.
x) {
return -1;
} else if (a.
x > b.
x) {
return 1;
} else {
if (a.
y < b.
y) {
return -1;
} else if (a.
y > b.
y) {
return 1;
} else {
return 0;
}
}
}
}
}