langs a-z

This commit is contained in:
Ingy döt Net 2013-04-10 22:43:41 -07:00
parent db842d013d
commit d066446780
11389 changed files with 98361 additions and 1020 deletions

View file

@ -0,0 +1,27 @@
#load "graphics.cma";;
let mandelbrot xMin xMax yMin yMax xPixels yPixels maxIter =
let rec mandelbrotIterator z c n =
if (Complex.norm z) > 2.0 then false else
match n with
| 0 -> true
| n -> let z' = Complex.add (Complex.mul z z) c in
mandelbrotIterator z' c (n-1) in
Graphics.open_graph
(" "^(string_of_int xPixels)^"x"^(string_of_int yPixels));
let dx = (xMax -. xMin) /. (float_of_int xPixels)
and dy = (yMax -. yMin) /. (float_of_int yPixels) in
for xi = 0 to xPixels - 1 do
for yi = 0 to yPixels - 1 do
let c = {Complex.re = xMin +. (dx *. float_of_int xi);
Complex.im = yMin +. (dy *. float_of_int yi)} in
if (mandelbrotIterator Complex.zero c maxIter) then
(Graphics.set_color Graphics.white;
Graphics.plot xi yi )
else
(Graphics.set_color Graphics.black;
Graphics.plot xi yi )
done
done;;
mandelbrot (-1.5) 0.5 (-1.0) 1.0 500 500 200;;

View file

@ -0,0 +1,34 @@
#! /usr/bin/octave -qf
global width = 200;
global height = 200;
maxiter = 100;
z0 = 0;
global cmax = 1 + i;
global cmin = -2 - i;
function cs = pscale(c)
global cmax;
global cmin;
global width;
global height;
persistent px = (real(cmax-cmin))/width;
persistent py = (imag(cmax-cmin))/height;
cs = real(cmin) + px*real(c) + i*(imag(cmin) + py*imag(c));
endfunction
ms = zeros(width, height);
for x = 0:width-1
for y = 0:height-1
z0 = 0;
c = pscale(x+y*i);
for ic = 1:maxiter
z1 = z0^2 + c;
if ( abs(z1) > 2 ) break; endif
z0 = z1;
endfor
ms(x+1, y+1) = ic/maxiter;
endfor
endfor
saveimage("mandel.ppm", round(ms .* 255).', "ppm");

View file

@ -0,0 +1,76 @@
my $height = @*ARGS[0] // 31;
$height = $height % 2 ?? +$height !! 1+$height;
my $width = $height;
my $max_iterations = 50;
my $upper-right = -2 + 5/4i;
my $lower-left = 1/2 - 5/4i;
my @color_map = (
"0 0 0", "0 0 252", "64 0 252", "124 0 252", "188 0 252",
"252 0 252", "252 0 188", "252 0 124", "252 0 64", "252 0 0",
"252 64 0", "252 124 0", "252 188 0", "252 252 0", "188 252 0",
"124 252 0", "64 252 0", "0 252 0", "0 252 64", "0 252 124",
"0 252 188", "0 252 252", "0 188 252", "0 124 252", "0 64 252",
"124 124 252", "156 124 252", "188 124 252", "220 124 252", "252 124 252",
"252 124 220", "252 124 188", "252 124 156", "252 124 124", "252 156 124",
"252 188 124", "252 220 124", "252 252 124", "220 252 124", "188 252 124",
"156 252 124", "124 252 124", "124 252 156", "124 252 188", "124 252 220",
"124 252 252", "124 220 252", "124 188 252", "124 156 252", "180 180 252",
"196 180 252", "216 180 252", "232 180 252", "252 180 252", "252 180 232",
"252 180 216", "252 180 196", "252 180 180", "252 196 180", "252 216 180",
"252 232 180", "252 252 180", "232 252 180", "216 252 180", "196 252 180",
"180 252 180", "180 252 196", "180 252 216", "180 252 232", "180 252 252",
"180 232 252", "180 216 252", "180 196 252", "0 0 112", "28 0 112",
"56 0 112", "84 0 112", "112 0 112", "112 0 84", "112 0 56",
"112 0 28", "112 0 0", "112 28 0", "112 56 0", "112 84 0",
"112 112 0", "84 112 0", "56 112 0", "28 112 0", "0 112 0",
"0 112 28", "0 112 56", "0 112 84", "0 112 112", "0 84 112",
"0 56 112", "0 28 112", "56 56 112", "68 56 112", "84 56 112",
"96 56 112", "112 56 112", "112 56 96", "112 56 84", "112 56 68",
"112 56 56", "112 68 56", "112 84 56", "112 96 56", "112 112 56",
"96 112 56", "84 112 56", "68 112 56", "56 112 56", "56 112 68",
"56 112 84", "56 112 96", "56 112 112", "56 96 112", "56 84 112",
"56 68 112", "80 80 112", "88 80 112", "96 80 112", "104 80 112",
"112 80 112", "112 80 104", "112 80 96", "112 80 88", "112 80 80",
"112 88 80", "112 96 80", "112 104 80", "112 112 80", "104 112 80",
"96 112 80", "88 112 80", "80 112 80", "80 112 88", "80 112 96",
"80 112 104", "80 112 112", "80 104 112", "80 96 112", "80 88 112",
"0 0 64", "16 0 64", "32 0 64", "48 0 64", "64 0 64", "64 0 48",
"64 0 32", "64 0 16", "64 0 0", "64 16 0", "64 32 0", "64 48 0",
"64 64 0", "48 64 0", "32 64 0", "16 64 0", "0 64 0", "0 64 16",
"0 64 32", "0 64 48", "0 64 64", "0 48 64", "0 32 64", "0 16 64",
"32 32 64", "40 32 64", "48 32 64", "56 32 64", "64 32 64",
"64 32 56", "64 32 48", "64 32 40", "64 32 32", "64 40 32",
"64 48 32", "64 56 32", "64 64 32", "56 64 32", "48 64 32",
"40 64 32", "32 64 32", "32 64 40", "32 64 48", "32 64 56",
"32 64 64", "32 56 64", "32 48 64", "32 40 64", "44 44 64",
"48 44 64", "52 44 64", "60 44 64", "64 44 64", "64 44 60",
"64 44 52", "64 44 48", "64 44 44", "64 48 44", "64 52 44",
"64 60 44", "64 64 44", "60 64 44", "52 64 44", "48 64 44",
"44 64 44", "44 64 48", "44 64 52", "44 64 60", "44 64 64",
"44 60 64", "44 52 64", "44 48 64"
);
sub mandel (Complex $c) {
my $z = 0i;
for ^$max_iterations -> $i {
return $i + 1 if $z.abs > 2;
$z = $z * $z + $c;
}
return 0;
}
sub subdivide($low, $high, $count) {
(^$count).map: { $low + ($_ / ($count - 1)) * ($high - $low) }
}
say "P3";
say "$width $height";
say "255";
for subdivide($upper-right.re, $lower-left.re, $height) -> $re {
my @line = subdivide($re + ($upper-right.im)i, $re + 0i, ($width + 1) / 2).map: { mandel($_) }
my $middle = @line.pop;
say ~(@line, $middle, @line.reverse).map: { @color_map[$_] }
}

View file

@ -0,0 +1,46 @@
%!PS-Adobe-2.0
%%BoundingBox: 0 0 300 200
%%EndComments
/origstate save def
/ld {load def} bind def
/m /moveto ld /g /setgray ld
/dot { currentpoint 1 0 360 arc fill } bind def
%%EndProlog
% param
/maxiter 200 def
% complex manipulation
/complex { 2 array astore } def
/real { 0 get } def
/imag { 1 get } def
/cmul { /a exch def /b exch def
a real b real mul
a imag b imag mul sub
a real b imag mul
a imag b real mul add
2 array astore
} def
/cadd { aload pop 3 -1 roll aload pop
3 -1 roll add
3 1 roll add exch 2 array astore
} def
/cconj { aload pop neg 2 array astore } def
/cabs2 { dup cconj cmul 0 get} def
% mandel
200 100 translate
-200 1 100 { /x exch def
-100 1 100 { /y exch def
/z0 0.0 0.0 complex def
0 1 maxiter { /iter exch def
x 100 div y 100 div complex
z0 z0 cmul
cadd dup /z0 exch def
cabs2 4 gt {exit} if
} for
iter maxiter div g
x y m dot
} for
} for
%
showpage
origstate restore
%%EOF

View file

@ -0,0 +1,58 @@
EnableExplicit
#Window1 = 0
#Image1 = 0
#ImgGadget = 0
#max_iteration = 64
#width = 800
#height = 600
Define.d x0 ,y0 ,xtemp ,cr, ci
Define.i i, n, x, y ,Event ,color
Dim Color.l (255)
For n = 0 To 63
Color( 0 + n ) = RGB( n*4+128, 4 * n, 0 )
Color( 64 + n ) = RGB( 64, 255, 4 * n )
Color( 128 + n ) = RGB( 64, 255 - 4 * n , 255 )
Color( 192 + n ) = RGB( 64, 0, 255 - 4 * n )
Next
If OpenWindow(#Window1, 0, 0, #width, #height, "'Mandelbrot set' PureBasic Example", #PB_Window_SystemMenu )
If CreateImage(#Image1, #width, #height)
ImageGadget(#ImgGadget, 0, 0, #width, #height, ImageID(#Image1))
For y.i = 1 To #height -1
StartDrawing(ImageOutput(#Image1))
For x.i = 1 To #width -1
x0 = 0
y0 = 0;
cr = (x / #width)*2.5 -2
ci = (y / #height)*2.5 -1.25
i = 0
While (x0*x0 + y0*y0 <= 4.0) And i < #max_iteration
i +1
xtemp = x0*x0 - y0*y0 + cr
y0 = 2*x0*y0 + ci
x0 = xtemp
Wend
If i >= #max_iteration
Plot(x, y, 0 )
Else
Plot(x, y, Color(i & 255))
EndIf
Next
StopDrawing()
SetGadgetState(#ImgGadget, ImageID(#Image1))
Repeat
Event = WindowEvent()
If Event = #PB_Event_CloseWindow
End
EndIf
Until Event = 0
Next
EndIf
Repeat
Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow
EndIf

View file

@ -0,0 +1,25 @@
PROGRAM:MANDELBR
:Input "ITER. ",D
:For(A,Xmin,Xmax,ΔX)
:For(B,Ymin,Ymax,ΔY)
:0→X
:0→Y
:0→I
:D→M
:While X^2+Y^2≤4 and I<M
:X^2-Y^2+A→R
:2XY+B→Y
:R→X
:I+1→I
:End
:If I≠M
:Then
:I→C
:Else
:0→C
:End
:If C<1
:Pt-On(A,B)
:End
:End
:End

View file

@ -0,0 +1,26 @@
include c:\cxpl\codes; \intrinsic 'code' declarations
int X, Y, \screen coordinates of current point
Cnt; \iteration counter
real Cx, Cy, \coordinates scaled to +/-2 range
Zx, Zy, \complex accumulator
Temp; \temporary scratch
[SetVid($112); \set 640x480x24 graphics mode
for Y:= 0 to 480-1 do \for all points on the screen...
for X:= 0 to 640-1 do
[Cx:= (float(X)/640.0 - 0.5) * 4.0; \range: -2.0 to +2.0
Cy:= (float(Y-240)/240.0) * 1.5; \range: -1.5 to +1.5
Cnt:= 0; Zx:= 0.0; Zy:= 0.0; \initialize
loop [if Zx*Zx + Zy*Zy > 2.0 then \Z heads toward infinity
[Point(X, Y, Cnt<<21+Cnt<<10+Cnt<<3); \set color of pixel to
quit; \ rate it approached infinity
]; \move on to next point
Temp:= Zx*Zy;
Zx:= Zx*Zx - Zy*Zy + Cx; \calculate next iteration of Z
Zy:= 2.0*Temp + Cy;
Cnt:= Cnt+1; \count number of iterations
if Cnt >= 1000 then quit; \assume point is in Mandelbrot
]; \ set and leave it colored black
];
X:= ChIn(1); \wait for keystroke
SetVid($03); \restore normal text display
]

View file

@ -0,0 +1,95 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- XSLT Mandelbrot - written by Joel Yliluoma 2007, http://iki.fi/bisqwit/ -->
<xsl:output method="html" indent="no"
doctype-public="-//W3C//DTD HTML 4.01//EN"
doctype-system="http://www.w3.org/TR/REC-html40/strict.dtd"
/>
<xsl:template match="/fractal">
<html>
<head>
<title>XSLT fractal</title>
<style type="text/css">
body { color:#55F; background:#000 }
pre { font-family:monospace; font-size:7px }
pre span { background:<xsl:value-of select="background" /> }
</style>
</head>
<body>
<div style="position:absolute;top:20px;left:20em">
Copyright © 1992,2007 Joel Yliluoma
(<a href="http://iki.fi/bisqwit/">http://iki.fi/bisqwit/</a>)
</div>
<h1 style="margin:0px">XSLT fractal</h1>
<pre><xsl:call-template name="bisqwit-mandelbrot" /></pre>
</body>
</html>
</xsl:template>
<xsl:template name="bisqwit-mandelbrot"
><xsl:call-template name="bisqwit-mandelbrot-line">
<xsl:with-param name="y" select="y/min"/>
</xsl:call-template
></xsl:template>
<xsl:template name="bisqwit-mandelbrot-line"
><xsl:param name="y"
/><xsl:call-template name="bisqwit-mandelbrot-column">
<xsl:with-param name="x" select="x/min"/>
<xsl:with-param name="y" select="$y"/>
</xsl:call-template
><xsl:if test="$y < y/max"
><br
/><xsl:call-template name="bisqwit-mandelbrot-line">
<xsl:with-param name="y" select="$y + y/step"/>
</xsl:call-template
></xsl:if
></xsl:template>
<xsl:template name="bisqwit-mandelbrot-column"
><xsl:param name="x"
/><xsl:param name="y"
/><xsl:call-template name="bisqwit-mandelbrot-slot">
<xsl:with-param name="x" select="$x" />
<xsl:with-param name="y" select="$y" />
<xsl:with-param name="zr" select="$x" />
<xsl:with-param name="zi" select="$y" />
</xsl:call-template
><xsl:if test="$x < x/max"
><xsl:call-template name="bisqwit-mandelbrot-column">
<xsl:with-param name="x" select="$x + x/step"/>
<xsl:with-param name="y" select="$y" />
</xsl:call-template
></xsl:if
></xsl:template>
<xsl:template name="bisqwit-mandelbrot-slot"
><xsl:param name="x"
/><xsl:param name="y"
/><xsl:param name="zr"
/><xsl:param name="zi"
/><xsl:param name="iter" select="0"
/><xsl:variable name="zrsqr" select="($zr * $zr)"
/><xsl:variable name="zisqr" select="($zi * $zi)"
/><xsl:choose>
<xsl:when test="(4*scale*scale >= $zrsqr + $zisqr) and (maxiter > $iter+1)"
><xsl:call-template name="bisqwit-mandelbrot-slot">
<xsl:with-param name="x" select="$x" />
<xsl:with-param name="y" select="$y" />
<xsl:with-param name="zi" select="(2 * $zr * $zi) div scale + $y" />
<xsl:with-param name="zr" select="($zrsqr - $zisqr) div scale + $x" />
<xsl:with-param name="iter" select="$iter + 1" />
</xsl:call-template
></xsl:when>
<xsl:otherwise
><xsl:variable name="magnitude" select="magnitude[@value=$iter]"
/><span style="color:{$magnitude/color}"
><xsl:value-of select="$magnitude/symbol"
/></span></xsl:otherwise>
</xsl:choose
></xsl:template>
</xsl:stylesheet>