bezier.pas 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. Program Bezier;
  2. {
  3. This program draws Bezier curves using the degree elevation
  4. method. For large numbers of points (more than 10, for
  5. example) this is faster than the recursive way.
  6. }
  7. {
  8. History:
  9. Changed the source to use 2.0+.
  10. Looks a lot better.
  11. Added CloseWindowSafely.
  12. Made the window dynamic, it will
  13. adjust the size after the screen size.
  14. 9 May 1998.
  15. Translated the source to fpc.
  16. 20 Aug 1998.
  17. Changed to use TAGS and pas2c.
  18. 31 Oct 1998.
  19. Removed Opening of graphics.library,
  20. handled by graphics.pas.
  21. 21 Mar 2001.
  22. Uses systemvartags and
  23. OpenScreenTags
  24. OpenWindowTags
  25. Text to GText.
  26. 09 Nov 2002.
  27. [email protected]
  28. }
  29. uses exec, intuition, agraphics, utility;
  30. type
  31. PointRec = packed Record
  32. X, Y : Real;
  33. end;
  34. Const
  35. w : pWindow = Nil;
  36. s : pScreen = Nil;
  37. {
  38. This will make the new look for screen.
  39. SA_Pens, Integer(pens)
  40. }
  41. pens : array [0..0] of integer = (not 0);
  42. Var
  43. rp : pRastPort;
  44. PointCount : Word;
  45. Points : Array [1..200] of PointRec;
  46. LastX, LastY : Word;
  47. Procedure CleanUpAndDie;
  48. begin
  49. if assigned(w) then CloseWindow(w);
  50. if assigned(s) then CloseScreen(s);
  51. Halt(0);
  52. end;
  53. Procedure DrawLine;
  54. begin
  55. GFXMove(rp, Trunc(Points[PointCount].X), Trunc(Points[PointCount].Y));
  56. Draw(rp, LastX, LastY);
  57. end;
  58. Procedure GetPoints;
  59. var
  60. LastSeconds,
  61. LastMicros : Longint;
  62. IM : pIntuiMessage;
  63. StoreMsg : tIntuiMessage;
  64. Leave : Boolean;
  65. OutOfBounds : Boolean;
  66. BorderLeft, BorderRight,
  67. BorderTop, BorderBottom : Word;
  68. dummy : Boolean;
  69. Procedure AddPoint;
  70. begin
  71. Inc(PointCount);
  72. with Points[PointCount] do begin
  73. X := Real(StoreMsg.MouseX);
  74. Y := Real(StoreMsg.MouseY);
  75. end;
  76. with StoreMsg do begin
  77. LastX := MouseX;
  78. LastY := MouseY;
  79. LastSeconds := Seconds;
  80. LastMicros := Micros;
  81. end;
  82. SetAPen(rp, 2);
  83. SetDrMd(rp, JAM1);
  84. DrawEllipse(rp, LastX, LastY, 5, 3);
  85. SetAPen(rp, 3);
  86. SetDrMd(rp, COMPLEMENT);
  87. DrawLine;
  88. end;
  89. Function CheckForExit : Boolean;
  90. { This function determines whether the user wanted to stop
  91. entering points. I added the position tests because my
  92. doubleclick time is too long, and I was too lazy to dig
  93. out Preferences to change it. }
  94. begin
  95. with StoreMsg do
  96. CheckForExit := DoubleClick(LastSeconds, LastMicros,
  97. Seconds, Micros) and
  98. (Abs(MouseX - Trunc(Points[PointCount].X)) < 5) and
  99. (Abs(MouseY - TRunc(Points[PointCount].Y)) < 3);
  100. end;
  101. Procedure ClearIt;
  102. { This just clears the screen when you enter your first point }
  103. begin
  104. SetDrMd(rp, JAM1);
  105. SetAPen(rp, 0);
  106. RectFill(rp, BorderLeft, BorderTop, BorderRight, BorderBottom);
  107. SetDrMd(rp, COMPLEMENT);
  108. SetAPen(rp, 3);
  109. end;
  110. begin
  111. dummy := ModifyIDCMP(w, IDCMP_CLOSEWINDOW or IDCMP_MOUSEBUTTONS or IDCMP_MOUSEMOVE);
  112. SetDrMd(rp, COMPLEMENT);
  113. PointCount := 0;
  114. Leave := False;
  115. OutOfBounds := False;
  116. BorderLeft := w^.BorderLeft;
  117. BorderRight := (w^.Width - w^.BorderRight) -1;
  118. BorderTop := w^.BorderTop;
  119. BorderBottom := (w^.Height - w^.BorderBottom) -1;
  120. repeat
  121. IM := pIntuiMessage(WaitPort(w^.UserPort));
  122. IM := pIntuiMessage(GetMsg(w^.UserPort));
  123. StoreMsg := IM^;
  124. ReplyMsg(pMessage(IM));
  125. case StoreMsg.IClass of
  126. IDCMP_MOUSEMOVE : if PointCount > 0 then begin
  127. if not OutOfBounds then
  128. DrawLine;
  129. LastX := StoreMsg.MouseX;
  130. LastY := StoreMsg.MouseY;
  131. if (LastX > BorderLeft) and
  132. (LastX < BorderRight) and
  133. (LastY > BorderTop) and
  134. (LastY < BorderBottom) then begin
  135. DrawLine;
  136. OutOfBounds := False;
  137. end else
  138. OutOfBounds := True;
  139. end;
  140. IDCMP_MOUSEBUTTONS : if StoreMsg.Code = SELECTUP then begin
  141. if PointCount > 0 then
  142. Leave := CheckForExit
  143. else
  144. ClearIt;
  145. if (not Leave) and (not OutOfBounds) then
  146. AddPoint;
  147. end;
  148. IDCMP_CLOSEWINDOW : CleanUpAndDie;
  149. end;
  150. until Leave or (PointCount > 50);
  151. if not Leave then
  152. DrawLine;
  153. dummy := ModifyIDCMP(w, IDCMP_CLOSEWINDOW);
  154. SetDrMd(rp, JAM1);
  155. SetAPen(rp, 1);
  156. end;
  157. Procedure Elevate;
  158. var
  159. t, tprime,
  160. RealPoints : Real;
  161. i : Integer;
  162. begin
  163. Inc(PointCount);
  164. RealPoints := Real(PointCount);
  165. Points[PointCount] := Points[Pred(PointCount)];
  166. for i := Pred(PointCount) downto 2 do
  167. with Points[i] do begin
  168. t := Real(i) / RealPoints;
  169. tprime := 1.0 - t;
  170. X := t * Points[Pred(i)].X + tprime * X;
  171. Y := t * Points[Pred(i)].Y + tprime * Y;
  172. end;
  173. end;
  174. Procedure DrawCurve;
  175. var
  176. i : Integer;
  177. begin
  178. GfxMove(rp, Trunc(Points[1].X), Trunc(Points[1].Y));
  179. for i := 2 to PointCount do
  180. Draw(rp, Round(Points[i].X), Round(Points[i].Y));
  181. end;
  182. Procedure DrawBezier;
  183. begin
  184. SetAPen(rp, 2);
  185. while PointCount < 100 do begin
  186. Elevate;
  187. DrawCurve;
  188. if GetMsg(w^.UserPort) <> Nil then
  189. CleanUpAndDie;
  190. end;
  191. SetAPen(rp, 1);
  192. DrawCurve;
  193. end;
  194. begin
  195. s := OpenScreenTags(nil,[
  196. AsTag(SA_Pens), AsTag(@pens),
  197. AsTag(SA_Depth), 2,
  198. AsTag(SA_DisplayID), HIRES_KEY,
  199. AsTag(SA_Title), AsTag('Simple Bezier Curves'),
  200. TAG_END]);
  201. if s = NIL then CleanUpAndDie;
  202. w := OpenWindowTags(nil,[
  203. WA_IDCMP, IDCMP_CLOSEWINDOW,
  204. WA_Left, 0,
  205. WA_Top, s^.BarHeight +1,
  206. WA_Width, s^.Width,
  207. WA_Height, s^.Height - (s^.BarHeight + 1),
  208. WA_DepthGadget, AsTag(True),
  209. WA_DragBar, AsTag(True),
  210. WA_CloseGadget, AsTag(True),
  211. WA_ReportMouse, AsTag(True),
  212. WA_SmartRefresh, AsTag(True),
  213. WA_Activate, AsTag(True),
  214. WA_Title, AsTag('Close the Window to Quit'),
  215. WA_CustomScreen, AsTag(s),
  216. TAG_END]);
  217. IF w = NIL THEN CleanUpAndDie;
  218. rp := w^.RPort;
  219. GfxMove(rp, 252, 30);
  220. GfxText(rp, 'Enter points by pressing the left mouse button', 46);
  221. GfxMove(rp, 252, 40);
  222. GfxText(rp, 'Double click on the last point to begin drawing', 47);
  223. repeat
  224. GetPoints; { Both these routines will quit if }
  225. DrawBezier; { the window is closed. }
  226. until False;
  227. CleanUpAndDie;
  228. end.