home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / basic / mandel.bas < prev    next >
Encoding:
BASIC Source File  |  1989-11-09  |  5.4 KB  |  179 lines

  1. DEFINT A-Z       ' Default variable type is integer.
  2.  
  3. DECLARE    SUB ShiftPalette ()
  4. DECLARE    SUB WindowVals (WL%, WR%, WT%, WB%)
  5. DECLARE    SUB ScreenTest (EM%, CR%, VL%, VR%, VT%, VB%)
  6.  
  7. CONST FALSE = 0, TRUE = NOT FALSE ' Boolean constants
  8.  
  9. ' Set maximum number of iterations per point:
  10. CONST MAXLOOP =    30, MAXSIZE = 1000000
  11.  
  12. DIM PaletteArray(15)
  13. FOR I =    0 TO 15: PaletteArray(I) = I: NEXT I
  14.  
  15. ' Call WindowVals to get coordinates of window corners:
  16. WindowVals WLeft, WRight, WTop,    WBottom
  17.  
  18. ' Call ScreenTest to find out if this is an EGA machine
  19. ' and get coordinates of viewport corners:
  20. ScreenTest EgaMode, ColorRange,    VLeft, VRight, VTop, VBottom
  21.  
  22. ' Define viewport and corresponding window:
  23. VIEW (VLeft, VTop)-(VRight, VBottom), 0, ColorRange
  24. WINDOW (WLeft, WTop)-(WRight, WBottom)
  25.  
  26. LOCATE 24, 10 : PRINT "Press any key to quit.";
  27.  
  28. XLength    = VRight - VLeft
  29. YLength    = VBottom - VTop
  30. ColorWidth = MAXLOOP \ ColorRange
  31.  
  32. ' Loop through each pixel in viewport and calculate
  33. ' whether or not it is in the Mandelbrot Set:
  34. FOR Y =    0 TO YLength       ' Loop through every line
  35.                ' in the viewport.
  36.    LogicY = PMAP(Y, 3)       ' Get the pixel's view
  37.                ' y-coordinate.
  38.    PSET    (WLeft,    LogicY)       ' Plot leftmost pixel in the line.
  39.    OldColor = 0           ' Start with background color.
  40.  
  41.    FOR X = 0 TO    XLength       ' Loop through every pixel
  42.                ' in the line.
  43.       LogicX = PMAP(X, 2)  ' Get the pixel's view
  44.                ' x-coordinate.
  45.       MandelX& = LogicX
  46.       MandelY& = LogicY
  47.       ' Do the calculations to see if this point
  48.       ' is in the Mandelbrot Set:
  49.       FOR I = 1    TO MAXLOOP
  50.          RealNum& = MandelX& * MandelX&
  51.          ImagNum& = MandelY& * MandelY&
  52.          IF (RealNum& + ImagNum&) >= MAXSIZE THEN EXIT FOR
  53.          MandelY& = (MandelX& * MandelY&) \ 250 + LogicY
  54.          MandelX& = (RealNum& - ImagNum&) \ 500 + LogicX
  55.       NEXT I
  56.  
  57.       '    Assign a color to the point:
  58.       PColor = I \ ColorWidth
  59.  
  60.       '    If color has changed, draw a line from
  61.       ' the last point referenced to the new point,
  62.       '    using the old color:
  63.       IF PColor    <> OldColor THEN
  64.      LINE -(LogicX, LogicY), (ColorRange - OldColor)
  65.      OldColor = PColor
  66.       END IF
  67.  
  68.       IF INKEY$    <> "" THEN END
  69.    NEXT    X
  70.  
  71.    ' Draw the last line    segment    to the right edge
  72.    ' of the viewport:
  73.    LINE    -(LogicX, LogicY), (ColorRange - OldColor)
  74.  
  75.    ' If    this is    an EGA machine,    shift the palette after
  76.    ' drawing each line:
  77.    IF EgaMode THEN ShiftPalette
  78. NEXT Y
  79.  
  80. DO
  81.    ' Continue shifting the palette
  82.    ' until the user presses a key:
  83.    IF EgaMode THEN ShiftPalette
  84. LOOP WHILE INKEY$ = ""
  85.  
  86. SCREEN 0, 0        ' Restore the screen to text mode,
  87. WIDTH 80        ' 80 columns.
  88. END
  89.  
  90. BadScreen:        ' Error handler that is invoked if
  91.    EgaMode = FALSE    ' there is no EGA graphics card
  92.    RESUME NEXT
  93. ' ====================== ShiftPalette =====================
  94. '    Rotates the palette by one each time it is called
  95. ' =========================================================
  96.  
  97. SUB ShiftPalette STATIC
  98.    SHARED PaletteArray(), ColorRange
  99.  
  100.    FOR I = 1 TO    ColorRange
  101.       PaletteArray(I) =    (PaletteArray(I) MOD ColorRange) + 1
  102.    NEXT    I
  103.    PALETTE USING PaletteArray(0)
  104.  
  105. END SUB
  106. ' ======================= ScreenTest ======================
  107. '    Uses a SCREEN 8 statement as a test to see if user has
  108. '    EGA hardware. If this causes an error, the EM flag is
  109. '    set to FALSE, and the screen is set with SCREEN 1.
  110.  
  111. '    Also sets values for corners of viewport (VL = left,
  112. '    VR = right, VT = top, VB = bottom), scaled with the
  113. '    correct aspect ratio so viewport is a perfect square.
  114. ' =========================================================
  115.  
  116. SUB ScreenTest (EM, CR,    VL, VR,    VT, VB) STATIC
  117.    EM =    TRUE
  118.    ON ERROR GOTO BadScreen
  119.    SCREEN 8, 1
  120.    ON ERROR GOTO 0
  121.  
  122.    IF EM THEN            ' No error, SCREEN 8 is OK.
  123.       VL = 110: VR = 529
  124.       VT = 5: VB = 179
  125.       CR = 15            ' 16 colors (0 - 15)
  126.  
  127.    ELSE                ' Error, so use SCREEN 1.
  128.       SCREEN 1,    1
  129.       VL = 55: VR = 264
  130.       VT = 5: VB = 179
  131.       CR = 3            ' 4 colors (0 - 3)
  132.    END IF
  133.  
  134. END SUB
  135. ' ======================= WindowVals ======================
  136. '     Gets window corners as input from the user, or sets
  137. '     values for the corners if there is no input
  138. ' =========================================================
  139.  
  140. SUB WindowVals (WL, WR,    WT, WB)    STATIC
  141.    CLS
  142.    PRINT "This program prints the graphic representation of"
  143.    PRINT "the complete Mandelbrot Set. The default window"
  144.    PRINT "is from (-1000,625) to (250,-625). To zoom in on"
  145.    PRINT "part of the figure, input coordinates inside"
  146.    PRINT "this window."
  147.    PRINT "Press <ENTER> to see the default window or"
  148.    PRINT "any other key to input window coordinates: ";
  149.    LOCATE , , 1
  150.    Resp$ = INPUT$(1)
  151.  
  152.    ' User didn't press ENTER, so input window corners:
  153.    IF Resp$ <> CHR$(13)    THEN
  154.       PRINT
  155.       INPUT "x-coordinate of upper-left corner: ", WL
  156.       DO
  157.      INPUT "x-coordinate of lower-right corner: ", WR
  158.      IF WR <= WL THEN
  159.         PRINT "Right corner must be greater than left corner."
  160.      END IF
  161.       LOOP WHILE WR <= WL
  162.       INPUT "y-coordinate of upper-left corner: ", WT
  163.       DO
  164.      INPUT "y-coordinate of lower-right corner: ", WB
  165.      IF WB >= WT THEN
  166.         PRINT "Bottom corner must be less than top corner."
  167.      END IF
  168.       LOOP WHILE WB >= WT
  169.  
  170.    ' User pressed ENTER, so set default values:
  171.    ELSE
  172.       WL = -1000
  173.       WR = 250
  174.       WT = 625
  175.       WB = -625
  176.    END IF
  177. END SUB
  178.  
  179.