Guest User

Untitled

a guest
Dec 11th, 2019
105
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. unit Unit1;
  2.  
  3. interface
  4.  
  5. uses
  6. Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  7. Dialogs, Unit2, ExtCtrls, ComCtrls;
  8.  
  9. type
  10. matrix = array[0..3, 0..3] of real;
  11. figure = record
  12. points: array[0..32] of TPoint3D;
  13. p_count: 0..32;
  14. edges: array[byte, 0..1] of ^TPoint3D;
  15. e_count: byte;
  16. end;
  17. TForm1 = class(TForm)
  18. Image1: TImage;
  19. Timer1: TTimer;
  20. procedure FormCreate(Sender: TObject);
  21. function Transform(point: TPoint3D; trs: matrix): TPoint3D;
  22. procedure DrawFigure(fig: figure; col: TColor);
  23. procedure Timer1Timer(Sender: TObject);
  24. procedure InitHexahedron();
  25. procedure InitMatrix();
  26. private
  27. { Private declarations }
  28. public
  29. { Public declarations }
  30. end;
  31.  
  32. var
  33. Form1: TForm1;
  34. hexahedron, hexahedron2: figure;
  35. ticks: longint;
  36. isometry_mtx, move_mtx,move_mtx2: matrix;
  37.  
  38. implementation
  39.  
  40. {$R *.dfm}
  41.  
  42. procedure TForm1.FormCreate(Sender: TObject);
  43. var size: integer;
  44. begin
  45. ticks := 0;
  46. InitHexahedron;
  47. InitMatrix;
  48. Image1.Canvas.Pen.Width := 4;
  49. end;
  50.  
  51. procedure TForm1.InitHexahedron;
  52. var a, size, i: integer;
  53. turn_mtx_X, turn_mtx_Z: matrix;
  54. c, s, angle: real;
  55. temp_point: TPoint3D;
  56. begin
  57. a := 100;
  58. with hexahedron do begin
  59. points[0] := TPoint3D.Point3D(-a, -a, -a); // координаты вершины 0
  60. points[1] := TPoint3D.Point3D(-a, -a, a); // координаты вершины 1
  61. points[2] := TPoint3D.Point3D(a, -a, a); // координаты вершины 2
  62. points[3] := TPoint3D.Point3D(a, -a, -a); // координаты вершины 3
  63. points[4] := TPoint3D.Point3D(a, a, -a); // координаты вершины 4
  64. points[5] := TPoint3D.Point3D(a, a, a); // координаты вершины 5
  65. points[6] := TPoint3D.Point3D(-a, a, a); // координаты вершины 6
  66. points[7] := TPoint3D.Point3D(-a, a, -a); // координаты вершины 7
  67.  
  68. p_count := 7; // количество вершин
  69. { 5_________6
  70. /| /|
  71. /_|_______/7|
  72. 4| |_______|_|
  73. | /0 | /1
  74. |/________|/
  75. 3 2 }
  76. edges[0,0] := @points[0]; edges[0,1] := @points[1]; //соединияем 0 c 1 ребро
  77. edges[1,0] := @points[0]; edges[1,1] := @points[3]; //соединияем 0 c 3 ребро
  78. edges[2,0] := @points[0]; edges[2,1] := @points[7]; //соединияем 0 c 7 ребро
  79. edges[3,0] := @points[1]; edges[3,1] := @points[6]; //соединияем 1 c 6 ребро
  80. edges[4,0] := @points[1]; edges[4,1] := @points[2]; //соединияем 1 c 2 ребро
  81. edges[5,0] := @points[2]; edges[5,1] := @points[3]; //соединияем 2 c 3 ребро
  82. edges[6,0] := @points[2]; edges[6,1] := @points[5]; //соединияем 2 c 5 ребро
  83. edges[7,0] := @points[3]; edges[7,1] := @points[4]; //соединияем 3 c 4 ребро
  84. edges[8,0] := @points[4]; edges[8,1] := @points[5]; //соединияем 4 c 5 ребро
  85. edges[9,0] := @points[4]; edges[9,1] := @points[7]; //соединияем 4 c 7 ребро
  86. edges[10,0] := @points[5]; edges[10,1] := @points[6]; //соединияем 5 c 6 ребро
  87. edges[11,0] := @points[6]; edges[11,1] := @points[7]; //соединияем 6 c 7 ребро
  88.  
  89. e_count := 11; // количество рёбер
  90.  
  91. end;
  92. with hexahedron2 do begin // вторая фигура черный квадрат
  93. points[0] := TPoint3D.Point3D(-a, -a, -a); // координаты вершины 0
  94. points[1] := TPoint3D.Point3D(-a, -a, a); // координаты вершины 1
  95. points[2] := TPoint3D.Point3D(a, -a, a); // координаты вершины 2
  96. points[3] := TPoint3D.Point3D(a, -a, -a); // координаты вершины 3
  97. points[4] := TPoint3D.Point3D(a, a, -a); // координаты вершины 4
  98. points[5] := TPoint3D.Point3D(a, a, a); // координаты вершины 5
  99. points[6] := TPoint3D.Point3D(-a, a, a); // координаты вершины 6
  100. points[7] := TPoint3D.Point3D(-a, a, -a); // координаты вершины 7
  101. {points[0] := TPoint3D.Point3D(10, 50, a); // координаты вершины 0
  102. points[1] := TPoint3D.Point3D(a, a, 3*a); // координаты вершины 1 4
  103. points[2] := TPoint3D.Point3D(3*a, a, 3*a); // координаты вершины 2 7
  104. points[3] := TPoint3D.Point3D(250, 250, 400); // координаты вершины 3 1
  105. points[4] := TPoint3D.Point3D(300, 250, a); // координаты вершины 4
  106. points[5] := TPoint3D.Point3D(3*a, 3*a, 3*a); // координаты вершины 5
  107. points[6] := TPoint3D.Point3D(a, 3*a, 3*a); // координаты вершины 6
  108. points[7] := TPoint3D.Point3D(250, 50, 10); // координаты вершины 7 }
  109.  
  110. p_count := 7; // количество вершин
  111. { 5_________6
  112. /| /|
  113. /_|_______/7|
  114. 4| |_______|_|
  115. | /0 | /1
  116. |/________|/
  117. 3 2 }
  118. edges[0,0] := @points[0]; edges[0,1] := @points[1]; //соединияем 0 c 1 ребро
  119. edges[1,0] := @points[0]; edges[1,1] := @points[3]; //соединияем 0 c 3 ребро
  120. edges[2,0] := @points[0]; edges[2,1] := @points[7]; //соединияем 0 c 7 ребро
  121. edges[3,0] := @points[1]; edges[3,1] := @points[6]; //соединияем 1 c 6 ребро
  122. edges[4,0] := @points[1]; edges[4,1] := @points[2]; //соединияем 1 c 2 ребро
  123. edges[5,0] := @points[2]; edges[5,1] := @points[3]; //соединияем 2 c 3 ребро
  124. edges[6,0] := @points[2]; edges[6,1] := @points[5]; //соединияем 2 c 5 ребро
  125. edges[7,0] := @points[3]; edges[7,1] := @points[4]; //соединияем 3 c 4 ребро
  126. edges[8,0] := @points[4]; edges[8,1] := @points[5]; //соединияем 4 c 5 ребро
  127. edges[9,0] := @points[4]; edges[9,1] := @points[7]; //соединияем 4 c 7 ребро
  128. edges[10,0] := @points[5]; edges[10,1] := @points[6]; //соединияем 5 c 6 ребро
  129. edges[11,0] := @points[6]; edges[11,1] := @points[7]; //соединияем 6 c 7 ребро
  130.  
  131. e_count := 11; // количество рёбер
  132.  
  133. end;
  134. //DrawFigure(hexahedron2, clBlack);
  135. end;
  136.  
  137. procedure TForm1.InitMatrix;
  138. var
  139. nx, ny, nz: real;
  140. begin
  141. move_mtx[0,0] := 1; // 1
  142. move_mtx[0,1] := 0;
  143. move_mtx[0,2] := 0;
  144. move_mtx[0,3] := 0;
  145. move_mtx[1,0] := 0; // 2
  146. move_mtx[1,1] := 1;
  147. move_mtx[1,2] := 0;
  148. move_mtx[1,3] := 0;
  149. move_mtx[2,0] := 0; // 3
  150. move_mtx[2,1] := 0;
  151. move_mtx[2,2] := 1;
  152. move_mtx[2,3] := 0;
  153. move_mtx[3,0] := Image1.Width/2; // 4
  154. move_mtx[3,1] := Image1.Height/2;
  155. move_mtx[3,2] := 0;
  156. move_mtx[3,3] := 1;
  157.  
  158. isometry_mtx[0,0] := cos(45); // 1 матрица преобразования изометрии
  159. isometry_mtx[0,1] := sin(45)*cos(35.26);
  160. isometry_mtx[0,2] := 0;
  161. isometry_mtx[0,3] := 0;
  162. isometry_mtx[1,0] := 0; // 2 где тета=35.26 градусов и фи = 45 градусов
  163. isometry_mtx[1,1] := cos(35.26);
  164. isometry_mtx[1,2] := 0;
  165. isometry_mtx[1,3] := 0;
  166. isometry_mtx[2,0] := sin(45); // 3
  167. isometry_mtx[2,1] := -cos(45)*sin(35.26);
  168. isometry_mtx[2,2] := 0;
  169. isometry_mtx[2,3] := 0;
  170. isometry_mtx[3,0] := 0; // 4
  171. isometry_mtx[3,1] := 0;
  172. isometry_mtx[3,2] := 0;
  173. isometry_mtx[3,3] := 1;
  174.  
  175. move_mtx2[0,0] := 1; // 1
  176. move_mtx2[0,1] := 0;
  177. move_mtx2[0,2] := 0;
  178. move_mtx2[0,3] := 0;
  179. move_mtx2[1,0] := 0; // 2
  180. move_mtx2[1,1] := 1;
  181. move_mtx2[1,2] := 0;
  182. move_mtx2[1,3] := 0;
  183. move_mtx2[2,0] := 0; // 3
  184. move_mtx2[2,1] := 0;
  185. move_mtx2[2,2] := 1;
  186. move_mtx2[2,3] := 0;
  187. move_mtx2[3,0] := Image1.Width/2-300; // 4
  188. move_mtx2[3,1] := Image1.Height/2;
  189. move_mtx2[3,2] := 0;
  190. move_mtx2[3,3] := 1;
  191.  
  192. end;
  193.  
  194. procedure TForm1.Timer1Timer(Sender: TObject);
  195. var
  196. temp_point: TPoint3D;
  197. turn_mtx_Y: matrix;
  198. i: Integer;
  199. angle, s, c: real;
  200. begin
  201. Inc(ticks);
  202.  
  203. //isometry_mtx[2,3] := 1/1000;
  204.  
  205. angle := ticks*1/50;
  206. s := sin(angle);
  207. c := cos(angle);
  208.  
  209. turn_mtx_Y[0,0] := c; // 1
  210. turn_mtx_Y[0,1] := 0;
  211. turn_mtx_Y[0,2] := -s;
  212. turn_mtx_Y[0,3] := 0;
  213. turn_mtx_Y[1,0] := 0; // 2
  214. turn_mtx_Y[1,1] := 1;
  215. turn_mtx_Y[1,2] := 0;
  216. turn_mtx_Y[1,3] := 0;
  217. turn_mtx_Y[2,0] := s; // 3
  218. turn_mtx_Y[2,1] := 0;
  219. turn_mtx_Y[2,2] := c;
  220. turn_mtx_Y[2,3] := 0;
  221. turn_mtx_Y[3,0] := 0; // 4
  222. turn_mtx_Y[3,1] := 0;
  223. turn_mtx_Y[3,2] := 0;
  224. turn_mtx_Y[3,3] := 1;
  225.  
  226. DrawFigure(hexahedron, clWhite);
  227. DrawFigure(hexahedron2, clWhite);
  228. InitHexahedron;
  229.  
  230. // InitHexahedron;
  231.  
  232. with hexahedron do
  233. for i := 0 to p_count do begin
  234. temp_point := Transform(points[i], turn_mtx_Y); //этат строчка не будэт
  235. temp_point := Transform(temp_point, isometry_mtx);
  236. temp_point := Transform(temp_point, move_mtx);
  237.  
  238. points[i].X := temp_point.X;
  239. points[i].Y := temp_point.Y;
  240. points[i].Z := temp_point.Z;
  241. end;
  242.  
  243.  
  244. DrawFigure(hexahedron, RGB(Random(256),Random(256),Random(256)));
  245.  
  246. with hexahedron2 do
  247. for i := 0 to p_count do begin
  248. temp_point := Transform(points[i], turn_mtx_Y); //этат строчка не будэт
  249. temp_point := Transform(temp_point, move_mtx2);
  250. points[i].X := temp_point.X;
  251. points[i].Y := temp_point.Y;
  252. points[i].Z := temp_point.Z;
  253. end;
  254. DrawFigure(hexahedron2, clBlack);
  255. end;
  256.  
  257. function TForm1.Transform(point: TPoint3D; trs: matrix): TPoint3D;
  258. var
  259. i, j: Integer;
  260. pointX, res: matrix;
  261. begin
  262. pointX[0,0] := point.X;
  263. pointX[0,1] := point.Y;
  264. pointX[0,2] := point.Z;
  265. pointX[0,3] := 1;
  266. for i := 0 to 3 do begin
  267. res[0, i] := 0;
  268. for j := 0 to 3 do
  269. res[0, i] := res[0, i] + pointX[0, j]*trs[j, i];
  270. end;
  271. Transform := TPoint3D.Point3D(res[0, 0]/res[0, 3], res[0, 1]/res[0, 3], res[0, 2]/res[0, 3], res[0, 3]);
  272. end;
  273.  
  274.  
  275. procedure TForm1.DrawFigure(fig: figure; col: TColor);
  276. var i: integer;
  277. line: array [0..1] of TPoint;
  278. begin
  279. Image1.Canvas.Pen.Color := col;
  280. for i := 0 to fig.e_count do begin
  281. line[0] := Point(Round(fig.edges[i,0]^.X), Round(fig.edges[i,0]^.Y));
  282. line[1] := Point(Round(fig.edges[i,1]^.X), Round(fig.edges[i,1]^.Y));
  283. Image1.Canvas.Polygon(line);
  284. end;
  285. end;
  286.  
  287. end.
  288.  
  289. end.
RAW Paste Data