在平均点c++周围的点阵列上进行均匀缩放

Uniform scaling on array of point around average point c++

本文关键字:缩放 c++ 周围 阵列      更新时间:2023-10-16

我要做的事情:

1.均匀缩放一个点周围的点阵列。2.一个点必须是点阵列的平均点

下面的代码似乎有效,但我不知道这是否是正确的方法。

我知道均匀缩放只是将点乘以某个值,但这是在0,0,0点上缩放,如何在平均点周围进行缩放?

代码可以通过以下步骤细分:

  1. 通过将所有位置相加并除以多个点,获得点阵列的平均点
  2. 比率是缩放值
  3. 然后我做向量减法,得到一个从点到平均点的向量
  4. 我归一化那个向量(我得到单位向量)
  5. 然后,我将标准化向量与当前点相乘(1-比率)*0.5

最后一位第5点完全来自于检查值的总长度。

我之前提到的所有例子都是在数学中使用矩阵,我真的不懂矩阵运算。

这是正确的统一缩放方法吗?如果不是,你能指出我做错了什么吗?

//Get center of a curve 
//That is average of all points

MatMxN curveCenter = MatMxN::Zero(2, 1); //This is just 1 vector/point with x and y coordinates
for (int i = 0; i < n; i++)
curveCenter += points.col(i);
curveCenter /= n;
//Scaling value
float ratio = 1.3;
//Get vector pointing to center  and move by ratio
for (int i = 0; i < n; i++) {
MatMxN vector = curveCenter - points.col(i);
MatMxN unit = vector.normalized();
points.col(i) += unit*(1 - ratio)*0.5; //points.col(i) this is point in array
}

要使用特定的中心点(而不是0)缩放点,请执行以下步骤:

  1. MatMxN vector = points.col(i) - curveCenter;的子中心
  2. 将矢量乘以比例因子vector *= ratio
  3. 将中心添加到缩放矢量以获得新点points.col(i) = vector + curveCenter

这种方法可以解决与您的公式非常相似的问题。让我们调用中心C、要缩放的点P0、缩放点P1和缩放因子s。以上3个步骤可以写成:

v0 = P0 - C
v1 = s * v0
P1 = v1 + C

=>

P1 = s * P0 + C * (1 - s)

现在我们为一些x:定义P1 = P0 + x

P0 + x = s * P0 + C * (1 - s)

=>

x = s * P0 + C * (1 - s) - P0
= C * (1 - s) - P0 * (1 - s)
= (C - P0) * (1 - s)

因此,更新可以写如下,而不是使用提到的3个步骤:

MatMxN vector = curveCenter - points.col(i);
points.col(i) += vector * (1 - ratio);

然而,我更喜欢反向写子动作,因为它更接近原始步骤,更容易凭直觉理解:

MatMxN vector = points.col(i) - curveCenter;
points.col(i) += vector * (ratio - 1);

我不知道你在哪里找到了normalize和*0.5的想法。