home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / pctchnqs / 1991 / number2 / poly.pas < prev    next >
Pascal/Delphi Source File  |  1991-03-27  |  2KB  |  81 lines

  1. { poly.pas -- Draw a complex filled polygon }
  2.  
  3. unit Poly;
  4.  
  5. interface
  6.  
  7. uses WinTypes, WObjects;
  8.  
  9. const
  10.  
  11.   maxPolyPoints = 100;   { Maximum points at 4 bytes each }
  12.  
  13. type
  14.  
  15.   PointArray = Array[0 .. maxPolyPoints - 1] of TPoint;
  16.  
  17.   PPolygon = ^TPolygon;
  18.   TPolygon = object(TObject)
  19.     PA: ^PointArray;    { Pointer to polygon points array }
  20.     Count: Integer;     { Number of points in the array }
  21.     PolyBrush: HBrush;  { Brush pattern for filling polygon }
  22.     constructor Init(NumPoints, XMax, YMax: Integer);
  23.     destructor Done; virtual;
  24.     procedure Draw(DC: HDc); virtual;
  25.   end;
  26.  
  27. implementation
  28.  
  29. uses WinProcs;
  30.  
  31. { TPolygon }
  32.  
  33. {- Construct new instance of a randomly ordered polygon }
  34. constructor TPolygon.Init(NumPoints, XMax, YMax: Integer);
  35. var
  36.   I: Integer;
  37. begin
  38.   TObject.Init;
  39.   if NumPoints < 3 then NumPoints := 3;
  40.   GetMem(PA, NumPoints * Sizeof(TPoint));
  41.   if PA = nil then Fail else
  42.   begin
  43.     Count := NumPoints;
  44.     for I := 0 to Count - 1 do with PA^[I] do
  45.     begin
  46.       x := Random(XMax);
  47.       y := Random(YMax)
  48.     end;
  49.     PolyBrush := CreateHatchBrush(hs_DiagCross,
  50.       RGB(Random(255), Random(255), Random(255)))
  51.   end
  52. end;
  53.  
  54. {- Destroy a TPolygon instance, disposing the PA array }
  55. destructor TPolygon.Done;
  56. begin
  57.   if PA <> nil then FreeMem(PA, Count * Sizeof(TPoint));
  58.   DeleteObject(PolyBrush)  { Delete fill brush }
  59. end;
  60.  
  61. {- Draw polygon to display context represented by DC }
  62. procedure TPolygon.Draw(DC: HDc);
  63. var
  64.   OldBrush: HBrush;
  65. begin
  66.   if PA <> nil then
  67.   begin
  68.     OldBrush := SelectObject(DC, PolyBrush);
  69.     Polygon(DC, PA^, Count);    { Note dereference! }
  70.     SelectObject(DC, OldBrush);
  71.   end
  72. end;
  73.  
  74. end.
  75.  
  76.  
  77. {--------------------------------------------------------------
  78.   Copyright (c) 1991 by Tom Swan. All rights reserved.
  79.   Revision 1.00    Date: 3/26/1991
  80. ---------------------------------------------------------------}
  81.