我有我的代码工作翻译,唯一的问题是我似乎无法居中它。 我试图做 “translate = translate * center”,并将它移动到它的右上角。我不确定我还能做什么。我知道我的矩阵功能正常工作,我在这里错过了什么?2D转换,C++,旋转/平移/比例/居中
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;
}
的,如果在上面的代码语句是我是想出来的,我知道它的非必要的。
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;
}
我的翻译,旋转等方法看起来像这样。在这种方式被格式化很好,他们称之为计算()在结束
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 < < “键按下向下” < <的std :: ENDL; tY = tY-20; vContext.translation(tX,tY); gc-> clear(); derived→draw(gc,& vContext); 休息;
我不完全确定我会做错什么。 我使用键盘翻译(向上,向下箭头键和什么不是。向左) 完美的作品,但由于我的中心部分已实施,它将移动到右上角,然后开始翻译。我不确定发生了什么问题。 任何帮助将不胜感激!
你的问题并没有提供很多关于你在做什么的信息,但它看起来像是在均匀坐标系中的平移和旋转的矩阵表示。你在哪里使用它们? OpenGL的?请记住,使用矩阵时,顺序很重要,所以你'翻译=中心* S * T'首先适用于T,然后S,然后居中,如果你以后计算'翻译* coordinateVector' – Anedar
@Andar对不起我的其他帖子,有人写我提供了太多的信息。 我在一个基于Linux的虚拟机上。 center是我的矩阵,它包含您在代码中看到的内容。T包含已翻译的坐标。 S包括缩放后的坐标。 我是否应该在这里放置代码部分? 但是,我先居中,然后缩放,然后翻译。我相信这是实施它的正确方法。 有什么我失踪? –
从你发布的代码中,'center','R'和'translated'是矩阵。但是S和T是什么?你期望'model_to_device()'做什么?你应该提供与你的问题相关的所有信息 - 只有那些相关的信息。 – Anedar