|
Mandelbrota tīkla kods
Ja Jūs interesē Mandelbrota tīkla uzbūve no programmēšanaas
viedokļa, tad piedāvāju mazu uz Quick BASIC 4.5 rakstītu programiņu, kas
ģenerē uz ekrāna Mandelbrota tīkla attēlu - es pievienoju iespēju pārvietoties
ar
kursortautiņiem uz augšu/leju, pa labi/kreisi un ar +/- iet iekšā kādā
Mandelbrota tīkla fragmentā. Ar C var
mainīt krāsu paleti un ar D var izvēlēties distanci ar kādu notiek navigācijas
pa Mandelbrota tīklu. Iziet ar ESC.
Lai izmēģinātu šo programmu jums ir jānokopē zemāk redzamais teksts un
jāsaglabā kā *.bas fails - pēc
tam to var atvērt jebkurā BASIC valodas izstrādes programmā un startēt!
DEFINT A-Z
' Default variable type is integer
DECLARE SUB ShiftPalette ()
' Set maximum number of iterations
per point:
CONST MAXLOOP = 30, MAXSIZE = 20000000: Sk = 100
DIM PaletteArray(15)
FOR I = 0 TO 15
PaletteArray(I) = I
NEXT I
' Define viewport:
SCREEN 8, 1
ColorRange = 15
' 16 colors (0 - 15)
VBottom = 179
VTop = 5
VRight = 400
VLeft = 150
VIEW (VLeft, VTop)-(VRight,
VBottom), 5, ColorRange
' Define corresponding window:
WLeft = -1000
WRight = 250
WTop = 625
WBottom = -625
WINDOW (WLeft, WTop)-(WRight, WBottom)
XLength = VRight
- VLeft
YLength = VBottom - VTop
ColorWidth = MAXLOOP \ ColorRange
' Loop through each pixel in viewport
and calculate
' whether or not it is in the Mandelbrot Set:
DO
N = N + 1
LOCATE 1, 1: PRINT "Cikls="; N
LOCATE 2, 1: PRINT "Pozicija:"
LOCATE 3, 1: PRINT "---------"
LOCATE 4, 1: PRINT "Pa kreisi ="; WLeft
LOCATE 5, 1: PRINT "Pa labi ="; WRight
LOCATE 6, 1: PRINT "Uz augsu ="; WTop
LOCATE 7, 1: PRINT "Uz leju ="; WBottom
FOR Y = 0 TO YLength
' Loop through every line in
' the viewport.
LogicY = PMAP(Y, 3) ' Get the pixel's
logical y
' coordinate.
PSET (WLeft, LogicY) ' Plot leftmost pixel
in the line.
OldColor = 0
' Start with background color.
FOR X = 0 TO XLength
' Loop through every pixel in
' the line.
LogicX = PMAP(X, 2) ' Get the pixel's
logical x
' coordinate .
MandelX& = LogicX
MandelY& = LogicY
' Do
the calculations to see if this point is in
' the Mandelbrot Set:
FOR I = 1 TO MAXLOOP
RealNum& = MandelX&
* MandelX&
ImagNum& = MandelY&
* MandelY&
IF (RealNum& + ImagNum&)
>= MAXSIZE THEN EXIT FOR
MandelY& = (MandelX&
* MandelY&) \ 250 + LogicY 'Sitos ja maina
MandelX& = (RealNum&
- ImagNum&) \ 500 + LogicX 'sanak kemi
NEXT I
' Assign
a color to the point:
PColor = I \ ColorWidth
' If
color has changed, draw a line from the
' last point referenced to the new point,
' using the old color:
IF PColor <> OldColor THEN
LINE -(LogicX, LogicY),
(ColorRange - OldColor)
OldColor = PColor
END IF
NEXT X
' Draw the last line
segment to the right edge of
' the viewport:
LINE -(LogicX, LogicY), (ColorRange - OldColor)
NEXT Y
Izvele:
DO: Kbd$ = UCASE$(INKEY$): LOOP UNTIL Kbd$ <> ""
SELECT CASE Kbd$
' <Up> & <Down>
CASE CHR$(0) + "P"
WTop
= WTop - Sk
WBottom
= WBottom - Sk
CASE CHR$(0) + "H"
WTop
= WTop + Sk
WBottom
= WBottom + Sk
' <Left> & <Right>
CASE CHR$(0) + CHR$(77)
WLeft
= WLeft + Sk
WRight
= WRight + Sk
CASE CHR$(0) + CHR$(75)
WLeft
= WLeft - Sk
WRight
= WRight - Sk
CASE "C"
ShiftPalette
CASE "N"
CLS
CASE "D"
LOCATE
8, 1: INPUT "Distance =", Sk: GOSUB Izvele
CASE "-"
WLeft
= WLeft - Sk
WRight
= WRight + Sk
WTop
= WTop + Sk
WBottom
= WBottom - Sk
CASE "+"
WLeft
= WLeft + Sk
WRight
= WRight - Sk
WTop
= WTop - Sk
WBottom
= WBottom + Sk
IF
WRight <= WLeft THEN
PRINT
"Kluda!!!"
END
IF
IF
WBottom >= WTop THEN
PRINT
"Kluda!!!"
END
IF
CASE CHR$(27)
GOSUB
Izeja
CASE ELSE
GOSUB
Izvele
END SELECT
WINDOW (WLeft, WTop)-(WRight, WBottom)
LOOP
Izeja:
SCREEN 0, 0
END
SUB ShiftPalette STATIC
SHARED PaletteArray(),
ColorRange
FOR I = 1 TO ColorRange
PaletteArray(I) = (PaletteArray(I) MOD
ColorRange) + 1
NEXT I
PALETTE USING PaletteArray(0)
END SUB
|