'=========================================================================== ' Subject: CREATE, EDIT & ANIMATE POLYGONS Date: 06-23-99 (09:41) ' Author: Matt Gulden Code: QB, QBasic, PDS ' Origin: www.karland.com/code/basic/ Packet: GRAPHICS.ABC '=========================================================================== ' POLYGON.BAS by Matt Gulden (aka Folter) -- webmaster@thrillhaus.com ' This file originated at www.thrillhaus.com ' A demonstration of how to create and edit, and animate polygons... ' Works in any screen mode, just modify MaxX, and MaxY. TYPE PolyType X AS INTEGER Y AS INTEGER XINC AS INTEGER YINC AS INTEGER END TYPE DEFINT A-Z DECLARE SUB AnimatePoly () DECLARE SUB GeneratePoly () DECLARE SUB DrawPoly () DECLARE SUB ErasePoly () CONST Points = 10 ' How many points in the polygon CONST Colour = 15 ' Color of polygon CONST MaxX = 319, MaxY = 199 ' Greatest screen coordinates DIM SHARED Polygon(Points) AS PolyType RANDOMIZE TIMER ' Use the timer as a seed for random number generator SCREEN 13 GeneratePoly ' Generates all points AnimatePoly ' Example of animating polygon END SUB AnimatePoly WHILE INKEY$ = "" FOR NUM = 0 TO Points - 1 Polygon(NUM).X = Polygon(NUM).X + Polygon(NUM).XINC Polygon(NUM).Y = Polygon(NUM).Y + Polygon(NUM).YINC IF Polygon(NUM).X > MaxX THEN Polygon(NUM).XINC = -Polygon(NUM).XINC IF Polygon(NUM).X < 0 THEN Polygon(NUM).XINC = -Polygon(NUM).XINC IF Polygon(NUM).Y > MaxY THEN Polygon(NUM).YINC = -Polygon(NUM).YINC IF Polygon(NUM).Y < 0 THEN Polygon(NUM).YINC = -Polygon(NUM).YINC NEXT DrawPoly ' Draws polygon on screen WAIT &H3DA, 8 ' WAIT for vertical retrace, lessens flicker ErasePoly ' Draws poly in black WEND END SUB SUB DrawPoly FOR NUM = 0 TO Points - 1 IF NUM = 0 THEN X2 = Polygon(Points - 1).X Y2 = Polygon(Points - 1).Y ELSE X2 = Polygon(NUM - 1).X Y2 = Polygon(NUM - 1).Y END IF LINE (Polygon(NUM).X, Polygon(NUM).Y)-(X2, Y2), Colour NEXT END SUB SUB ErasePoly FOR NUM = 0 TO Points - 1 IF NUM = 0 THEN X2 = Polygon(Points - 1).X Y2 = Polygon(Points - 1).Y ELSE X2 = Polygon(NUM - 1).X Y2 = Polygon(NUM - 1).Y END IF LINE (Polygon(NUM).X, Polygon(NUM).Y)-(X2, Y2), 0 NEXT END SUB SUB GeneratePoly FOR NUM = 0 TO Points - 1 ' Set random location Polygon(NUM).X = INT(RND * (MaxX + 1)) Polygon(NUM).Y = INT(RND * (MaxY + 1)) ' Set random speed for points Polygon(NUM).XINC = INT(RND * 9) - 5 Polygon(NUM).YINC = INT(RND * 9) - 5 ' Make sure every point is moving IF Polygon(NUM).XINC = 0 THEN Polygon(NUM).XINC = 1 IF Polygon(NUM).YINC = 0 THEN Polygon(NUM).YINC = 1 NEXT END SUB