home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / pascal / library / dos / grafik / gr3d / gr3d_pt.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1993-08-23  |  4.7 KB  |  232 lines

  1. Unit Gr3d_pt;
  2. {$N+,E+}
  3.  
  4. interface
  5.  
  6. uses
  7.   MM_Mem, Graph;
  8.  
  9. const
  10.   GraphMidX: integer = 0;
  11.   GraphMidY: integer = 0;
  12.   DefaultColor: integer = Black;
  13.   BackColor: integer = Black;
  14.  
  15.  
  16. type
  17.  
  18.   float = real;
  19.  
  20.   P3dPoint = ^T3dPoint;
  21.   T3dPoint = record
  22.     x, y, z: integer;
  23.   end;
  24.  
  25.   P2dPoint = ^T2dPoint;
  26.   T2dPoint = record
  27.     x, y: integer;
  28.   end;
  29.  
  30.   PPointGroup = ^TPointGroup;
  31.   TPointGroup = object
  32.     M: PMatrix;
  33.     N: integer;
  34.     constructor Init(InitX, InitY, InitZ: integer);
  35.     destructor Done;
  36.     procedure Move(DX, DY, DZ: integer); virtual;
  37.     procedure Draw; virtual;
  38.     procedure Hide; virtual;
  39.     procedure RotateXZ(Angle: integer); virtual;
  40.     procedure Rotate(AngleXZ, AngleXY, AngleYZ: integer); virtual;
  41.     procedure InsertPoint(NewX, NewY, NewZ: integer);
  42.     procedure Assign(R: byte; NewX, NewY, NewZ: integer);
  43.     function Getx(R: byte): integer;
  44.     function Gety(R: byte): integer;
  45.     function Getz(R: byte): integer;
  46.   end;
  47.  
  48.   procedure ConvertPoint(x,y,z: integer; var Sx, Sy: integer);
  49.   procedure M3dShift(var M: PMatrix; Dx, Dy, Dz: integer);
  50.  
  51. implementation
  52.  
  53. procedure ConvertPoint(x,y,z: integer; var Sx, Sy: integer);
  54. begin
  55.   if y <> 0 then
  56.   begin
  57.     Sx := (x*GraphMidx) div y + GraphMidx;
  58.     Sy := GraphMidY - (z*GraphMidy) div y;
  59.   end
  60.   else
  61.   begin
  62.     Sx := x*GraphMidx;
  63.     Sy := -z*GraphMidy;
  64.   end;
  65. end;
  66.  
  67. procedure M3dShift(var M: PMatrix; Dx, Dy, Dz: integer);
  68. var
  69.   i: integer;
  70. begin
  71.   for i := 1 to M^.DimRow do
  72.   begin
  73.     Inc(GetPtr(M, i, 1)^, Dx);
  74.     Inc(GetPtr(M, i, 2)^, Dy);
  75.     Inc(GetPtr(M, i, 3)^, Dz);
  76.   end;
  77. end;
  78.  
  79. {------------------}
  80. { TPointGroup }
  81.  
  82. constructor TPointGroup.Init(InitX, InitY, InitZ: integer);
  83. begin
  84.   N := 1;
  85.   NewMatrix(M, N, 3);
  86.   Assign(1, InitX, InitY, InitZ);
  87. end;
  88.  
  89. destructor TPointGroup.Done;
  90. begin
  91.   DelMatrix(M);
  92. end;
  93.  
  94. procedure TPointGroup.InsertPoint(NewX, NewY, NewZ: integer);
  95. var
  96.   T: PMatrix;
  97. begin
  98.   N := N+1;
  99.   NewMatrix(T, 1, 3);
  100.   MStoreVal(T, 1, 1, NewX);
  101.   MStoreVal(T, 1, 2, NewY);
  102.   MStoreVal(T, 1, 3, NewZ);
  103.   MInsert(M,T,M^.DimRow+1);
  104.   DelMatrix(T);
  105. end;
  106.  
  107. procedure TPointGroup.Assign(R: byte; NewX, NewY, NewZ: integer);
  108. begin
  109.   MStoreVal(M, R, 1, NewX);
  110.   MStoreVal(M, R, 2, NewY);
  111.   MStoreVal(M, R, 3, NewZ);
  112. end;
  113.  
  114. procedure TPointGroup.Move(DX, DY, DZ: integer);
  115. begin
  116.   M3dShift(M, Dx, Dy, Dz);
  117. end;
  118.  
  119. function TPointGroup.Getx(R: byte): integer;
  120. var
  121.   Result: integer;
  122. begin
  123.   MGetVal(M, R, 1, TMCell(Result));
  124.   Getx := Result;
  125. end;
  126.  
  127. function TPointGroup.Gety(R: byte): integer;
  128. var
  129.   Result: integer;
  130. begin
  131.   MGetVal(M, R, 2, TMCell(Result));
  132.   Gety := Result;
  133. end;
  134.  
  135. function TPointGroup.Getz(R: byte): integer;
  136. var
  137.   Result: integer;
  138. begin
  139.   MGetVal(M, R, 3, TMCell(Result));
  140.   Getz := Result;
  141. end;
  142.  
  143. procedure TPointGroup.Draw;
  144. begin
  145. end;
  146.  
  147. procedure TPointGroup.Hide;
  148. var
  149.   TempC: Integer;
  150. begin
  151.   TempC := GetColor;
  152.   SetColor(BackColor);
  153.   Draw;
  154.   SetColor(TempC);
  155. end;
  156.  
  157. procedure TPointGroup.RotateXZ(Angle: integer);
  158. var
  159.   Trans, Temp, R: PMatrix;
  160.   VCos, VSin: float;
  161.   P1, P2: P3dPoint;
  162.   i,j: integer;
  163. const
  164.   Factor: integer = 100;
  165. begin
  166.   VCos := cos(Angle*pi/180);
  167.   VSin := sin(Angle*pi/180);
  168.   NewMatrix(Trans, M^.DimRow, M^.DimCol);
  169.   for i := 1 to M^.DimRow do
  170.   begin
  171.     P1 := P3dPoint(GetPtr(M,i,1));
  172.     P2 := P3dPoint(GetPtr(Trans,i,1));
  173.     P2^.x := Trunc(P1^.x*VCos+P1^.z*VSin);
  174.     P2^.y := P1^.y;
  175.     P2^.z := Trunc(-P1^.x*VSin+P1^.z*VCos);
  176.   end;
  177.  
  178.   Temp := M;
  179.   M := Trans;
  180.   Draw;
  181.   M := Temp;
  182.   DelMatrix(Trans);
  183. end;
  184.  
  185. procedure TPointGroup.Rotate(AngleXZ, AngleXY, AngleYZ: integer);
  186. var
  187.   Trans, Temp, R: PMatrix;
  188.   VCosXZ, VSinXZ: float;
  189.   VCosXY, VSinXY: float;
  190.   VCosYZ, VSinYZ: float;
  191.   P1, P2: P3dPoint;
  192.   i: integer;
  193. begin
  194.   VCosXZ := cos(AngleXZ*pi/180);
  195.   VSinXZ := sin(AngleXZ*pi/180);
  196.   VCosXY := cos(AngleXY*pi/180);
  197.   VSinXY := sin(AngleXY*pi/180);
  198.   VCosYZ := cos(AngleYZ*pi/180);
  199.   VSinYZ := sin(AngleYZ*pi/180);
  200.   NewMatrix(Trans, M^.DimRow, M^.DimCol);
  201.  
  202.   for i := 1 to M^.DimRow do
  203.   begin
  204.     P1 := P3dPoint(GetPtr(M,i,1));
  205.     P2 := P3dPoint(GetPtr(Trans,i,1));
  206.     if AngleXZ<>0 then
  207.     begin
  208.       P2^.x := Trunc(P1^.x*VCosXZ+P1^.z*VSinXZ);
  209.       P2^.y := P1^.y;
  210.       P2^.z := Trunc(-P1^.x*VSinXZ+P1^.z*VCosXZ);
  211.     end;
  212.     if AngleXY<>0 then
  213.     begin
  214.       P2^.x := Trunc(P1^.x*VCosXY+P1^.y*VCosXY);
  215.       P2^.y := Trunc(-P1^.x*VSinXY+P1^.y*VCosXY);
  216.       P2^.z := P1^.z;
  217.     end;
  218.     if AngleYZ<>0 then
  219.     begin
  220.       P2^.x := P1^.x;
  221.       P2^.y := Trunc(P1^.y*VCosYZ+P1^.z*VSinYZ);
  222.       P2^.z := Trunc(-P1^.y*VSinYZ+P1^.z*VCosYZ);
  223.     end;
  224.   end;
  225.  
  226.   Temp := M;
  227.   M := Trans;
  228.   Draw;
  229.   M := Temp;
  230.   DelMatrix(Trans);
  231. end;
  232. end. {Unit}