/*
三角形三个顶点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;
}
多边形内生成随机点
最新推荐文章于 2023-10-10 10:38:40 发布