凸包算法

private List calcConvexHull(List list)
{
List resPoint = new List();
//查找最小坐标点
int minIndex = 0;
for (int i = 1; i < list.Count; i++)
{
if (list[i].Y < list[minIndex].Y)
{
minIndex = i;
}
}
Point minPoint = list[minIndex];
resPoint.Add(list[minIndex]);
list.RemoveAt(minIndex);
//坐标点排序
list.Sort(
delegate(Point p1, Point p2)
{
vector baseVec;
baseVec.x = 1;
baseVec.y = 0;

        vector p1Vec;
        p1Vec.x = p1.X - minPoint.X;
        p1Vec.y = p1.Y - minPoint.Y;

        vector p2Vec;
        p2Vec.x = p2.X - minPoint.X;
        p2Vec.y = p2.Y - minPoint.Y;

        double up1 = p1Vec.x * baseVec.x;
        double down1 = Math.Sqrt(p1Vec.x * p1Vec.x + p1Vec.y * p1Vec.y);

        double up2 = p2Vec.x * baseVec.x;
        double down2 = Math.Sqrt(p2Vec.x * p2Vec.x + p2Vec.y * p2Vec.y);


        double cosP1 = up1 / down1;
        double cosP2 = up2 / down2;

        if (cosP1 > cosP2)
        {
            return -1;
        }
        else
        {
            return 1;
        }
    }
    );
resPoint.Add(list[0]);
resPoint.Add(list[1]);
for (int i = 2; i < list.Count; i++)
{
    Point basePt = resPoint[resPoint.Count - 2];
    vector v1;
    v1.x = list[i - 1].X - basePt.X;
    v1.y = list[i - 1].Y - basePt.Y;

    vector v2;
    v2.x = list[i].X - basePt.X;
    v2.y = list[i].Y - basePt.Y;

    if (v1.x * v2.y - v1.y * v2.x < 0)
    {
        resPoint.RemoveAt(resPoint.Count - 1);
        while (true)
        {
            Point basePt2 = resPoint[resPoint.Count - 2];
            vector v12;
            v12.x = resPoint[resPoint.Count - 1].X - basePt2.X;
            v12.y = resPoint[resPoint.Count - 1].Y - basePt2.Y;
            vector v22;
            v22.x = list[i].X - basePt2.X;
            v22.y = list[i].Y - basePt2.Y;
            if (v12.x * v22.y - v12.y * v22.x < 0)
            {
                resPoint.RemoveAt(resPoint.Count - 1);
            }
            else
            {
                break;
            }
        }
        resPoint.Add(list[i]);
    }
    else
    {
        resPoint.Add(list[i]);
    }
}
return resPoint;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值