MainUnit.pas 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. unit MainUnit;
  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 Vectorial Polygon Rasterizer for Graphics32
  23. *
  24. * The Initial Developer of the Original Code is
  25. * Christian-W. Budde
  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. {$include GR32.inc}
  35. uses
  36. {$IFDEF FPC}LCLIntf, {$ELSE}Windows, {$ENDIF} SysUtils, Classes, Graphics,
  37. Controls, Forms, Dialogs, GR32, GR32_Image, GR32_Polygons, GR32_Paths;
  38. type
  39. TFormBezier = class(TForm)
  40. PaintBox32: TPaintBox32;
  41. procedure FormCreate(Sender: TObject);
  42. procedure FormDestroy(Sender: TObject);
  43. procedure PaintBox32PaintBuffer(Sender: TObject);
  44. procedure PaintBox32DblClick(Sender: TObject);
  45. procedure PaintBox32MouseDown(Sender: TObject; Button: TMouseButton;
  46. Shift: TShiftState; X, Y: Integer);
  47. procedure PaintBox32MouseUp(Sender: TObject; Button: TMouseButton;
  48. Shift: TShiftState; X, Y: Integer);
  49. procedure PaintBox32MouseMove(Sender: TObject; Shift: TShiftState; X,
  50. Y: Integer);
  51. procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  52. private
  53. FRenderer: TPolygonRenderer32VPR;
  54. FCurrentIndex: Integer;
  55. FVertices: TArrayOfFloatPoint;
  56. procedure RandomizeVertices;
  57. end;
  58. var
  59. FormBezier: TFormBezier;
  60. implementation
  61. {$R *.dfm}
  62. uses
  63. Math,
  64. Types,
  65. GR32_Math,
  66. GR32_LowLevel,
  67. GR32_VectorUtils;
  68. { TFormBezier }
  69. procedure TFormBezier.FormCreate(Sender: TObject);
  70. begin
  71. FRenderer := TPolygonRenderer32VPR.Create(PaintBox32.Buffer);
  72. SetLength(FVertices, 6);
  73. RandomizeVertices;
  74. FCurrentIndex := -1;
  75. end;
  76. procedure TFormBezier.FormDestroy(Sender: TObject);
  77. begin
  78. FRenderer.Free;
  79. end;
  80. procedure TFormBezier.FormKeyDown(Sender: TObject; var Key: Word;
  81. Shift: TShiftState);
  82. begin
  83. case Key of
  84. 27: Close;
  85. 13:
  86. begin
  87. RandomizeVertices;
  88. PaintBox32.Invalidate;
  89. end;
  90. 187:
  91. begin
  92. SetLength(FVertices, Length(FVertices) + 1);
  93. with PaintBox32 do
  94. FVertices[Length(FVertices) - 1] := FloatPoint(Random * Width,
  95. Random * Height);
  96. PaintBox32.Invalidate;
  97. end;
  98. 189:
  99. if Length(FVertices) > 5 then
  100. begin
  101. SetLength(FVertices, Length(FVertices) - 1);
  102. PaintBox32.Invalidate;
  103. end;
  104. end;
  105. end;
  106. function CubicInterpolation(const Fractional: TFloat;
  107. const Data0, Data1, Data2, Data3: TFloat): TFloat;
  108. begin
  109. Result := Data1 + 0.5 * Fractional * (Data2 - Data0 + Fractional *
  110. (4 * Data2 + 2 * Data0 - 5 * Data1 - Data3 + Fractional *
  111. (3 * (Data1 - Data2) - Data0 + Data3)));
  112. end;
  113. procedure TFormBezier.RandomizeVertices;
  114. var
  115. Index: Integer;
  116. begin
  117. with PaintBox32 do
  118. for Index := 0 to High(FVertices) do
  119. FVertices[Index] := FloatPoint(Random * Width, Random * Height);
  120. end;
  121. procedure TFormBezier.PaintBox32DblClick(Sender: TObject);
  122. begin
  123. RandomizeVertices;
  124. PaintBox32.Invalidate;
  125. end;
  126. procedure TFormBezier.PaintBox32MouseDown(Sender: TObject; Button: TMouseButton;
  127. Shift: TShiftState; X, Y: Integer);
  128. var
  129. Index: Integer;
  130. Dist, MinDist: TFloat;
  131. MinDistIndex: Integer;
  132. begin
  133. FCurrentIndex := -1;
  134. for Index := 0 to Length(FVertices) - 1 do
  135. if Sqr(FVertices[Index].X - X) + Sqr(FVertices[Index].Y - Y) < 25 then
  136. begin
  137. if (Length(FVertices) > 5) and (Button = mbRight) then
  138. begin
  139. if Index < Length(FVertices) - 1 then
  140. Move(FVertices[Index + 1], FVertices[Index],
  141. (Length(FVertices) - Index - 1) * SizeOf(TFloatPoint));
  142. SetLength(FVertices, Length(FVertices) - 1);
  143. PaintBox32.Invalidate;
  144. end
  145. else
  146. FCurrentIndex := Index;
  147. Exit;
  148. end;
  149. if Button = mbLeft then
  150. begin
  151. MinDistIndex := 0;
  152. MinDist := Sqr(X - FVertices[0].X) + Sqr(Y - FVertices[0].Y);
  153. for Index := 1 to High(FVertices) do
  154. begin
  155. Dist := Sqr(X - FVertices[Index].X) + Sqr(Y - FVertices[Index].Y);
  156. if Dist < MinDist then
  157. begin
  158. MinDistIndex := Index;
  159. MinDist := Dist;
  160. end;
  161. end;
  162. SetLength(FVertices, Length(FVertices) + 1);
  163. Move(FVertices[MinDistIndex], FVertices[MinDistIndex + 1],
  164. (Length(FVertices) - MinDistIndex) * SizeOf(TFloatPoint));
  165. FCurrentIndex := MinDistIndex;
  166. FVertices[FCurrentIndex] := FloatPoint(X, Y);
  167. PaintBox32.Invalidate;
  168. end;
  169. end;
  170. procedure TFormBezier.PaintBox32MouseMove(Sender: TObject; Shift: TShiftState;
  171. X, Y: Integer);
  172. begin
  173. if FCurrentIndex >= 0 then
  174. begin
  175. FVertices[FCurrentIndex] := FloatPoint(X, Y);
  176. PaintBox32.Invalidate;
  177. end;
  178. end;
  179. procedure TFormBezier.PaintBox32MouseUp(Sender: TObject; Button: TMouseButton;
  180. Shift: TShiftState; X, Y: Integer);
  181. begin
  182. if FCurrentIndex >= 0 then
  183. with PaintBox32 do
  184. begin
  185. FVertices[FCurrentIndex].X := EnsureRange(FVertices[FCurrentIndex].X,
  186. 0, Width);
  187. FVertices[FCurrentIndex].Y := EnsureRange(FVertices[FCurrentIndex].Y,
  188. 0, Height);
  189. end;
  190. FCurrentIndex := -1;
  191. PaintBox32.Invalidate;
  192. end;
  193. procedure TFormBezier.PaintBox32PaintBuffer(Sender: TObject);
  194. var
  195. Index: Integer;
  196. Val: Double;
  197. Fractional: Double;
  198. Indices: array [0..3] of Integer;
  199. PolyCount: Integer;
  200. Outline: TArrayOfArrayOfFloatPoint;
  201. const
  202. CVertexCountStep = 64;
  203. begin
  204. PaintBox32.Buffer.Clear($FFFFFFFF);
  205. Outline := BuildPolyPolyLine(PolyPolygon(FVertices), True, 2);
  206. PolyCount := Length(Outline);
  207. SetLength(Outline, PolyCount + Length(FVertices));
  208. for Index := 0 to Length(FVertices) - 1 do
  209. Outline[PolyCount + Index] := Circle(FVertices[Index].X, FVertices[Index].Y, 5, 32);
  210. FRenderer.Color := $80000080;
  211. FRenderer.PolyPolygonFS(Outline);
  212. SetLength(Outline, 1, CVertexCountStep);
  213. Outline[0, 0] := FVertices[0];
  214. Index := 0;
  215. Val := 0;
  216. while Val < Length(FVertices) do
  217. begin
  218. Indices[0] := (Length(FVertices) + Trunc(Val) - 2 + 1) mod Length(FVertices);
  219. Indices[1] := (Indices[0] + 1) mod Length(FVertices);
  220. Indices[2] := (Indices[1] + 1) mod Length(FVertices);
  221. Indices[3] := (Indices[2] + 1) mod Length(FVertices);
  222. Fractional := Frac(Val);
  223. Inc(Index);
  224. if Index = Length(Outline[0]) then
  225. SetLength(Outline[0], Length(Outline[0]) + CVertexCountStep);
  226. Outline[0, Index] := FloatPoint(
  227. CubicInterpolation(Fractional, FVertices[Indices[0]].X,
  228. FVertices[Indices[1]].X, FVertices[Indices[2]].X,
  229. FVertices[Indices[3]].X),
  230. CubicInterpolation(Fractional, FVertices[Indices[0]].Y,
  231. FVertices[Indices[1]].Y, FVertices[Indices[2]].Y,
  232. FVertices[Indices[3]].Y));
  233. Val := Val + 0.03;
  234. end;
  235. SetLength(Outline[0], Index + 1);
  236. FRenderer.Color := $FF000000;
  237. FRenderer.PolyPolygonFS(BuildPolyPolyline(Outline, True, 2));
  238. end;
  239. end.