%! % BEZIER CURVE THROUGH FOUR POINTS UTILITY % ============================================== % |1Copyright c 1999 by Don Lancaster and Synergetics, Box 809, Thatcher, AZ, 85552 % (520) 428-4073 don@tinaja.com http://www.tinaja.com % Consulting services available per http://www.tinaja.com/info01.html % All commercial rights and all electronic media rights fully reserved. % Personal use permitted provided header and entire file remains intact. % Linking is welcome. Reposting expressly forbidden. % This utility demo shows how to use PostScript to draw a smooth Bezier % curve through four points. It is based on the Jim Fitzsimmons tutorial % and derivation in BEZ4PTS.PS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % /bez4pts is a modified Jim Fitzsimmons utility. This version draws % a straight line from the currentpoint, then appends a Bezier curve % through the next four points, presented as an eight element matrix. % Format is [ix0 iy0 ix1 iy1 ix2 iy2 ix3 iy3 ] bez4pts /bez4pts {aload pop % unpack array /y3 exch store /x3 exch store % stash data. Strange /y5 exch store /x5 exch store % numbering is in use /y4 exch store /x4 exch store /y0 exch store /x0 exch store % Solve for x1. /x1 x0 dup add x3 add 3 div def % Solve for x2. /x2 x3 dup add x0 add 3 div def % Solve for u4. /u4 x4 dup add x0 sub x3 sub x3 x0 sub div def % Solve for u5. /u5 x5 dup add x0 sub x3 sub x3 x0 sub div def % Define u0 and u3. /u0 -1 def /u3 1 def % Define y04. /y04 y0 y4 sub u0 u4 sub div def % Define y45. /y45 y4 y5 sub u4 u5 sub div def % Define y53. /y53 y5 y3 sub u5 u3 sub div def % Define y05. /y05 y04 y45 sub u0 u5 sub div def % Define y43. /y43 y45 y53 sub u4 u3 sub div def % Solve for b3. /b3 y43 y05 sub 2 div def % Solve for b2. /b2 y05 u4 u5 add 1 sub b3 mul sub def % Solve for y1. /y1 y0 dup add y3 add b2 b3 sub dup add dup add sub 3 div def % Solve for y2. /y2 y3 dup add y0 add b2 b3 add dup add dup add sub 3 div def % Draw the cubic. x0 y0 lineto x1 y1 x2 y2 x3 y3 curveto } def %% % demo - comment out before use /mtdot {3 0 360 arc fill} store gsave 0 1 0 setrgbcolor % optionally show data points 100 100 mtdot 200 120 mtdot 300 190 mtdot 400 300 mtdot grestore 100 100 moveto [100 100 200 120 300 190 400 300] bez4pts 0.1 setlinewidth stroke showpage %eof