Fix Jarvis infinite loop
This commit is contained in:
@@ -65,6 +65,12 @@ public class Jarvis
|
|||||||
do
|
do
|
||||||
{
|
{
|
||||||
ch.add(current);
|
ch.add(current);
|
||||||
|
assert ch.size() <= points.size() : "hull has more points than graph";
|
||||||
|
if (ch.size() > points.size())
|
||||||
|
{
|
||||||
|
// Just to make sure we never somehow get stuck in this loop
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
// the next point - all points are to the right of the
|
// the next point - all points are to the right of the
|
||||||
// line between current and next
|
// line between current and next
|
||||||
@@ -78,7 +84,7 @@ public class Jarvis
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
int cp = crossProduct(current, p, next);
|
long cp = crossProduct(current, p, next);
|
||||||
if (cp > 0 || (cp == 0 && current.distanceTo(p) > current.distanceTo(next)))
|
if (cp > 0 || (cp == 0 && current.distanceTo(p) > current.distanceTo(next)))
|
||||||
{
|
{
|
||||||
next = p;
|
next = p;
|
||||||
@@ -91,7 +97,6 @@ public class Jarvis
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
assert ch.size() <= points.size() : "hull has more points than graph";
|
|
||||||
current = next;
|
current = next;
|
||||||
}
|
}
|
||||||
while (current != left);
|
while (current != left);
|
||||||
@@ -118,10 +123,10 @@ public class Jarvis
|
|||||||
return left;
|
return left;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static int crossProduct(Point p, Point q, Point r)
|
private static long crossProduct(Point p, Point q, Point r)
|
||||||
{
|
{
|
||||||
int val = (q.getY() - p.getY()) * (r.getX() - q.getX())
|
long val = (long)(q.getY() - p.getY()) * (r.getX() - q.getX())
|
||||||
- (q.getX() - p.getX()) * (r.getY() - q.getY());
|
- (long)(q.getX() - p.getX()) * (r.getY() - q.getY());
|
||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user