n. Doe February 2016

2D transformation, c++, rotate/translate/Scale/centering

I have my code working for translate, the only problem is I cannot seem to center it. I have tried to do "translate = translate * center" and that moves it to the upper right corner it seems like. I am not exactly sure what else I can do. I know my matrix functions are working properly, what am I missing here?

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

the if statement in the code above is something I was trying out, I know its un-necessary.

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;

}

my methods that translate, rotate, etc look like this. well are formatted in this sort of way, they call calculate() at the end

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.
     */
    cen        

Answers


Anedar February 2016

I assume that your matrix-multiplication is implemented correctly, then your translated matrix is build as

translated = 1 * center * S * T

where 1 is your initial identity-matrix.

I further assume from your comments that param in model_to_device() is a coordinate vector, resulting in

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

As you might see, the first operator applied to param is actually T, then S, then center. So first you translate the coordinates, then scale them (with scaling center at (0,0) ) and then use your center-matrix which translates them by (width,height) and inverts y. So basically you first translate, then scale, then center.

Post Status

Asked in February 2016
Viewed 2,210 times
Voted 12
Answered 1 times

Search




Leave an answer