我正在尝试做的事情:
<强>1。围绕一个点均匀缩放点数组。 2. 点必须是点数组的平均点。
下面的代码似乎可以工作,但我不知道这样做是否正确。
我知道均匀缩放只是将点乘以某个值,但这是在 0,0,0 点上缩放,如何围绕平均点进行缩放?
代码可以 segmentation 为以下步骤:
- 通过将所有位置相加并除以点数,得到点数组的平均点。
- 比率是缩放值
- 然后我进行 vector 减法以获得从点指向平均点的 vector 。
- 我将该 vector 归一化(我得到单位 vector )
- 然后我将该归一化 vector 添加到当前点乘以 (1 - ratio)*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 以外)缩放点,请按照下列步骤操作:
- 从点减去中心
MatMxN vector = points.col(i) - curveCenter;
- 将 vector 乘以比例因子
vector *= ratio
- 将中心添加到缩放 vector 以获得新点
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
的想法。
关于c++ - 平均点c++周围点数组的均匀缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46806406/