MainUnit.pas 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 Line Stippling Example
  23. *
  24. * The Initial Developer of the Original Code is
  25. * Alex A. Denisov
  26. *
  27. * Portions created by the Initial Developer are Copyright (C) 2000-2005
  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, LResources, {$ENDIF}
  37. SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls,
  38. GR32,
  39. GR32_Image;
  40. type
  41. TFormLineStippling = class(TForm)
  42. PaintBox: TPaintBox32;
  43. ScrollBar: TScrollBar;
  44. TimerAnimate: TTimer;
  45. procedure ScrollBarChange(Sender: TObject);
  46. procedure PaintBoxPaintBuffer(Sender: TObject);
  47. procedure PaintBoxClick(Sender: TObject);
  48. procedure TimerAnimateTimer(Sender: TObject);
  49. private
  50. FStippleCounter: Single;
  51. public
  52. procedure Spiral(X, Y: Integer);
  53. end;
  54. var
  55. FormLineStippling: TFormLineStippling;
  56. implementation
  57. {$R *.dfm}
  58. uses
  59. Math,
  60. GR32_Gamma,
  61. GR32_Math;
  62. { TFormLineStippling }
  63. procedure TFormLineStippling.PaintBoxClick(Sender: TObject);
  64. begin
  65. TimerAnimate.Enabled := not TimerAnimate.Enabled;
  66. end;
  67. procedure TFormLineStippling.PaintBoxPaintBuffer(Sender: TObject);
  68. var
  69. Step: Single;
  70. Stipple: TArrayOfColor32;
  71. begin
  72. Step := ScrollBar.Position * 0.01;
  73. PaintBox.Buffer.BeginUpdate;
  74. try
  75. PaintBox.Buffer.Clear(clBlack32);
  76. PaintBox.Buffer.StippleStep := Step;
  77. // Note that we are not using PaintBox.Buffer.Width & Height since
  78. // PaintBox.BufferOversize might cause the buffer to be bigger than
  79. // the control.
  80. // Dynamic array of colors
  81. Stipple := [clWhite32, clWhite32, clWhite32, clWhite32, 0, 0, 0, 0];
  82. PaintBox.Buffer.SetStipple(Stipple);
  83. PaintBox.Buffer.StippleCounter := FStippleCounter;
  84. Spiral(PaintBox.Width div 4, PaintBox.Height div 4);
  85. // Static array of colors
  86. PaintBox.Buffer.SetStipple([clWhite32, $00FFFFFF]);
  87. PaintBox.Buffer.StippleCounter := FStippleCounter;
  88. Spiral(3*PaintBox.Width div 4, PaintBox.Height div 4);
  89. PaintBox.Buffer.SetStipple([clWhite32, clRed32, clGreen32, 0, 0, 0]);
  90. PaintBox.Buffer.StippleCounter := FStippleCounter;
  91. Spiral(PaintBox.Width div 4, 3*PaintBox.Height div 4);
  92. PaintBox.Buffer.SetStipple([clGreen32, clGreen32, clGreen32, 0, 0, clWhite32, 0, 0]);
  93. PaintBox.Buffer.StippleCounter := FStippleCounter;
  94. Spiral(3*PaintBox.Width div 4, 3*PaintBox.Height div 4);
  95. finally
  96. PaintBox.Buffer.EndUpdate;
  97. end;
  98. end;
  99. procedure TFormLineStippling.ScrollBarChange(Sender: TObject);
  100. begin
  101. PaintBox.Invalidate;
  102. end;
  103. procedure TFormLineStippling.Spiral(X, Y: Integer);
  104. var
  105. Theta: Single;
  106. Sn, Cn: Single;
  107. Step: Single;
  108. begin
  109. PaintBox.Buffer.MoveToF(X, Y);
  110. Theta := 0;
  111. Step := 40 / Max(PaintBox.Width, PaintBox.Height);
  112. while Theta < 15 * Pi do
  113. begin
  114. GR32_Math.SinCos(Theta, Sn, Cn);
  115. PaintBox.Buffer.LineToFSP(X + Cn * Theta * (PaintBox.Width / 220), Y + Sn * Theta * (PaintBox.Height / 220));
  116. Theta := Theta + Step;
  117. end;
  118. end;
  119. procedure TFormLineStippling.TimerAnimateTimer(Sender: TObject);
  120. begin
  121. FStippleCounter := FStippleCounter + 0.1;
  122. PaintBox.Invalidate;
  123. end;
  124. end.