home *** CD-ROM | disk | FTP | other *** search
/ The Mother of All Windows Books / CD-MOM.iso / cd_mom / newsletr / vbz / vbz1-3 / spyro2.bas < prev    next >
BASIC Source File  |  1993-06-19  |  6KB  |  221 lines

  1.     DefDbl A-Z
  2.     Dim Ratio, TwoPi, ScrHeight, ScrWidth, CLSFlag%
  3.     Dim Largest, Smallest, ErasePoly, NomSize
  4.  
  5.  
  6. Global Const AppName$ = "Spyrograph"
  7.  
  8. Sub CleanUp ()
  9.     For Z = frmSave!Picture1.Left To -15 Step -180
  10.         If Z > 180 Then
  11.             frmSave!Picture1.Left = Z
  12.         Else
  13.             frmSave!Picture1.Left = -15
  14.         End If
  15.     Next
  16.     Unload frmSave
  17.     End
  18. End Sub
  19.  
  20. Sub DrawPoly (Order, h, v, Size)
  21.  
  22.     k2 = TwoPi / Order
  23.     ReDim IAry(0 To Order - 1)
  24.     Shuffle IAry()
  25.     For p = 0 To Order - 1
  26.         i = IAry(p)
  27.         k3 = k2 * i
  28.         X = (Size * Sin(k3) * Ratio) + h
  29.         y = (Size * Cos(k3)) + v
  30.         '
  31.         ' Draw the vertex connections alternately
  32.         ' clockwise and counterclockwise, on a
  33.         ' random basis.
  34.         '
  35.         If Rnd > .5 Then    ' about half the time
  36.             For j = i + 1 To Order - 1
  37.                 GoSub InnerLoop
  38.             Next j
  39.         Else
  40.             For j = Order - 1 To i + 1 Step -1
  41.                 GoSub InnerLoop
  42.             Next j
  43.         End If
  44.     Next
  45.     Exit Sub
  46.     
  47. InnerLoop:
  48.     k3 = k2 * j
  49.     x1 = (Size * Sin(k3)) * Ratio + h
  50.     y1 = (Size * Cos(k3)) + v
  51.     frmSave.Line (X, y)-(x1, y1)
  52.     Z = DoEvents()
  53.     Return
  54.  
  55. End Sub
  56.  
  57. Sub InitSave ()
  58.     ScreenCapture  'captures the screen into the saver form
  59.     frmSave!Picture1.Picture = frmSave.Image
  60.     frmSave!Picture1.Top = -15
  61.     Select Case Screen.Width \ Screen.TwipsPerPixelX
  62.         Case Is > 800
  63.             frmSave.Picture = frmSave!picSSVGA.Picture
  64.         Case Is > 640
  65.             frmSave.Picture = frmSave!picSVGA.Picture
  66.         Case Else
  67.             frmSave.Picture = frmSave!picVGA.Picture
  68.     End Select
  69.     frmSave.Show
  70.     frmSave!timSave.Enabled = True
  71.     ScrHeight = frmSave.Height
  72.     ScrWidth = frmSave.Width
  73.     Ratio = 1  ' Aspect ratio, =1 for "square" pixels
  74.     ReScale    ' choose appropriate sizes
  75. End Sub
  76.  
  77. Sub Outer ()
  78. '
  79. ' This is where the action takes place.
  80. '
  81.  
  82.     Randomize
  83.     TwoPi = 6.283185317
  84.     OnScreen% = 0       ' number currently on screen
  85.     Do While DoEvents()
  86.         ' Select a polygon "order" based on the current
  87.         ' lowest and highest allowable values.  See
  88.         ' "ReScale" for how Lowest and Highest are set
  89.         '
  90.         Order = Int(Rnd * (Largest - Smallest + 1)) + Smallest
  91.         '
  92.         ' Choose a random color some distance from the
  93.         ' current color
  94.         '
  95. ReColor:
  96.         Do
  97.             Colr% = Int(Rnd * 16)
  98.         Loop While Abs(Colr% - PrevColr%) < 6
  99.         '
  100.         ' Don't allow BackColor unless the screen
  101.         ' has at least 10 polygons already on it
  102.         '
  103.         If QBColor(Colr%) = BackColor Then
  104.             If OnScreen% < 10 Then
  105.                 GoTo ReColor
  106.             End If
  107.         End If
  108.         PrevColr% = Colr%
  109.         frmSave.ForeColor = QBColor(Colr%)
  110.         '
  111.         ' Every so often (maybe 10% of the time)
  112.         ' erase the images by drawing a special
  113.         ' polygon (of order "ErasePoly") in Black
  114.         '
  115.         If Rnd > .9 Then
  116.             '
  117.             ' But only do that if there are at least
  118.             ' 10 polygons already drawn.  Otherwise
  119.             ' just draw a new polygon over the last.
  120.             '
  121.             If OnScreen% > 10 Then
  122.                 frmSave.ForeColor = 0
  123.                 Order = ErasePoly
  124.                 Bigger = ScrWidth
  125.                 If ScrHeight > Bigger Then
  126.                     Bigger = ScrHeight
  127.                 End If
  128.                 Size = Bigger / (1.42 * Ratio)
  129.                 h = ScrWidth / 2
  130.                 v = ScrHeight / 2
  131.                 OnScreen% = 0
  132.             End If
  133.         ElseIf Rnd > .07 Then
  134.             '
  135.             ' Most of the time (roughly 93%), choose
  136.             ' a new size and position for the newest
  137.             ' polygon.
  138.             '
  139.             Size = NomSize / (((Rnd * 2.5) + 2) / 1.2)
  140.             h = (Rnd * ScrWidth)        ' keep the center
  141.             v = (Rnd * ScrHeight)       '   on the screen
  142.             OnScreen% = OnScreen% + 1   ' count this one
  143.         End If
  144.         '
  145.         ' Now draw the polygon
  146.         '
  147.         Call DrawPoly(Order, h, v, Size)
  148.         '
  149.         ' If this was an ErasePoly, clear its rubble
  150.         '
  151.         If OnScreen% = 0 Then
  152.             frmSave.Cls
  153.             Temp& = Timer 'put a little pause in there
  154.             While DoEvents() And ((Timer - Temp&) < 3)
  155.             Wend
  156.         End If
  157.     Loop
  158. End Sub
  159.  
  160. Sub ReScale ()
  161.     NomSize = Sqr(ScrWidth * ScrHeight)
  162.     If frmSave.WindowState = 1 Then 'if minimized, restrict range
  163.         Smallest = 3
  164.         Largest = 15
  165.         ErasePoly = 30
  166.     Else
  167.         tmp = ScrWidth
  168.         If ScrHeight < NomSize Then
  169.             tmp = ScrHeight
  170.         End If
  171.         tmp = CInt(tmp / 900)
  172.         If tmp < 5 Then
  173.             tmp = 5
  174.         End If
  175.         Largest = tmp * 6
  176.         Smallest = 3
  177.         ErasePoly = CInt(Sqr(NomSize)) And Not 1
  178.     End If
  179. End Sub
  180.  
  181. Sub Shuffle (IAry())
  182.  
  183. ' Randomize the order in which the vertices are accessed.
  184. ' IAry() is an array containing vertex numbers.
  185.  
  186.     k% = UBound(IAry)
  187.     '
  188.     ' Self-fill the array: I(9)=9, e.g.
  189.     '
  190.     For m% = 0 To k%
  191.         IAry(m%) = m%
  192.     Next
  193.     '
  194.     ' Most of the time (about 90%), randomize the order
  195.     ' in which the points will be accessed, but once
  196.     ' in a while, let it happen in order.
  197.     '
  198.     If Rnd > .1 Then
  199.         LastJ% = -1
  200.         For m% = 0 To k%
  201.             Do
  202.                 j% = Int(Rnd * (k% + 1))
  203.             Loop Until (j% <> m%) And (j% <> LastJ%)
  204.             LastJ% = j%
  205.             tmp = IAry(j%)
  206.             IAry(j%) = IAry(m%)
  207.             IAry(m%) = tmp
  208.         Next
  209.     End If
  210. End Sub
  211.  
  212. Sub TimerProc ()
  213.     TimeToGo = False
  214.     frmSave.Picture1.Left = frmSave.Picture1.Left + 30
  215.     If frmSave.Picture1.Left >= frmSave.ScaleWidth Then
  216.         frmSave!timSave.Enabled = False
  217.         Outer
  218.     End If
  219. End Sub
  220.  
  221.