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

时间:2017-10-18 09:01:14

标签: c++ matrix

我想做什么:

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

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

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

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

  1. 通过总结所有位置并除以一些点来获得点阵列的平均点。
  2. 比率是缩放值
  3. 然后我做矢量减法,得到一个从点到平均点的矢量。
  4. 我规范了那个向量(我得到了单位向量)
  5. 然后我将标准化矢量加到当前点乘以(1 - 比率)* 0.5
  6. 最后一点第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
        }
    

1 个答案:

答案 0 :(得分:2)

要使用特定中心点( 0 除外)缩放点,请按以下步骤操作:

  1. 从点MatMxN vector = points.col(i) - curveCenter;
  2. 中减去中心
  3. 按比例因子vector *= ratio
  4. 乘以向量
  5. 将中心添加到缩放的矢量以获取新点points.col(i) = vector + curveCenter
  6. 可以将此方法解析为与您的公式类似的远程方法。让我们调用中心C,缩放点P0,缩放点P1和缩放系数s。以上3个步骤可写为:

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

    =&GT;

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

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

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

    =&GT;

    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);
    

    我不知道您在哪里找到了规范化和*0.5想法。