fClipper.pas 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. unit fClipper;
  2. (* ***** BEGIN LICENSE BLOCK *****
  3. * Version: MPL 1.1 or LGPL 2.1 with linking exception
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. * http://www.mozilla.org/MPL/
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. *
  15. * Alternatively, the contents of this file may be used under the terms of the
  16. * Free Pascal modified version of the GNU Lesser General Public License
  17. * Version 2.1 (the "FPC modified LGPL License"), in which case the provisions
  18. * of this license are applicable instead of those above.
  19. * Please see the file LICENSE.txt for additional information concerning this
  20. * license.
  21. *
  22. * The Original Code is GR32_Clipper Example
  23. *
  24. * The Initial Developer of the Original Code is
  25. * Angus Johnson
  26. *
  27. * Portions created by the Initial Developer are Copyright (C) 2012
  28. * the Initial Developer. All Rights Reserved.
  29. *
  30. * Contributor(s):
  31. *
  32. * ***** END LICENSE BLOCK ***** *)
  33. interface
  34. {$I GR32.inc}
  35. uses
  36. {$IFNDEF FPC}Windows, {$ELSE} LCLIntf, LCLType, {$ENDIF} SysUtils, Classes,
  37. Types, Graphics, Controls, Forms, Dialogs, ExtCtrls, StdCtrls, Math,
  38. GR32, GR32_Image, GR32_Polygons, GR32_Layers, GR32_Geometry,
  39. GR32_Math, GR32_VectorUtils, GR32_Clipper;
  40. type
  41. TFrmClipper = class(TForm)
  42. BtnClear: TButton;
  43. BtnExit: TButton;
  44. ImgView32: TImgView32;
  45. PnlControl: TPanel;
  46. rgClipping: TRadioGroup;
  47. RgpObject: TRadioGroup;
  48. BtnInflate: TButton;
  49. BtnDeflate: TButton;
  50. procedure FormCreate(Sender: TObject);
  51. procedure BtnExitClick(Sender: TObject);
  52. procedure ImgView32MouseMove(Sender: TObject; Shift: TShiftState; X,
  53. Y: Integer; Layer: TCustomLayer);
  54. procedure ImgView32MouseDown(Sender: TObject; Button: TMouseButton;
  55. Shift: TShiftState; X, Y: Integer; Layer: TCustomLayer);
  56. procedure BtnClearClick(Sender: TObject);
  57. procedure ImgView32MouseLeave(Sender: TObject);
  58. procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  59. procedure BtnInflateClick(Sender: TObject);
  60. procedure BtnDeflateClick(Sender: TObject);
  61. private
  62. Polys: TArrayOfArrayOfFloatPoint;
  63. OutlinePolygon: TArrayOfFloatPoint;
  64. procedure AddPolygon(const Pts: TArrayOfFloatPoint);
  65. function MakeRectangle(const NewPoint: TPoint): TArrayOfFloatPoint;
  66. function MakeEllipse(const NewPoint: TPoint): TArrayOfFloatPoint;
  67. function MakeStar(const NewPoint: TPoint): TArrayOfFloatPoint;
  68. procedure DrawPolygons;
  69. end;
  70. var
  71. FrmClipper: TFrmClipper;
  72. implementation
  73. {$IFDEF FPC}
  74. {$R *.lfm}
  75. {$ELSE}
  76. {$R *.dfm}
  77. {$ENDIF}
  78. procedure DrawStippled(Bitmap: TBitmap32;
  79. const Afp: TArrayOfFloatPoint;
  80. StippleColors: array of TColor32; StippleStep: TFloat);
  81. var
  82. i: Integer;
  83. begin
  84. if Afp = nil then Exit;
  85. Bitmap.StippleStep := StippleStep;
  86. Bitmap.SetStipple(StippleColors);
  87. Bitmap.MoveToF(Afp[0].X, Afp[0].Y);
  88. for i := 1 to High(Afp) do
  89. Bitmap.LineToFSP(Afp[i].X, Afp[i].Y);
  90. Bitmap.LineToFSP(Afp[0].X, Afp[0].Y);
  91. end;
  92. { TFrmClipper methods }
  93. procedure TFrmClipper.FormCreate(Sender: TObject);
  94. begin
  95. ImgView32.SetupBitmap(true);
  96. AddPolygon(MakeStar(GR32.Point(125, 150)));
  97. ImgView32.ScrollToCenter(0, 0);
  98. end;
  99. procedure TFrmClipper.FormKeyDown(Sender: TObject; var Key: Word;
  100. Shift: TShiftState);
  101. begin
  102. if Key = 27 then
  103. Exit;
  104. end;
  105. procedure TFrmClipper.AddPolygon(const Pts: TArrayOfFloatPoint);
  106. var
  107. ct: TClipType;
  108. begin
  109. with TClipper.Create do
  110. try
  111. //add multiple contours of existing polygons as subject polygons ...
  112. AddPaths(Polys, ptSubject);
  113. //add the single contour of the new polygon as the clipping polygon ...
  114. AddPath(Pts, ptClip);
  115. //do the clipping operation (result => Polys) ...
  116. case rgClipping.ItemIndex of
  117. 0: ct := ctIntersection;
  118. 1: ct := ctUnion;
  119. 2: ct := ctDifference;
  120. else ct := ctXor;
  121. end;
  122. Execute(ct, frNonZero, Polys);
  123. finally
  124. free;
  125. end;
  126. DrawPolygons;
  127. end;
  128. function TFrmClipper.MakeRectangle(const NewPoint: TPoint): TArrayOfFloatPoint;
  129. begin
  130. SetLength(Result, 4);
  131. Result[0] := FloatPoint(NewPoint.X - 50, NewPoint.Y - 30);
  132. Result[1] := FloatPoint(NewPoint.X + 50, NewPoint.Y - 30);
  133. Result[2] := FloatPoint(NewPoint.X + 50, NewPoint.Y + 30);
  134. Result[3] := FloatPoint(NewPoint.X - 50, NewPoint.Y + 30);
  135. end;
  136. function TFrmClipper.MakeEllipse(const NewPoint: TPoint): TArrayOfFloatPoint;
  137. begin
  138. Result := Ellipse(FloatPoint(NewPoint), FloatPoint(60,40));
  139. end;
  140. function TFrmClipper.MakeStar(const NewPoint: TPoint): TArrayOfFloatPoint;
  141. begin
  142. Result := Star(FloatPoint(NewPoint), 40.0, 60.0, 7);
  143. end;
  144. procedure TFrmClipper.DrawPolygons;
  145. begin
  146. ImgView32.Bitmap.FillRectS(ImgView32.Bitmap.BoundsRect, clWhite32);
  147. PolyPolyLineFS(ImgView32.Bitmap, Polys, clRed32, True, 2);
  148. PolyPolygonFS(ImgView32.Bitmap, Polys, $40FF0000, pfWinding);
  149. DrawStippled(ImgView32.Bitmap,
  150. OutlinePolygon, [clBlue32, clBlue32, $000000FF], 0.35);
  151. end;
  152. procedure TFrmClipper.ImgView32MouseDown(Sender: TObject;
  153. Button: TMouseButton; Shift: TShiftState; X, Y: Integer;
  154. Layer: TCustomLayer);
  155. begin
  156. AddPolygon(OutlinePolygon);
  157. end;
  158. procedure TFrmClipper.ImgView32MouseMove(Sender: TObject;
  159. Shift: TShiftState; X, Y: Integer; Layer: TCustomLayer);
  160. var
  161. NewPt: TPoint;
  162. begin
  163. NewPt := ImgView32.ControlToBitmap(GR32.Point(X, Y));
  164. case RgpObject.ItemIndex of
  165. 0: OutlinePolygon := MakeStar(NewPt);
  166. 1: OutlinePolygon := MakeEllipse(NewPt);
  167. else OutlinePolygon := MakeRectangle(NewPt);
  168. end;
  169. DrawPolygons;
  170. end;
  171. procedure TFrmClipper.ImgView32MouseLeave(Sender: TObject);
  172. begin
  173. OutlinePolygon := nil;
  174. DrawPolygons;
  175. end;
  176. procedure TFrmClipper.BtnExitClick(Sender: TObject);
  177. begin
  178. Close;
  179. end;
  180. procedure TFrmClipper.BtnClearClick(Sender: TObject);
  181. begin
  182. Polys := nil;
  183. DrawPolygons;
  184. end;
  185. procedure TFrmClipper.BtnInflateClick(Sender: TObject);
  186. begin
  187. Polys := InflatePaths(Polys, 10, jtRound, etPolygon);
  188. DrawPolygons;
  189. end;
  190. procedure TFrmClipper.BtnDeflateClick(Sender: TObject);
  191. begin
  192. Polys := InflatePaths(Polys, -10, jtRound, etPolygon);
  193. DrawPolygons;
  194. end;
  195. end.