Nature with Object Pascal
Pascal translation of "The Nature of Code"
3D Matrix from Native Object Pascal
2024.06.01 02:20
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;
===========================================================================
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;
Comment 0
No. | Subject | Author | Date | Views |
---|---|---|---|---|
Notice | The Nature of Code + Color Code [1] | me | 2024.02.17 | 127 |
» |
3D Matrix from Native Object Pascal
![]() | me | 2024.06.01 | 57 |
4 |
3D Matrix from BGRABitmap Library
![]() | me | 2024.06.01 | 50 |
3 |
TPoint3D and IBGRAVertex3D
![]() | me | 2024.03.11 | 83 |
2 |
BGRA Bitmap Library's TPointF as 2D Vector
![]() | me | 2024.02.12 | 105 |
1 |
IBGRAObject3D properties to be familiar with
![]() | me | 2024.01.26 | 127 |