home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l196 / 3.ddi / MANDEL.BA$ / MANDEL.bin
Encoding:
Text File  |  1990-06-24  |  5.4 KB  |  178 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.  
  94. ' ====================== ShiftPalette =====================
  95. '    Rotates the palette by one each time it is called
  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.  
  107. ' ======================= ScreenTest ======================
  108. '    Uses a SCREEN 8 statement as a test to see if user has
  109. '    EGA hardware. If this causes an error, the EM flag is
  110. '    set to FALSE, and the screen is set with SCREEN 1.
  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. SUB ScreenTest (EM, CR,    VL, VR,    VT, VB) STATIC
  116.    EM =    TRUE
  117.    ON ERROR GOTO BadScreen
  118.    SCREEN 8, 1
  119.    ON ERROR GOTO 0
  120.  
  121.    IF EM THEN            ' No error, SCREEN 8 is OK.
  122.       VL = 110: VR = 529
  123.       VT = 5: VB = 179
  124.       CR = 15            ' 16 colors (0 - 15)
  125.  
  126.    ELSE                ' Error, so use SCREEN 1.
  127.       SCREEN 1,    1
  128.       VL = 55: VR = 264
  129.       VT = 5: VB = 179
  130.       CR = 3            ' 4 colors (0 - 3)
  131.    END IF
  132.  
  133. END SUB
  134.  
  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. SUB WindowVals (WL, WR,    WT, WB)    STATIC
  140.    CLS
  141.    PRINT "This program prints the graphic representation of"
  142.    PRINT "the complete Mandelbrot Set. The default window"
  143.    PRINT "is from (-1000,625) to (250,-625). To zoom in on"
  144.    PRINT "part of the figure, input coordinates inside"
  145.    PRINT "this window."
  146.    PRINT "Press <ENTER> to see the default window or"
  147.    PRINT "any other key to input window coordinates: ";
  148.    LOCATE , , 1
  149.    Resp$ = INPUT$(1)
  150.  
  151.    ' User didn't press ENTER, so input window corners:
  152.    IF Resp$ <> CHR$(13)    THEN
  153.       PRINT
  154.       INPUT "x-coordinate of upper-left corner: ", WL
  155.       DO
  156.      INPUT "x-coordinate of lower-right corner: ", WR
  157.      IF WR <= WL THEN
  158.         PRINT "Right corner must be greater than left corner."
  159.      END IF
  160.       LOOP WHILE WR <= WL
  161.       INPUT "y-coordinate of upper-left corner: ", WT
  162.       DO
  163.      INPUT "y-coordinate of lower-right corner: ", WB
  164.      IF WB >= WT THEN
  165.         PRINT "Bottom corner must be less than top corner."
  166.      END IF
  167.       LOOP WHILE WB >= WT
  168.  
  169.    ' User pressed ENTER, so set default values:
  170.    ELSE
  171.       WL = -1000
  172.       WR = 250
  173.       WT = 625
  174.       WB = -625
  175.    END IF
  176. END SUB
  177.  
  178.