bezier.pas 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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,
  107. BorderRight, BorderBottom);
  108. SetDrMd(rp, COMPLEMENT);
  109. SetAPen(rp, 3);
  110. end;
  111. begin
  112. dummy := ModifyIDCMP(w, IDCMP_CLOSEWINDOW or IDCMP_MOUSEBUTTONS or
  113. IDCMP_MOUSEMOVE);
  114. SetDrMd(rp, COMPLEMENT);
  115. PointCount := 0;
  116. Leave := False;
  117. OutOfBounds := False;
  118. BorderLeft := w^.BorderLeft;
  119. BorderRight := (w^.Width - w^.BorderRight) -1;
  120. BorderTop := w^.BorderTop;
  121. BorderBottom := (w^.Height - w^.BorderBottom) -1;
  122. repeat
  123. IM := pIntuiMessage(WaitPort(w^.UserPort));
  124. IM := pIntuiMessage(GetMsg(w^.UserPort));
  125. StoreMsg := IM^;
  126. ReplyMsg(pMessage(IM));
  127. case StoreMsg.IClass of
  128. IDCMP_MOUSEMOVE : if PointCount > 0 then begin
  129. if not OutOfBounds then
  130. DrawLine;
  131. LastX := StoreMsg.MouseX;
  132. LastY := StoreMsg.MouseY;
  133. if (LastX > BorderLeft) and
  134. (LastX < BorderRight) and
  135. (LastY > BorderTop) and
  136. (LastY < BorderBottom) then begin
  137. DrawLine;
  138. OutOfBounds := False;
  139. end else
  140. OutOfBounds := True;
  141. end;
  142. IDCMP_MOUSEBUTTONS : if StoreMsg.Code = SELECTUP then begin
  143. if PointCount > 0 then
  144. Leave := CheckForExit
  145. else
  146. ClearIt;
  147. if (not Leave) and (not OutOfBounds) then
  148. AddPoint;
  149. end;
  150. IDCMP_CLOSEWINDOW : CleanUpAndDie;
  151. end;
  152. until Leave or (PointCount > 50);
  153. if not Leave then
  154. DrawLine;
  155. dummy := ModifyIDCMP(w, IDCMP_CLOSEWINDOW);
  156. SetDrMd(rp, JAM1);
  157. SetAPen(rp, 1);
  158. end;
  159. Procedure Elevate;
  160. var
  161. t, tprime,
  162. RealPoints : Real;
  163. i : Integer;
  164. begin
  165. Inc(PointCount);
  166. RealPoints := Real(PointCount);
  167. Points[PointCount] := Points[Pred(PointCount)];
  168. for i := Pred(PointCount) downto 2 do
  169. with Points[i] do begin
  170. t := Real(i) / RealPoints;
  171. tprime := 1.0 - t;
  172. X := t * Points[Pred(i)].X + tprime * X;
  173. Y := t * Points[Pred(i)].Y + tprime * Y;
  174. end;
  175. end;
  176. Procedure DrawCurve;
  177. var
  178. i : Integer;
  179. begin
  180. GfxMove(rp, Trunc(Points[1].X), Trunc(Points[1].Y));
  181. for i := 2 to PointCount do
  182. Draw(rp, Round(Points[i].X), Round(Points[i].Y));
  183. end;
  184. Procedure DrawBezier;
  185. begin
  186. SetAPen(rp, 2);
  187. while PointCount < 100 do begin
  188. Elevate;
  189. DrawCurve;
  190. if GetMsg(w^.UserPort) <> Nil then
  191. CleanUpAndDie;
  192. end;
  193. SetAPen(rp, 1);
  194. DrawCurve;
  195. end;
  196. begin
  197. s := OpenScreenTags(nil,[
  198. AsTag(SA_Pens), AsTag(@pens),
  199. AsTag(SA_Depth), 2,
  200. AsTag(SA_DisplayID), HIRES_KEY,
  201. AsTag(SA_Title), AsTag('Simple Bezier Curves'),
  202. TAG_END]);
  203. if s = NIL then CleanUpAndDie;
  204. w := OpenWindowTags(nil,[
  205. WA_IDCMP, IDCMP_CLOSEWINDOW,
  206. WA_Left, 0,
  207. WA_Top, s^.BarHeight +1,
  208. WA_Width, s^.Width,
  209. WA_Height, s^.Height - (s^.BarHeight + 1),
  210. WA_DepthGadget, ltrue,
  211. WA_DragBar, ltrue,
  212. WA_CloseGadget, ltrue,
  213. WA_ReportMouse, ltrue,
  214. WA_SmartRefresh, ltrue,
  215. WA_Activate, ltrue,
  216. WA_Title, AsTag('Close the Window to Quit'),
  217. WA_CustomScreen, AsTag(s),
  218. TAG_END]);
  219. IF w=NIL THEN CleanUpAndDie;
  220. rp := w^.RPort;
  221. GfxMove(rp, 252, 30);
  222. GfxText(rp, 'Enter points by pressing the left mouse button', 46);
  223. GfxMove(rp, 252, 40);
  224. GfxText(rp, 'Double click on the last point to begin drawing', 47);
  225. repeat
  226. GetPoints; { Both these routines will quit if }
  227. DrawBezier; { the window is closed. }
  228. until False;
  229. CleanUpAndDie;
  230. end.