All set for Mandelbrot: a type-in on a fascinating frontier of maths, guided by RpM
In the age of the supercomputer whole new areas of problem-solving have been invented because they're possible. Complex equations involving hundreds of thousands of calculations have become manageable What would have taken hundreds of man-years to complete can take seconds on a powerful computer. Several mathematicians' theories have been explored in depth as a result of the computer. The Mandelbrot set - which stems from fractals is both obscure and spectacular. Much has been made of Benoit Mandelbrot and the set he discovered in 1974. Why? Look at the colourful eye-catching designs of ever-increasing complexity No use whatsoever - but breathtaking.
The Mandelbrot set is pictured as an area in two dimensions It's a collection of points defined by a simple repeated function You can see the complete Mandelbrot set in the picture the black region. The coloured bands represent the behaviour of points as they get close to the set.
It's the complexity of the shape that makes the Mandelbrot set so fascinating: the more you enlarge and examine minute areas of the set. the more complex it gets. Although the area is finite lies within the circle defined by x² + j² = 4 (which I'm not going to prove, you can get further details from Mandelbrot's book The Fractal Geometry of Nature) - the edge is infinitely complex and of infinite length. Mathematicians have a word for this: fractal. A few recent games use fractal graphics to build, say. a real-looking mountain from only an equation Eidolon and Koronis Rift are two Enough of the background information. Let's get down to the nitty-gritty of things. Here is a type-in that will produce the entire Mandelbrot set and the other areas seen on this page.
Alex Clark, the man from Lamlash in Strathclyde, is responsible for the listing capable of producing the spectacular Mandelbrot set. (And thanks to all the other readers who submined versions as a result of Owen Cunningham's letter in Reaction 21.) It has two ways of operation: automatic mode draws the complete Mandelbrot set and six other interesting locations from the set: manual mode lets you select and view an area of the set. In either case the final design is saved to disk or cassette. It is best to run the program and leave Arnold to do his bit for a day or so: each picture takes between three and eight hours to produce.
1 ' Mandelbrot generator
2 ' by Alex Clark
3 ' Amstrad Action August 87
10 MODE 1: MEMORY &4FFF: DIM c%(255)
20 INPUT"Automatic or manual operation? (a/m): ";am$
30 IF LEFT$(LOWER$(am$),1)="a" THEN GOSUB 120 ELSE GOSUB 50
Alex has been rather clever in the way he has used LEFT$ and LOWER$ in this line. LEFT$ (string expression, required length) is the standard format for the command. Its function is to extract the number of characters from a string (specified by required length) starting from the left of the string.
LOWER$ forces a string to lower-case - UPPER$ does the opposite. It is now easy to see what line 30 does to your input -which is held in am$ : the command LEFT$ (LOWER$ (am$) ,1) = a will force am$ to lowercase and interest itself in the leftmost character only. If the expression falls true, that is, finds that your input consists of an a , you'll be whisked off to line 120 , otherwise you're banished to line 50.
Dicing with data
Notice line 100, which reads a$= "!" + a$. The filename is held in a$. The exclamation mark prefixed to a$ prevents the SAVE message appearing on screen (which would rum five hours' computer time).
50 ' *** "j" "k" and "q" manual input
60 INPUT "Enter j... [real axis start]: ";j
70 INPUT "Enter k... [imaginary axis start]: ";k
80 INPUT "Enter q... [axes' length]: ";q
90 INPUT "Enter filename to save picture under: ";a$
110 GOSUB 280: GOSUB 540: MODE 1: RETURN
Once the program has accepted your input it will GOSUB 260 which does the calculating, GOSUB 540 (draws up the picture and saves it) and finally RETURNS whence it came.
120 ' *** Load in your own data for "j" "k" and "q"
130 ' or run this routine to give five good examples
140 ' from the Mandelbrot set ***
150 FOR a=1 TO 6: READ xa(a),ya(a),sa(a): NEXT
160 ' *** Coordinatess for entire set ***
170 DATA -2.0,-1.25,2.5
180 ' *** Interesting locations from set ***
190 DATA -0.9, 0.15, 0.2
200 DATA 0.353, 0.647, 0.005
210 DATA 0.34, 0.63, 0.04
220 DATA -0.737, 0.25, 0.025
230 DATA -0.725, 0.257, 0.006
240 FOR pics=1 TO 6
260 j=xa(pics): k=ya(pics): q=sa(pics)
270 GOSUB 280: GOSUB 540: MODE 1: NEXT: RETURN
If you picked automatic then you'll have landed here. Line 150 reads in the data starting at 170 which holds the coordinates for the entire Mandelbrot set and five other locations in and around the set. If you wish you can replace the existing data with some of your own.
The data is placed into arrays. Array xa will hold the six different j values, ya the six k values and so on.
Line 150 contains an interesting example of string-handling: a$ holds |mandpic. However, there is a plus sign following the closing quotemark , indicating more to a$ than is immediately obvious. The plus is followed by MID$ and a smattering of assorted characters. The function MID$ has the format MID$ (string variable, position, new string length). It behaves in a similar way to LEFT$. The string variable in this case is STR$ (pics). The variable pics ranges from one to six (defined in the for-next loop, line 240). STR$ will convert a numeric expression into string form. So the net result is that a$ will contain |mandpic1 , with the final digit increasing each time until 6.
The next section of the program is responsible for working out and storing the different points of the Mandelbrot set. You should have no trouble following this part. What you may not have come across is the form variable%. The percent-sign tagged onto a Basic variable's name indicates a special type an integer variable. An integer is a whole number; it does not allow a fractional part. There is a good reason for using this form of variable: it takes up less space in memory and consequently increases execution speed.
280 ' *** Mandelbrot set generator ***
290 BORDER 0: INK 0,0: INK 1,13: PAPER 0: PEN 1: MODE 1
300 PRINT "RUNNING"
350 FOR j%=0 TO 127
370 FOR k%=0 TO 127
460 IF n%=s% OR x2+y2>=4 THEN 470 ELSE 410
470 POKE add,n%: add=add+1: NEXT
480 PRINT j%+1;"completed out of 128 runs taking"
490 se=INT((TIME-t)/300): mi=INT(se/60): sec=se-(mi*60)
500 PRINT mi;"mins ";sec;"secs"
510 tot=tot+mi+(sec/60): NEXT
520 PRINT "end after about";INT(tot);"minutes"
The listing works out the points for the Mandelbrot set within an area 128 by 128. Each point is worked out individually - this amounts to an incredible 16,384 points to calculate. Given that each calculation with all its stages takes approximately one second, it's easy to understand why Arnold needs five hours to draw a single picture.
Drawing the set
The data for the Mandelbrot set is stored from memory location &5000 (in decimal, 20480). If a point is within the set it will have a value 255. The nearer the point is to the set. the higher its value. Points of the same value will have identical colours assigned to them, rather like contours on a map. In theory there could be 256 colours in the final display, but in practice too many colours would produce a confusing display. Not only that, the Amstrad can display a maximum of only 16 colours. So we compromise: ranges of numbers will have certain colours assigned to them.
540 ' *** Mandelbrot drawer ***
550 ' *** Initialise ***
570 FOR n%=0 TO 255
600 ' *** Find max and min values ***
620 FOR n%=1 TO 128*128
630 b%=PEEK(add): add=add+1
660 ' *** Assign colours to values ***
710 FOR n%=0 TO 254
740 IF t%>=ma*(maxcol%-j%+1) THEN j%=j%-1: IF j%<1 THEN j%=1
760 ' *** Draw Set ***
770 MODE 0: ORIGIN 64,72
780 RESTORE 790:FOR a%=0 TO 15:READ col%:INK a%,col%:NEXT
790 DATA 0,4,1,2,9,12,18,25,24,15,6,7,16,10,13,26
820 FOR j%=0 TO 127
830 FOR k%=0 TO 127
840 a%=PEEK(add): add=add+1
860 PLOT j%*4,k%*2,b%
890 ' *** Save Screen ***
900 SAVE a$ + ".pic", b, &C000, &4000
Now, while wait mg for your screen to fill, has anyone written a short, fast machine code version? Please send it in.
|★ AMSTRAD CPC ★ DOWNLOAD ★|
|★ AMSTRAD CPC ★ A voir aussi sur CPCrulez , les sujets suivants pourront vous intéresser...|
CPCrulez[Content Management System] v8.7-desktop
Page créée en 192 millisecondes et consultée 534 fois
L'Amstrad CPC est une machine 8 bits à base d'un Z80 à 4MHz. Le premier de la gamme fut le CPC 464 en 1984, équipé d'un lecteur de cassettes intégré il se plaçait en concurrent du Commodore C64 beaucoup plus compliqué à utiliser et plus cher. Ce fut un réel succès et sorti cette même années le CPC 664 équipé d'un lecteur de disquettes trois pouces intégré. Sa vie fut de courte durée puisqu'en 1985 il fut remplacé par le CPC 6128 qui était plus compact, plus soigné et surtout qui avait 128Ko de RAM au lieu de 64Ko.