多边形内生成随机点

/*
三角形三个顶点A,B,C首先:求得两个向量ab = B - A, ac = C - A。
然后:使用rand()获得两个0~1之间的随机实数x, y如果x+y>1,
那么令x'=1-x, y'=1-y如果x+y<=1, 那么令x'=x, y'=y最后:随机点 = A + x' * ab + y‘ * ac
链接:https://www.zhihu.com/question/31706710/answer/53131190
*/


TPoint3dArray createRandomPts(const TPoint3dArray& arr, int maxCount)
{
    TPoint3dArray result;
    osg::Vec3 centerPT;

    for (auto& p : arr)
    {
        centerPT += p;
    }
    centerPT /= arr.size();

    maxCount /= arr.size();

    for (int i = 0; i < arr.size(); i++)
    {
        int count = 0;

        int index1 = i, index2 = i + 1;
        if (i == arr.size() - 1)
        {
            index1 = 0; index2 = arr.size() - 1;
        }

        srand(time(NULL));

        while (count < maxCount)
        {
            osg::Vec3 ab = arr[index1] - centerPT;
            osg::Vec3 ac = arr[index2] - centerPT;

            float x = rand() / (RAND_MAX + 0.0);
            float y = rand() / (RAND_MAX + 0.0);
            float x1, y1;

            if (x + y > 1)
            {
                x1 = 1 - x; y1 = 1 - y;
            }
            else
            {
                x1 = x; y1 = y;
            }

            osg::Vec3 pt;
            pt = centerPT + ab * x1 + ac * y1;
            result.push_back(pt);
            count++;
        }

    }

    return result;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值