'=========================================================================== ' Subject: BEZIER CURVE DRAWING ROUTINE Date: 02-23-98 (08:40) ' Author: Petter Holmberg Code: QB, QBasic, PDS ' Origin: petter.holmberg@usa.net Packet: GRAPHICS.ABC '=========================================================================== ' B‚zier curve drawing routine by Petter Holmberg, -98: DECLARE SUB DrawBezier (x1!, y1!, x2!, y2!, c1x!, c1y!, c2x!, c2y!, col%) RANDOMIZE TIMER CLS SCREEN 0 WIDTH 80, 25 PRINT "B‚zier curve drawing routine by Petter Holmberg, -98:" PRINT PRINT "A B‚zier curve is a mathematical curve based on an iteration of two third-degree" PRINT "equations, and it can be used to draw almost any type of simple curves." PRINT "The B‚zier curve was invented in the early seventies by Pierre B‚zier, a french" PRINT "engineer and mathematician, to be used in CAD/CAM applications. Nowadays, a" PRINT "B‚zier curve drawing function exists in most sophisticated drawing programs." PRINT PRINT "A B‚zier curve is defined by four points; the endpoints and two control points." PRINT "By setting these points to different locations you can draw curves, straight" PRINT "lines or even loops." PRINT PRINT "This demo demonstrates the possibilities of the B‚zier equation." PRINT "If you are making a painting program and want to include B‚zier curves, feel" PRINT "free to use this routine. All I demand is that you give me the credits." PRINT "If you use it in a program of yours, I would be glad if you sent me a mail and" PRINT "told me about it. my email address is: petter.holmberg@usa.net" PRINT PRINT "Press any key to continue this demonstration..." PRINT PRINT "- This routine was based on information at:" PRINT " http://www.moshplant.com/direct-or/bezier/math.html" DO: LOOP WHILE INKEY$ = "" SCREEN 13 LOCATE 1, 1 PRINT "A simple B‚zier curve:" COLOR 12 PRINT "Red : endpoints" COLOR 14 PRINT "Yellow : control points" COLOR 15 PRINT "Press any key to draw the curve..." x1! = 100 y1! = 150 x2! = 200 y2! = 100 c1x! = 130 c1y! = 100 c2x! = 170 c2y! = 80 PSET (x1!, y1!), 12 PSET (c1x!, c1y!), 14 PSET (c2x!, c2y!), 14 PSET (x2!, y2!), 12 DO: LOOP WHILE INKEY$ = "" DrawBezier x1!, y1!, x2!, y2!, c1x!, c1y!, c2x!, c2y!, 15 PSET (x1!, y1!), 12 PSET (x2!, y2!), 12 DO: LOOP WHILE INKEY$ = "" CLS LOCATE 1, 1 PRINT "With the B‚zier formula you can draw..." DO: LOOP WHILE INKEY$ = "" PRINT PRINT "Lines..." x1! = 100 y1! = 20 x2! = 200 y2! = 20 c1x! = 130 c1y! = 20 c2x! = 170 c2y! = 20 DrawBezier x1!, y1!, x2!, y2!, c1x!, c1y!, c2x!, c2y!, 15 PSET (x1!, y1!), 12 PSET (c1x!, c1y!), 14 PSET (c2x!, c2y!), 14 PSET (x2!, y2!), 12 DO: LOOP WHILE INKEY$ = "" PRINT PRINT "Curves..." x1! = 100 y1! = 50 x2! = 200 y2! = 50 c1x! = 130 c1y! = 80 c2x! = 170 c2y! = 80 DrawBezier x1!, y1!, x2!, y2!, c1x!, c1y!, c2x!, c2y!, 15 PSET (x1!, y1!), 12 PSET (c1x!, c1y!), 14 PSET (c2x!, c2y!), 14 PSET (x2!, y2!), 12 x1! = 210 y1! = 50 x2! = 310 y2! = 80 c1x! = 240 c1y! = 100 c2x! = 280 c2y! = 30 DrawBezier x1!, y1!, x2!, y2!, c1x!, c1y!, c2x!, c2y!, 15 PSET (x1!, y1!), 12 PSET (c1x!, c1y!), 14 PSET (c2x!, c2y!), 14 PSET (x2!, y2!), 12 DO: LOOP WHILE INKEY$ = "" LOCATE 15 PRINT "And even loops..." x1! = 150 y1! = 150 x2! = 150 y2! = 130 c1x! = 200 c1y! = 110 c2x! = 200 c2y! = 170 DrawBezier x1!, y1!, x2!, y2!, c1x!, c1y!, c2x!, c2y!, 15 PSET (x1!, y1!), 12 PSET (c1x!, c1y!), 14 PSET (c2x!, c2y!), 14 PSET (x2!, y2!), 12 x1! = 250 y1! = 130 x2! = 220 y2! = 120 c1x! = 250 c1y! = 180 c2x! = 310 c2y! = 170 DrawBezier x1!, y1!, x2!, y2!, c1x!, c1y!, c2x!, c2y!, 15 PSET (x1!, y1!), 12 PSET (c1x!, c1y!), 14 PSET (c2x!, c2y!), 14 PSET (x2!, y2!), 12 DO: LOOP WHILE INKEY$ = "" CLS PRINT "Press any key for a random B‚zier curve." PRINT "You can quit at any time with ESC." DO DO key$ = INKEY$ LOOP WHILE key$ = "" LINE (0, 16)-(319, 199), 0, BF x1! = INT(RND * 320) y1! = INT(RND * 184) + 16 x2! = INT(RND * 320) y2! = INT(RND * 184) + 16 c1x! = INT(RND * 320) c1y! = INT(RND * 184) + 16 c2x! = INT(RND * 320) c2y! = INT(RND * 184) + 16 DrawBezier x1!, y1!, x2!, y2!, c1x!, c1y!, c2x!, c2y!, 15 PSET (x1!, y1!), 12 PSET (c1x!, c1y!), 14 PSET (x2!, y2!), 12 PSET (c2x!, c2y!), 14 LOOP UNTIL key$ = CHR$(27) CLS SUB DrawBezier (x1!, y1!, x2!, y2!, c1x!, c1y!, c2x!, c2y!, col%) x! = x1! y! = y1! cx! = 3 * (c1x! - x1!) bx! = 3 * (c2x! - c1x!) - cx! ax! = x2! - x1! - cx! - bx! cy! = 3 * (c1y! - y1!) by! = 3 * (c2y! - c1y!) - cy! ay! = y2! - y1! - cy! - by! PSET (x!, y!), col% FOR t! = 0 TO 1.05 STEP .05 x! = ((ax! * t! + bx!) * t! + cx!) * t! + x1! y! = ((ay! * t! + by!) * t! + cy!) * t! + y1! LINE -(x!, y!), col% NEXT t! END SUB