【发布时间】:2010-10-19 19:50:12
【问题描述】:
我正在尝试实现 Jarvis 的算法来查找一组点的凸包,但由于某种原因它不起作用。这是我的实现:
procedure TPointList.ConvexHull(aHull : TPointList); //Return the convex hull of a set of 2D points
var
vPointOnHull : TPoint2D;
vEndpoint : TPoint2D;
I : integer;
begin
aHull.Clear;
if Count < 3 then exit;
vPointOnHull := Self.LeftMostPoint;
repeat
aHull.Add(vPointOnHull);
vEndpoint := Self.Point[0];
for I := 1 to Self.Count-1 do
if Orientation(vPointOnHull,vEndpoint,Self.Point[I]) = LeftHandSide then
vEndpoint := Self.Point[I];
vPointOnHull := vEndpoint;
until vEndpoint = aHull.Point[0];
end;
- TPointList 是一个简单的点列表。
- Orientation 是 Arash Partow 的库“FastGEO”中的一个函数
- 该实现或多或少直接来自Wikipedia article on the algorithm
发生的情况是该方法开始一遍又一遍地向 aHull 添加相同的点。在一个测试用例中,我发送点 (200;200) (300;100) (200;50) 和 (100;100),算法首先将 (100;100) 添加到正确的 aHull 中,但随后它开始一遍又一遍地添加 (200;200)。
显然,我在实施过程中做错了什么,但对于我的生活,我看不出是什么。
更新:
Jonathan Dursi 让我走上了正轨。这一行
if Orientation(vPointOnHull,vEndpoint,Self.Point[I]) = LeftHandSide then
应该换成这个
if (vPointOnHull = vEndpoint) or (Orientation(vPointOnHull,vEndpoint,Self.Point[I]) = LeftHandSide) then
像魅力一样工作:-)
【问题讨论】: