Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -1 +1,3 @@
Generate and draw the [[wp:Mandelbrot set|Mandelbrot set]]. Note that there are [http://en.wikibooks.org/wiki/Fractals/Iterations_in_the_complex_plane/Mandelbrot_set many algorithms] to draw Mandelbrot set and there are [http://en.wikibooks.org/wiki/Pictures_of_Julia_and_Mandelbrot_sets many functions] which generate it .
Generate and draw the [[wp:Mandelbrot set|Mandelbrot set]].
Note that there are [http://en.wikibooks.org/wiki/Fractals/Iterations_in_the_complex_plane/Mandelbrot_set many algorithms] to draw Mandelbrot set and there are [http://en.wikibooks.org/wiki/Pictures_of_Julia_and_Mandelbrot_sets many functions] which generate it .

View file

@ -1,4 +1,5 @@
---
category:
- Graphics
- Raster graphics operations
note: Fractals

View file

@ -0,0 +1,69 @@
function Mandeliter( cx, cy, maxiter ){
var
x = 0.0,
y = 0.0,
xx = 0,
yy = 0,
xy = 0;
var i = maxiter;
while( i-- && xx + yy <= 4 ){
xy = x * y;
xx = x * x;
yy = y * y;
x = xx - yy + cx;
y = xy + xy + cy;
}
return maxiter - i;
}
function Mandelbrot( width,height, xmin,xmax, ymin,ymax, iterations ){
var canvas = document.createElement( 'canvas' );
canvas.width = width;
canvas.height = height;
var ctx = canvas.getContext( '2d' );
var img = ctx.getImageData( 0, 0, width, height );
var pix = img.data;
for( var ix = 0; ix < width; ++ix )
for( var iy = 0; iy < height; ++iy )
{
var x = xmin + (xmax - xmin) * ix / (width - 1);
var y = ymin + (ymax - ymin) * iy / (height - 1);
var i = Mandeliter( x, y, iterations );
var ppos = 4 * (width * iy + ix);
if( i === iterations )
{
pix[ppos] = 0;
pix[ppos+1] = 0;
pix[ppos+2] = 0;
}
else
{
var c = 3 * Math.log(i)/Math.log(iterations - 1.0);
if (c < 1)
{
pix[ppos] = 255*c;
pix[ppos+1] = 0;
pix[ppos+2] = 0;
}
else if( c < 2 )
{
pix[ppos] = 255;
pix[ppos+1] = 255*(c-1);
pix[ppos+2] = 0;
}
else
{
pix[ppos] = 255;
pix[ppos+1] = 255;
pix[ppos+2] = 255*(c-2);
}
}
pix[ ppos+3 ] = 255;
}
ctx.putImageData( img, 0,0 );
document.body.insertBefore( canvas, document.body.childNodes[0] );
}
Mandelbrot( 900,600, -2,1, -1,1, 1000 );

View file

@ -0,0 +1,88 @@
program mandelbrot;
const
ixmax = 800;
iymax = 800;
cxmin = -2.5;
cxmax = 1.5;
cymin = -2.0;
cymax = 2.0;
maxcolorcomponentvalue = 255;
maxiteration = 200;
escaperadius = 2;
type
colortype = record
red : byte;
green : byte;
blue : byte;
end;
var
ix, iy : integer;
cx, cy : real;
pixelwidth : real = (cxmax - cxmin) / ixmax;
pixelheight : real = (cymax - cymin) / iymax;
filename : string = 'new1.ppm';
comment : string = '# ';
outfile : textfile;
color : colortype;
zx, zy : real;
zx2, zy2 : real;
iteration : integer;
er2 : real = (escaperadius * escaperadius);
begin
{$I-}
assign(outfile, filename);
rewrite(outfile);
if ioresult <> 0 then
begin
writeln(stderr, 'unable to open output file: ', filename);
exit;
end;
writeln(outfile, 'P6');
writeln(outfile, ' ', comment);
writeln(outfile, ' ', ixmax);
writeln(outfile, ' ', iymax);
writeln(outfile, ' ', maxcolorcomponentvalue);
for iy := 1 to iymax do
begin
cy := cymin + (iy - 1)*pixelheight;
if abs(cy) < pixelheight / 2 then cy := 0.0;
for ix := 1 to ixmax do
begin
cx := cxmin + (ix - 1)*pixelwidth;
zx := 0.0;
zy := 0.0;
zx2 := zx*zx;
zy2 := zy*zy;
iteration := 0;
while (iteration < maxiteration) and (zx2 + zy2 < er2) do
begin
zy := 2*zx*zy + cy;
zx := zx2 - zy2 + cx;
zx2 := zx*zx;
zy2 := zy*zy;
iteration := iteration + 1;
end;
if iteration = maxiteration then
begin
color.red := 0;
color.green := 0;
color.blue := 0;
end
else
begin
color.red := 255;
color.green := 255;
color.blue := 255;
end;
write(outfile, chr(color.red), chr(color.green), chr(color.blue));
end;
end;
close(outfile);
end.

View file

@ -5,11 +5,14 @@ except:
pass
def mandelbrot(a): return reduce(lambda z, _: z*z + a, range(50), 0)
def step(start, step, iterations): return (start + (i * step) for i in range(iterations))
def mandelbrot(a):
return reduce(lambda z, _: z * z + a, range(50), 0)
rows = (('*' if abs(mandelbrot(complex(x, y))) < 2 else ' '
def step(start, step, iterations):
return (start + (i * step) for i in range(iterations))
rows = (("*" if abs(mandelbrot(complex(x, y))) < 2 else " "
for x in step(-2.0, .0315, 80))
for y in step(1, -.05, 41))
print( '\n'.join(''.join(row) for row in rows) )
print("\n".join("".join(row) for row in rows))

View file

@ -1,6 +1,15 @@
import math
mandelbrot = lambda z , c , n = 40 : float('nan') if abs(z) > 1000 else mandelbrot(z**2+c,c,n-1) if n > 0 else z**2+c
print("\n".join(["".join(["#" if not math.isnan(mandelbrot(0,x+1j*y).real) else " "
for x in [a*0.02 for a in xrange(-80,30)]])
for y in [a*0.05 for a in xrange(-20,20)]])
def mandelbrot(z , c , n=40):
if abs(z) > 1000:
return float("nan")
else:
if n > 0:
return mandelbrot(z ** 2 + c, c, n - 1)
else:
return z ** 2 + c
print("\n".join(["".join(["#" if not math.isnan(mandelbrot(0, x + 1j * y).real) else " "
for x in [a * 0.02 for a in range(-80, 30)]])
for y in [a * 0.05 for a in range(-20, 20)]])
)

View file

@ -0,0 +1,17 @@
/*REXX program generates and displays a Mandelbrot set as an ASCII image*/
xsize = 59; minre = -2; maxre = +1; stepx = (maxre-minre)/xsize
ysize = 21; minim = -1; maxim = +1; stepy = (maxim-minim)/ysize
do y=0 for ysize
im=minim+stepy*y
do x=0 for xsize
re=minre+stepx*x; zr=re; zi=im
do n=0 for 30
a=zr*zr; b=zi*zi
if a+b>4 then leave
zi=2*zr*zi+im; zr=a-b+re
end /*n*/
call charout ,d2c(62-n) /*display number as a char──►term*/
end /*x*/
say /*force last CHAROUTs to the term*/
end /*y*/ /*stick a fork in it, we're done.*/

View file

@ -1,4 +1,4 @@
import rosettacode.ArithmeticComplex._
import org.rosettacode.ArithmeticComplex._
import java.awt.Color
object Mandelbrot

View file

@ -1,3 +1,5 @@
import scala.swing._
import javax.swing.ImageIcon
val imgMandel=Mandelbrot.generate()
val mainframe=new MainFrame(){title="Test"; visible=true
contents=new Label(){icon=new ImageIcon(imgMandel.image)}

View file

@ -0,0 +1,240 @@
;
; Compute a Mandelbrot set on a simple Z80 computer.
;
; Porting this program to another Z80 platform should be easy and straight-
; forward: The only dependencies on my homebrew machine are the system-calls
; used to print strings and characters. These calls are performed by loading
; IX with the number of the system-call and performing an RST 08. To port this
; program to another operating system just replace these system-calls with
; the appropriate versions. Only three system-calls are used in the following:
; _crlf: Prints a CR/LF, _puts: Prints a 0-terminated string (the adress of
; which is expected in HL), and _putc: Print a single character which is
; expected in A. RST 0 give control back to the monitor.
;
#include "mondef.asm"
org ram_start
scale equ 256 ; Do NOT change this - the
; arithmetic routines rely on
; this scaling factor! :-)
divergent equ scale * 4
ld hl, welcome ; Print a welcome message
ld ix, _puts
rst 08
; for (y = <initial_value> ; y <= y_end; y += y_step)
; {
outer_loop ld hl, (y_end) ; Is y <= y_end?
ld de, (y)
and a ; Clear carry
sbc hl, de ; Perform the comparison
jp m, mandel_end ; End of outer loop reached
; for (x = x_start; x <= x_end; x += x_step)
; {
ld hl, (x_start) ; x = x_start
ld (x), hl
inner_loop ld hl, (x_end) ; Is x <= x_end?
ld de, (x)
and a
sbc hl, de
jp m, inner_loop_end ; End of inner loop reached
; z_0 = z_1 = 0;
ld hl, 0
ld (z_0), hl
ld (z_1), hl
; for (iteration = iteration_max; iteration; iteration--)
; {
ld a, (iteration_max)
ld b, a
iteration_loop push bc ; iteration -> stack
; z2 = (z_0 * z_0 - z_1 * z_1) / SCALE;
ld de, (z_1) ; Compute DE HL = z_1 * z_1
ld bc, de
call mul_16
ld (z_0_square_low), hl ; z_0 ** 2 is needed later again
ld (z_0_square_high), de
ld de, (z_0) ; Compute DE HL = z_0 * z_0
ld bc, de
call mul_16
ld (z_1_square_low), hl ; z_1 ** 2 will be also needed
ld (z_1_square_high), de
and a ; Compute subtraction
ld bc, (z_0_square_low)
sbc hl, bc
ld (scratch_0), hl ; Save lower 16 bit of result
ld hl, de
ld bc, (z_0_square_high)
sbc hl, bc
ld bc, (scratch_0) ; HL BC = z_0 ** 2 - z_1 ** 2
ld c, b ; Divide by scale = 256
ld b, l ; Discard the rest
push bc ; We need BC later
; z3 = 2 * z0 * z1 / SCALE;
ld hl, (z_0) ; Compute DE HL = 2 * z_0 * z_1
add hl, hl
ld de, hl
ld bc, (z_1)
call mul_16
ld b, e ; Divide by scale (= 256)
ld c, h ; BC contains now z_3
; z1 = z3 + y;
ld hl, (y)
add hl, bc
ld (z_1), hl
; z_0 = z_2 + x;
pop bc ; Here BC is needed again :-)
ld hl, (x)
add hl, bc
ld (z_0), hl
; if (z0 * z0 / SCALE + z1 * z1 / SCALE > 4 * SCALE)
ld hl, (z_0_square_low) ; Use the squares computed
ld de, (z_1_square_low) ; above
add hl, de
ld bc, hl ; BC contains lower word of sum
ld hl, (z_0_square_high)
ld de, (z_1_square_high)
adc hl, de
ld h, l ; HL now contains (z_0 ** 2 +
ld l, b ; z_1 ** 2) / scale
ld bc, divergent
and a
sbc hl, bc
; break;
jp c, iteration_dec ; No break
pop bc ; Get latest iteration counter
jr iteration_end ; Exit loop
; iteration++;
iteration_dec pop bc ; Get iteration counter
djnz iteration_loop ; We might fall through!
; }
iteration_end
; printf("%c", display[iteration % 7]);
ld a, b
and $7 ; lower three bits only (c = 0)
sbc hl, hl
ld l, a
ld de, display ; Get start of character array
add hl, de ; address and load the
ld a, (hl) ; character to be printed
ld ix, _putc ; Print the character
rst 08
ld de, (x_step) ; x += x_step
ld hl, (x)
add hl, de
ld (x), hl
jp inner_loop
; }
; printf("\n");
inner_loop_end ld ix, _crlf ; Print a CR/LF pair
rst 08
ld de, (y_step) ; y += y_step
ld hl, (y)
add hl, de
ld (y), hl ; Store new y-value
jp outer_loop
; }
mandel_end ld hl, finished ; Print finished-message
ld ix, _puts
rst 08
rst 0 ; Return to the monitor
welcome defb "Generating a Mandelbrot set"
defb cr, lf, eos
finished defb "Computation finished.", cr, lf, eos
iteration_max defb 10 ; How many iterations
x defw 0 ; x-coordinate
x_start defw -2 * scale ; Minimum x-coordinate
x_end defw 5 * scale / 10 ; Maximum x-coordinate
x_step defw 4 * scale / 100 ; x-coordinate step-width
y defw -1 * scale ; Minimum y-coordinate
y_end defw 1 * scale ; Maximum y-coordinate
y_step defw 1 * scale / 10 ; y-coordinate step-width
z_0 defw 0
z_1 defw 0
scratch_0 defw 0
z_0_square_high defw 0
z_0_square_low defw 0
z_1_square_high defw 0
z_1_square_low defw 0
display defb " .-+*=#@" ; 8 characters for the display
;
; Compute DEHL = BC * DE (signed): This routine is not too clever but it
; works. It is based on a standard 16-by-16 multiplication routine for unsigned
; integers. At the beginning the sign of the result is determined based on the
; signs of the operands which are negated if necessary. Then the unsigned
; multiplication takes place, followed by negating the result if necessary.
;
mul_16 xor a ; Clear carry and A (-> +)
bit 7, b ; Is BC negative?
jr z, bc_positive ; No
sub c ; A is still zero, complement
ld c, a
ld a, 0
sbc a, b
ld b, a
scf ; Set carry (-> -)
bc_positive bit 7, D ; Is DE negative?
jr z, de_positive ; No
push af ; Remember carry for later!
xor a
sub e
ld e, a
ld a, 0
sbc a, d
ld d, a
pop af ; Restore carry for complement
ccf ; Complement Carry (-> +/-?)
de_positive push af ; Remember state of carry
and a ; Start multiplication
sbc hl, hl
ld a, 16 ; 16 rounds
mul_16_loop add hl, hl
rl e
rl d
jr nc, mul_16_exit
add hl, bc
jr nc, mul_16_exit
inc de
mul_16_exit dec a
jr nz, mul_16_loop
pop af ; Restore carry from beginning
ret nc ; No sign inversion necessary
xor a ; Complement DE HL
sub l
ld l, a
ld a, 0
sbc a, h
ld h, a
ld a, 0
sbc a, e
ld e, a
ld a, 0
sbc a, d
ld d, a
ret