同次座標系で計算

それでは、実際に計算してみます。
正しく処理されているのがわかると思います。

//#pragma comment(linker, "/SUBSYSTEM:WINDOWS /ENTRY:mainCRTStartup")
#include <GL/freeglut/freeglut.h>
#include <stdio.h>

#define WIDTH 100
#define HEIGHT 50

//マトリクス構造体
struct MATRIX {
    union {
        struct {
            float _11, _12, _13, _14;
            float _21, _22, _23, _24;
            float _31, _32, _33, _34;
            float _41, _42, _43, _44;
        };
        float mat_4x4[4][4];
        float mat_16[16];
    };
 MATRIX(){//単位行列に初期化
  for(int i=0;i<16;i++){
   this->mat_16[i]=0;
  }
  this->_11=this->_22=this->_33=this->_44=1;
 }
 void PRINT(char* text){
  printf("%s\n%f,%f,%f,%f\n%f,%f,%f,%f\n%f,%f,%f,%f\n%f,%f,%f,%f\n\n",
   text,
   this->_11,this->_21,this->_31,this->_41,
   this->_12,this->_22,this->_32,this->_42,
   this->_13,this->_23,this->_33,this->_43,
   this->_14,this->_24,this->_34,this->_44);
 }
};

//4つのベクトル
struct Vector4f{
 union {
        struct {
   float x;
   float y;
   float z;
   float w;
  };
  float Index[4];
 };
 Vector4f(){};
 Vector4f(float _x,float _y,float _z,float _w){
  x=_x;y=_y;z=_z;w=_w;
 };
 Vector4f GeometricTransform(MATRIX& mat);//幾何変換
 void PRINT(char* text){
  printf("%s\n%f,%f,%f,%f\n\n",text,this->x,this->y,this->z,this->w);
 }
}vec4d;
Vector4f Vector4f::GeometricTransform(MATRIX &mat){
 Vector4f ret;
 for(int y=0;y<4;y++){
  ret.Index[y]=mat.mat_16[y]*this->x+mat.mat_16[y+4]*this->y+mat.mat_16[y+8]*this->z+mat.mat_16[y+12]*this->w;
 }
 return ret;
}

void display(void)
{

}

void Init(){

 Vector4f test;
 test.x=3.0f;
 test.y=1.0f;
 test.z=5.0f;
 test.w=1.0f;
 test.PRINT("同次座標を設定");

 MATRIX mat;
 mat._41=4.0f;
 mat._42=8.0f;
 mat._43=2.0f;
 mat.PRINT("適当に作成した平行移動行列");

 Vector4f ret=test.GeometricTransform(mat);
 ret.PRINT("積を行った結果");

}
int main(int argc, char *argv[])
{
 glutInitWindowPosition(100, 100);
 glutInitWindowSize(WIDTH, HEIGHT);
 glutInit(&argc, argv);
 glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
 glutCreateWindow("同次座標で計算");
 glutDisplayFunc(display);
 Init();
 glutMainLoop();
 return 0;
}

最終更新:2014年04月17日 00:47
添付ファイル