2D变换,c ++,旋转/平移/缩放/居中

时间:2016-02-09 01:29:09

标签: c++ graphics 2d computer-science transformation

我的代码正在为翻译工作,唯一的问题是我似乎无法将其置于中心位置。 我试过这样做 "翻译=翻译*中心"这似乎是将它移动到右上角。我不确定我还能做些什么。我知道我的矩阵函数工作正常,我在这里缺少什么?

oid viewContext::calculate() {
std::cout << "calculate" << std::endl;

/*
 * reinitialize these values
 */
translated.clear();
translated[0][0] = 1;
translated[1][1] = 1;
translated[2][2] = 1;

//translated = translated * center;
//if translate is called
if (whichMethod == trans) {
    translated = translated * center;
    translated = translated * S;
    translated = translated * T;
}

上面代码中的if语句是我尝试过的,我知道它是不必要的。

matrix viewContext::model_to_device(const matrix& param) {

    std::cout << "model to device" << std::endl;

    //matrix hold_values = param;

    matrix hold_values = translated * param;

    return hold_values;

}

我的翻译,旋转等方法看起来像这样。以这种方式格式化,他们最后调用calculate()

void viewContext::rotator(double degree) {
std::cout << "Rotate" << std::endl;

double theta = ((degree * M_PI) / 180);
R[0][0] = cos(theta);
R[0][1] = -1 * sin(theta);
R[1][0] = sin(theta);
R[1][1] = cos(theta);
calculate();

}

    viewContext::viewContext() :
        translated(3, 3), inverted(3, 3), center(3, 3), T(3, 3), S(3, 3), R(3,
                3) {
    //clear matrix just incase it isn't empty
    //Should be empty but this is precautionary
    translated.clear();

    inverted.clear();

    /*
     * Constant values
     * echelon form
     */
    T[0][0] = 1;
    T[1][1] = 1;
    T[2][2] = 1;

    /*
     * Constant values
     * 300 = x, 400 = y to put at origin.
     */
    center[0][0] = 1;
    center[0][2] = width;
    center[1][1] = -1;
    center[1][2] = height;
    center[2][2] = 1;

    /*
     * Constant values
     * Echelon form
     */
    S[0][0] = 1;
    S[1][1] = 1;
    S[2][2] = 1;

    //TODO
//  /*
//   * Constant values
//   * Echelon form
//   */
    translated[0][0] = 1;
    translated[1][1] = 1;
    translated[2][2] = 1;

    /*
     * Constant values
     * Echelon form
     */
    R[0][0] = 1;
    R[0][1] = 0;
    R[1][0] = 0;
    R[1][1] = 1;
    R[2][2] = 1;

    calculate();
}

这是我的构造函数的一部分,其中实现了中心。

    private:

    /* translation matrix */
    matrix translated;

    /* translation inverse matrix */
    matrix inverted;

    /* to center the image */
    matrix center;

    matrix T;
    matrix S;
    matrix R;

    const int height = 300;
    const int width = 400;
    void calculate();

    int whichMethod;
};

案例(KEY_U):     std :: cout&lt;&lt; &#34;按下键#34; &LT;&LT;的std :: ENDL;     tY = tY - 20;     vContext.translation(tX,tY);     GC-&GT;清除();     derived-&gt; draw(gc,&amp; vContext);     打破;

我不确定我可能做错了什么。 我使用键盘翻译(向上,向下箭头键,什么不是。左边) 这很有效,但由于我实现了中心部分,它将移动到右上角然后开始翻译。我不确定出了什么问题。 任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

我假设您的矩阵乘法正确实现,然后您的翻译矩阵构建为

translated = 1 * center * S * T

其中1是您的初始身份矩阵。

我在您的评论中进一步假设param中的model_to_device()是一个坐标向量,导致

hold_values = translated * param
hold_values = center * S * T * param

正如您所看到的,应用于param的第一个运算符实际上是T,然后是S,然后是中心。首先,你转换坐标,然后缩放它们(缩放中心位于(0,0)),然后使用你的中心矩阵,将它们平移(宽度,高度)并反转y。所以基本上你首先翻译,然后缩放,然后居中。

相关问题