在平均点c++周围的点阵列上进行均匀缩放
Uniform scaling on array of point around average point c++
我要做的事情:
1.均匀缩放一个点周围的点阵列。2.一个点必须是点阵列的平均点
下面的代码似乎有效,但我不知道这是否是正确的方法。
我知道均匀缩放只是将点乘以某个值,但这是在0,0,0点上缩放,如何在平均点周围进行缩放?
代码可以通过以下步骤细分:
- 通过将所有位置相加并除以多个点,获得点阵列的平均点
- 比率是缩放值
- 然后我做向量减法,得到一个从点到平均点的向量
- 我归一化那个向量(我得到单位向量)
- 然后,我将标准化向量与当前点相乘(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)缩放点,请执行以下步骤:
- 点
MatMxN vector = points.col(i) - curveCenter;
的子中心 - 将矢量乘以比例因子
vector *= ratio
- 将中心添加到缩放矢量以获得新点
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
的想法。
相关文章:
- UE4-如何在给定4个屏幕坐标的情况下缩放纹理或材质
- 计算缩放多边形的比例,得到给定的多边形面积
- QwtPlot具有相等的轴和自动缩放
- 如何在directx/c++中进行平移/缩放操作
- 在Qt中实现无限可缩放的画布
- 是否可以为 QPixmap 派生类嵌入缩放方法?
- 形状对象的旋转和缩放不正确C++
- 如何在 OpenSceneGraph 中缩放/旋转/移动资产
- 如何在OpenGL(GLFW,很高兴)中进行2D缩放?
- 改变或缩放两个正态分布以具有特定的相关系数
- SDL2 调整窗口大小后如何缩放鼠标坐标?
- 如何在OpenGL中正确旋转和缩放对象?
- 仅当类型为 std::complex 时,才进行缩放
- 将 VS Code 用于跨平台可缩放C++项目
- 根据帧速率缩放/缩小数字
- 在 c++ 中缩放浮点值
- 如何防止 DirectX C++程序的拉伸/缩放
- 使用提升几何缓冲区缩放多边形时的冗余折点
- 为什么我不能使用 Sphere 来缩放字形?
- 我在QChartView中找不到缩放图形的鼠标滚轮滚动事件