Skip to menu

Robotics with Object Pascal

Nature with Object Pascal

Pascal translation of "The Nature of Code"

3D Matrix from Native Object Pascal

2024.06.01 02:20

me Views:57

 

uses matrix;

 

var

   P : Tmatrix3_double;
   p3d : Tvector3_double;
   p3d_2d : Tvector3_double;  // only use x, y position, z is ignored

 

procedure TfrmMain.FormCreate(Sender: TObject);
begin
   // The Projection Matrix
   P.init_identity;

   P.data[2,2] := 0;
   p3d.init(-1,1,1);  // let's say 3D point is as this
   p3d_2d.init(0,0,0); 

   p3d_2d := P * p3d;  // result will be (-1, 1, 0)

   mR.lines.clear;
   mR.lines.add(format('Projected 2d(x,y): %d, %d', [
      round(p3d_2d.data[0]),
      round(p3d_2d.data[1])]));  

end;

 

first_01.png

 

===========================================================================

 

var

     tm4, sm4, rx4, ry4, rz4 : Tmatrix4_double;

 

procedure TfrmMain.InitializeMatrix;  // using basic matrix unit
begin
   // Translation Matrix
   tm4.init(
      1, 0, 0, 0,   //  1,  0,  0,  0
      0, 1, 0, 0,   //  0,  1,  0,  0
      0, 0, 1, 0,   //  0,  0,  1,  0
      1, 1, 1, 1    // tx, ty, tz,  1
   );

   // Scaling Matrix
   sm4.init(
      1, 0, 0, 0,   // Sx,  0,  0,  0
      0, 1, 0, 0,   //  0, Sy,  0,  0
      0, 0, 1, 0,   //  0,  0, Sz,  0
      0, 0, 0, 1    //  0,  0,  0,  1
   );

   // Rotation Matrix based on X axis
   rx4.init_identity; // 1,      0,      0, 0
                      // 0, cos(n), sin(n), 0
                      // 0,-sin(n), cos(n), 0
                      // 0,      0,      0, 1

   // Rotation Matrix based on Y axis
   ry4.init_identity; // cos(n), 0,-sin(n), 0
                      // 0,      1,      0, 0
                      // sin(n), 0, cos(n), 0
                      // 0,      0,      0, 1

   // Rotation Matrix based on Z axis
   rz4.init_identity; //  cos(n), sin(n), 0, 0
                      // -sin(n), cos(n), 0, 0
                      //       0,      0, 1, 0
                      //       0,      0, 0, 1

end;