September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -1,29 +1,13 @@
|
|||
GraphicsWindow.Show()
|
||||
size = 500
|
||||
half = 250
|
||||
GraphicsWindow.Width = size * 1.5
|
||||
GraphicsWindow.Height = size
|
||||
GraphicsWindow.Title = "Mandelbrot"
|
||||
For px = 1 To size * 1.5
|
||||
x_0 = px/half - 2
|
||||
For py = 1 To size
|
||||
y_0 = py/half - 1
|
||||
x = x_0
|
||||
y = y_0
|
||||
i = 0
|
||||
While(c <= 2 AND i<100)
|
||||
x_1 = Math.Power(x, 2) - Math.Power(y, 2) + x_0
|
||||
y_1 = 2 * x * y + y_0
|
||||
c = Math.Power(Math.Power(x_1, 2) + Math.Power(y_1, 2), 0.5)
|
||||
x = x_1
|
||||
y = y_1
|
||||
i = i + 1
|
||||
EndWhile
|
||||
If i < 99 Then
|
||||
GraphicsWindow.SetPixel(px, py, GraphicsWindow.GetColorFromRGB((255/25)*i, (255/25)*i, (255/5)*i))
|
||||
Else
|
||||
GraphicsWindow.SetPixel(px, py, "black")
|
||||
EndIf
|
||||
c=0
|
||||
EndFor
|
||||
EndFor
|
||||
10 FOR X=-2 TO 2 STEP 0.1
|
||||
20 FOR Y=-2 TO 2 STEP 0.1
|
||||
30 LET XA=0
|
||||
40 LET YA=0
|
||||
50 LET ITER=0
|
||||
60 LET XTEMP=XA*XA-YA*YA+X
|
||||
70 LET YA=2*XA*YA+Y
|
||||
80 LET XA=XTEMP
|
||||
90 LET ITER=ITER+1
|
||||
100 IF XA*XA+YA*YA<=4 AND ITER<200 THEN GOTO 60
|
||||
110 IF ITER<200 THEN PLOT X*10+20,Y*10+20
|
||||
120 NEXT Y
|
||||
130 NEXT X
|
||||
|
|
|
|||
|
|
@ -1,15 +1,29 @@
|
|||
Function mandel(xi As Double, yi As Double)
|
||||
|
||||
maxiter = 256
|
||||
x = 0
|
||||
y = 0
|
||||
|
||||
For i = 1 To maxiter
|
||||
If ((x * x) + (y * y)) > 4 Then Exit For
|
||||
xt = xi + ((x * x) - (y * y))
|
||||
y = yi + (2 * x * y)
|
||||
x = xt
|
||||
Next
|
||||
|
||||
mandel = i
|
||||
End Function
|
||||
GraphicsWindow.Show()
|
||||
size = 500
|
||||
half = 250
|
||||
GraphicsWindow.Width = size * 1.5
|
||||
GraphicsWindow.Height = size
|
||||
GraphicsWindow.Title = "Mandelbrot"
|
||||
For px = 1 To size * 1.5
|
||||
x_0 = px/half - 2
|
||||
For py = 1 To size
|
||||
y_0 = py/half - 1
|
||||
x = x_0
|
||||
y = y_0
|
||||
i = 0
|
||||
While(c <= 2 AND i<100)
|
||||
x_1 = Math.Power(x, 2) - Math.Power(y, 2) + x_0
|
||||
y_1 = 2 * x * y + y_0
|
||||
c = Math.Power(Math.Power(x_1, 2) + Math.Power(y_1, 2), 0.5)
|
||||
x = x_1
|
||||
y = y_1
|
||||
i = i + 1
|
||||
EndWhile
|
||||
If i < 99 Then
|
||||
GraphicsWindow.SetPixel(px, py, GraphicsWindow.GetColorFromRGB((255/25)*i, (255/25)*i, (255/5)*i))
|
||||
Else
|
||||
GraphicsWindow.SetPixel(px, py, "black")
|
||||
EndIf
|
||||
c=0
|
||||
EndFor
|
||||
EndFor
|
||||
|
|
|
|||
15
Task/Mandelbrot-set/BASIC/mandelbrot-set-9.basic
Normal file
15
Task/Mandelbrot-set/BASIC/mandelbrot-set-9.basic
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
Function mandel(xi As Double, yi As Double)
|
||||
|
||||
maxiter = 256
|
||||
x = 0
|
||||
y = 0
|
||||
|
||||
For i = 1 To maxiter
|
||||
If ((x * x) + (y * y)) > 4 Then Exit For
|
||||
xt = xi + ((x * x) - (y * y))
|
||||
y = yi + (2 * x * y)
|
||||
x = xt
|
||||
Next
|
||||
|
||||
mandel = i
|
||||
End Function
|
||||
|
|
@ -1,34 +0,0 @@
|
|||
fastgraphics
|
||||
|
||||
graphsize 384,384
|
||||
refresh
|
||||
kt=319 : m = 4.0
|
||||
xmin=2.1 : xmax=-0.6 : ymin=-1.35 : ymax=1.35
|
||||
dx=(xmax-xmin)/graphwidth : dy=(ymax-ymin)/graphheight
|
||||
|
||||
for x=0 to graphwidth
|
||||
jx = xmin+x*dx
|
||||
for y=0 to graphheight
|
||||
jy = ymin+y*dy
|
||||
k = 0 : wx = 0.0 : wy = 0.0
|
||||
do
|
||||
tx = wx*wx-(wy*wy+jx)
|
||||
ty = 2.0*wx*wy+jy
|
||||
wx = tx
|
||||
wy = ty
|
||||
r = wx*wx+wy*wy
|
||||
k = k+1
|
||||
until r>m or k>kt
|
||||
|
||||
if k>kt then
|
||||
color black
|
||||
else
|
||||
if k<16 then color k*8,k*8,128+k*4
|
||||
if k>=16 and k<64 then color 128+k-16,128+k-16,192+k-16
|
||||
if k>=64 then color kt-k,128+(kt-k)/2,kt-k
|
||||
end if
|
||||
plot x,y
|
||||
next y
|
||||
refresh
|
||||
next x
|
||||
imgsave "Mandelbrot_BASIC-256.png", "PNG"
|
||||
|
|
@ -1,22 +0,0 @@
|
|||
sizex% = 300 : sizey% = 300
|
||||
maxiter% = 128
|
||||
VDU 23,22,sizex%;sizey%;8,8,16,128
|
||||
ORIGIN 0,sizey%
|
||||
GCOL 1
|
||||
FOR X% = 0 TO 2*sizex%-2 STEP 2
|
||||
xi = X%/200 - 2
|
||||
FOR Y% = 0 TO sizey%-2 STEP 2
|
||||
yi = Y% / 200
|
||||
x = 0
|
||||
y = 0
|
||||
FOR I% = 1 TO maxiter%
|
||||
IF x*x+y*y > 4 EXIT FOR
|
||||
xt = xi + x*x-y*y
|
||||
y = yi + 2*x*y
|
||||
x = xt
|
||||
NEXT
|
||||
IF I%>maxiter% I%=0
|
||||
COLOUR 1,I%*15,I%*8,0
|
||||
PLOT X%,Y% : PLOT X%,-Y%
|
||||
NEXT
|
||||
NEXT X%
|
||||
|
|
@ -11,6 +11,7 @@
|
|||
to see the file use external application ( graphic viewer)
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
int main()
|
||||
{
|
||||
/* screen ( integer) coordinate */
|
||||
|
|
|
|||
45
Task/Mandelbrot-set/Common-Lisp/mandelbrot-set.lisp
Normal file
45
Task/Mandelbrot-set/Common-Lisp/mandelbrot-set.lisp
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
(defpackage #:mandelbrot
|
||||
(:use #:cl))
|
||||
|
||||
(in-package #:mandelbrot)
|
||||
|
||||
(deftype pixel () '(unsigned-byte 8))
|
||||
(deftype image () '(array pixel))
|
||||
|
||||
(defun write-pgm (image filespec)
|
||||
(declare (image image))
|
||||
(with-open-file (s filespec :direction :output :element-type 'pixel :if-exists :supersede)
|
||||
(let* ((width (array-dimension image 1))
|
||||
(height (array-dimension image 0))
|
||||
(header (format nil "P5~A~D ~D~A255~A" #\Newline width height #\Newline #\Newline)))
|
||||
(loop for c across header
|
||||
do (write-byte (char-code c) s))
|
||||
(dotimes (row height)
|
||||
(dotimes (col width)
|
||||
(write-byte (aref image row col) s))))))
|
||||
|
||||
(defparameter *x-max* 800)
|
||||
(defparameter *y-max* 800)
|
||||
(defparameter *cx-min* -2.5)
|
||||
(defparameter *cx-max* 1.5)
|
||||
(defparameter *cy-min* -2.0)
|
||||
(defparameter *cy-max* 2.0)
|
||||
(defparameter *escape-radius* 2)
|
||||
(defparameter *iteration-max* 40)
|
||||
|
||||
(defun mandelbrot (filespec)
|
||||
(let ((pixel-width (/ (- *cx-max* *cx-min*) *x-max*))
|
||||
(pixel-height (/ (- *cy-max* *cy-min*) *y-max*))
|
||||
(image (make-array (list *y-max* *x-max*) :element-type 'pixel :initial-element 0)))
|
||||
(loop for y from 0 below *y-max*
|
||||
for cy from *cy-min* by pixel-height
|
||||
do (loop for x from 0 below *x-max*
|
||||
for cx from *cx-min* by pixel-width
|
||||
for iteration = (loop with c = (complex cx cy)
|
||||
for iteration from 0 below *iteration-max*
|
||||
for z = c then (+ (* z z) c)
|
||||
while (< (abs z) *escape-radius*)
|
||||
finally (return iteration))
|
||||
for pixel = (round (* 255 (/ (- *iteration-max* iteration) *iteration-max*)))
|
||||
do (setf (aref image y x) pixel)))
|
||||
(write-pgm image filespec)))
|
||||
51
Task/Mandelbrot-set/EC/mandelbrot-set-1.ec
Normal file
51
Task/Mandelbrot-set/EC/mandelbrot-set-1.ec
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
void drawMandelbrot(Bitmap bmp, float range, Complex center, ColorAlpha * palette, int nPalEntries, int nIterations, float scale)
|
||||
{
|
||||
int x, y;
|
||||
int w = bmp.width, h = bmp.height;
|
||||
ColorAlpha * picture = (ColorAlpha *)bmp.picture;
|
||||
double logOf2 = log(2);
|
||||
Complex d
|
||||
{
|
||||
w > h ? range : range * w / h,
|
||||
h > w ? range : range * h / w
|
||||
};
|
||||
Complex C0 { center.a - d.a/2, center.b - d.b/2 };
|
||||
Complex C = C0;
|
||||
double delta = d.a / w;
|
||||
|
||||
for(y = 0; y < h; y++, C.a = C0.a, C.b += delta)
|
||||
{
|
||||
for(x = 0; x < w; x++, picture++, C.a += delta)
|
||||
{
|
||||
Complex Z { };
|
||||
int i;
|
||||
double ii = 0;
|
||||
bool out = false;
|
||||
double Za2 = Z.a * Z.a, Zb2 = Z.b * Z.b;
|
||||
for(i = 0; i < nIterations; i++)
|
||||
{
|
||||
double z2;
|
||||
Z = { Za2 - Zb2, 2*Z.a*Z.b };
|
||||
Z.a += C.a;
|
||||
Z.b += C.b;
|
||||
Za2 = Z.a * Z.a, Zb2 = Z.b * Z.b;
|
||||
z2 = Za2 + Zb2;
|
||||
|
||||
if(z2 >= 2*2)
|
||||
{
|
||||
ii = (double)(i + 1 - log(0.5 * log(z2)) / logOf2);
|
||||
out = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(out)
|
||||
{
|
||||
float si = (float)(ii * scale);
|
||||
int i0 = ((int)si) % nPalEntries;
|
||||
*picture = palette[i0];
|
||||
}
|
||||
else
|
||||
*picture = black;
|
||||
}
|
||||
}
|
||||
}
|
||||
135
Task/Mandelbrot-set/EC/mandelbrot-set-2.ec
Normal file
135
Task/Mandelbrot-set/EC/mandelbrot-set-2.ec
Normal file
|
|
@ -0,0 +1,135 @@
|
|||
class Mandelbrot : Window
|
||||
{
|
||||
caption = $"Mandelbrot";
|
||||
borderStyle = sizable;
|
||||
hasMaximize = true;
|
||||
hasMinimize = true;
|
||||
hasClose = true;
|
||||
clientSize = { 600, 600 };
|
||||
|
||||
Point mouseStart, mouseEnd;
|
||||
bool dragging;
|
||||
bool needUpdate;
|
||||
|
||||
float scale;
|
||||
int nIterations; nIterations = 256;
|
||||
ColorAlpha * palette;
|
||||
int nPalEntries;
|
||||
Complex center { -0.75, 0 };
|
||||
|
||||
float range; range = 4;
|
||||
Bitmap bmp { };
|
||||
|
||||
Mandelbrot()
|
||||
{
|
||||
static ColorKey keys[] =
|
||||
{
|
||||
{ navy, 0.0f },
|
||||
{ Color { 146, 213, 237 }, 0.198606268f },
|
||||
{ white, 0.3f },
|
||||
{ Color { 255, 255, 124 }, 0.444250882f },
|
||||
{ Color { 255, 100, 0 }, 0.634146333f },
|
||||
{ navy, 1 }
|
||||
};
|
||||
|
||||
nPalEntries = 30000;
|
||||
palette = new ColorAlpha[nPalEntries];
|
||||
scale = nPalEntries / 175.0f;
|
||||
PaletteGradient(palette, nPalEntries, keys, sizeof(keys)/sizeof(keys[0]), 1.0);
|
||||
needUpdate = true;
|
||||
}
|
||||
|
||||
~Mandelbrot() { delete palette; }
|
||||
|
||||
void OnRedraw(Surface surface)
|
||||
{
|
||||
if(needUpdate)
|
||||
{
|
||||
drawMandelbrot(bmp, range, center, palette, nPalEntries, nIterations, scale);
|
||||
needUpdate = false;
|
||||
}
|
||||
surface.Blit(bmp, 0,0, 0,0, bmp.width, bmp.height);
|
||||
|
||||
if(dragging)
|
||||
{
|
||||
surface.foreground = lime;
|
||||
surface.Rectangle(mouseStart.x, mouseStart.y, mouseEnd.x, mouseEnd.y);
|
||||
}
|
||||
}
|
||||
|
||||
bool OnLeftButtonDown(int x, int y, Modifiers mods)
|
||||
{
|
||||
mouseEnd = mouseStart = { x, y };
|
||||
Capture();
|
||||
dragging = true;
|
||||
Update(null);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool OnLeftButtonUp(int x, int y, Modifiers mods)
|
||||
{
|
||||
if(dragging)
|
||||
{
|
||||
int dx = Abs(mouseEnd.x - mouseStart.x), dy = Abs(mouseEnd.y - mouseStart.y);
|
||||
if(dx > 4 && dy > 4)
|
||||
{
|
||||
int w = clientSize.w, h = clientSize.h;
|
||||
float rangeX = w > h ? range : range * w / h;
|
||||
float rangeY = h > w ? range : range * h / w;
|
||||
|
||||
center.a += ((mouseStart.x + mouseEnd.x) - w) / 2.0f * rangeX / w;
|
||||
center.b += ((mouseStart.y + mouseEnd.y) - h) / 2.0f * rangeY / h;
|
||||
|
||||
range = dy > dx ? dy * range / h : dx * range / w;
|
||||
|
||||
needUpdate = true;
|
||||
Update(null);
|
||||
}
|
||||
ReleaseCapture();
|
||||
dragging = false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool OnMouseMove(int x, int y, Modifiers mods)
|
||||
{
|
||||
if(dragging)
|
||||
{
|
||||
mouseEnd = { x, y };
|
||||
Update(null);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool OnRightButtonDown(int x, int y, Modifiers mods)
|
||||
{
|
||||
range = 4;
|
||||
nIterations = 256;
|
||||
center = { -0.75, 0 };
|
||||
needUpdate = true;
|
||||
Update(null);
|
||||
return true;
|
||||
}
|
||||
|
||||
void OnResize(int width, int height)
|
||||
{
|
||||
bmp.Allocate(null, width, height, 0, pixelFormat888, false);
|
||||
needUpdate = true;
|
||||
Update(null);
|
||||
}
|
||||
|
||||
bool OnKeyHit(Key key, unichar ch)
|
||||
{
|
||||
switch(key)
|
||||
{
|
||||
case space: case keyPadPlus: case plus:
|
||||
nIterations += 256;
|
||||
needUpdate = true;
|
||||
Update(null);
|
||||
break;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Mandelbrot mandelbrotForm {};
|
||||
31
Task/Mandelbrot-set/Factor/mandelbrot-set.factor
Normal file
31
Task/Mandelbrot-set/Factor/mandelbrot-set.factor
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
! with ("::") or without (":") generalizations:
|
||||
! : [a..b] ( steps a b -- a..b ) 2dup swap - 4 nrot 1 - / <range> ;
|
||||
:: [a..b] ( steps a b -- a..b ) a b b a - steps 1 - / <range> ;
|
||||
|
||||
: >char ( n -- c )
|
||||
dup -1 = [ drop 32 ] [ 26 mod CHAR: a + ] if ;
|
||||
|
||||
! iterates z' = z^2 + c, Factor does complex numbers!
|
||||
: iter ( c z -- z' ) dup * + ;
|
||||
|
||||
: unbound ( c -- ? ) absq 4 > ;
|
||||
|
||||
:: mz ( c max i z -- n )
|
||||
{
|
||||
{ [ i max >= ] [ -1 ] }
|
||||
{ [ z unbound ] [ i ] }
|
||||
[ c max i 1 + c z iter mz ]
|
||||
} cond ;
|
||||
|
||||
: mandelzahl ( c max -- n ) 0 0 mz ;
|
||||
|
||||
:: mandel ( w h max -- )
|
||||
h -1. 1. [a..b] ! range over y
|
||||
[ w -2. 1. [a..b] ! range over x
|
||||
[ dupd swap rect> max mandelzahl >char ] map
|
||||
>string print
|
||||
drop ! old y
|
||||
] each
|
||||
;
|
||||
|
||||
70 25 1000 mandel
|
||||
2
Task/Mandelbrot-set/Gnuplot/mandelbrot-set-1.gnuplot
Normal file
2
Task/Mandelbrot-set/Gnuplot/mandelbrot-set-1.gnuplot
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
set terminal png
|
||||
set output 'mandelbrot.png'
|
||||
9
Task/Mandelbrot-set/Gnuplot/mandelbrot-set-2.gnuplot
Normal file
9
Task/Mandelbrot-set/Gnuplot/mandelbrot-set-2.gnuplot
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
rmax = 2
|
||||
nmax = 100
|
||||
complex (x, y) = x * {1, 0} + y * {0, 1}
|
||||
mandelbrot (z, z0, n) = n == nmax || abs (z) > rmax ? n : mandelbrot (z ** 2 + z0, z0, n + 1)
|
||||
set samples 200
|
||||
set isosamples 200
|
||||
set pm3d map
|
||||
set size square
|
||||
splot [-2 : .8] [-1.4 : 1.4] mandelbrot (complex (0, 0), complex (x, y), 0) notitle
|
||||
|
|
@ -1,66 +1,69 @@
|
|||
function Mandeliter(cx, cy, maxiter)
|
||||
{
|
||||
var i;
|
||||
function mandelIter(cx, cy, maxIter) {
|
||||
var x = 0.0;
|
||||
var y = 0.0;
|
||||
for (i = 0; i < maxiter && x*x + y*y <= 4; ++i)
|
||||
{
|
||||
var tmp = 2*x*y;
|
||||
x = x*x - y*y + cx;
|
||||
y = tmp + cy;
|
||||
var xx = 0;
|
||||
var yy = 0;
|
||||
var 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 i;
|
||||
return maxIter - i;
|
||||
}
|
||||
|
||||
function Mandelbrot()
|
||||
{
|
||||
var width = 900;
|
||||
var height = 600;
|
||||
var cd = document.getElementById('calcdata');
|
||||
var xmin = parseFloat(cd.xmin.value);
|
||||
var xmax = parseFloat(cd.xmax.value);
|
||||
var ymin = parseFloat(cd.ymin.value);
|
||||
var ymax = parseFloat(cd.ymax.value);
|
||||
var iterations = parseInt(cd.iterations.value);
|
||||
var ctx = document.getElementById('mandelimage').getContext("2d");
|
||||
function mandelbrot(canvas, xmin, xmax, ymin, ymax, iterations) {
|
||||
var width = canvas.width;
|
||||
var height = canvas.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*(900*iy + ix);
|
||||
if (i == iterations)
|
||||
{
|
||||
|
||||
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;
|
||||
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)
|
||||
{
|
||||
else if ( c < 2 ) {
|
||||
pix[ppos] = 255;
|
||||
pix[ppos+1] = 255*(c-1);
|
||||
pix[ppos+2] = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
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 + 1] = 255;
|
||||
pix[ppos + 2] = 255 * (c - 2);
|
||||
}
|
||||
}
|
||||
pix[ppos+3] = 255;
|
||||
pix[ppos + 3] = 255;
|
||||
}
|
||||
ctx.putImageData(img,0,0);
|
||||
}
|
||||
|
||||
ctx.putImageData(img, 0, 0);
|
||||
}
|
||||
|
||||
var canvas = document.createElement('canvas');
|
||||
canvas.width = 900;
|
||||
canvas.height = 600;
|
||||
|
||||
document.body.insertBefore(canvas, document.body.childNodes[0]);
|
||||
|
||||
mandelbrot(canvas, -2, 1, -1, 1, 1000);
|
||||
|
|
|
|||
|
|
@ -1,38 +1,22 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Mandelbrot set</title>
|
||||
<script src="Mandelbrot.js" type="text/javascript"></script>
|
||||
</head>
|
||||
var mandelIter;
|
||||
fetch("./mandelIter.wasm")
|
||||
.then(res => {
|
||||
if (res.ok) return res.arrayBuffer();
|
||||
throw new Error('Unable to fetch WASM.');
|
||||
})
|
||||
.then(bytes => { return WebAssembly.compile(bytes); })
|
||||
.then(module => { return WebAssembly.instantiate(module); })
|
||||
.then(instance => { WebAssembly.instance = instance; draw(); })
|
||||
|
||||
<body onload="Mandelbrot()">
|
||||
<h1>Mandelbrot set</h1>
|
||||
function mandelbrot(canvas, xmin, xmax, ymin, ymax, iterations) {
|
||||
// ...
|
||||
var i = WebAssembly.instance.exports.mandelIter(x, y, iterations);
|
||||
// ...
|
||||
}
|
||||
|
||||
<form id="calcdata" onsubmit="javascript:Mandelbrot(); return false;">
|
||||
<table>
|
||||
<tr>
|
||||
<td>xmin =</td>
|
||||
<td><input name="xmin" type="text" size="10" value="-2"></td>
|
||||
<td>xmax =</td>
|
||||
<td><input name="xmax" type="text" size="10" value="1"></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>ymin =</td>
|
||||
<td><input name="ymin" type="text" size="10" value="-1"></td>
|
||||
<td>ymax =</td>
|
||||
<td><input name="ymax" type="text" size="10" value="1"></td>
|
||||
</tr>
|
||||
</table>
|
||||
<p>iterations =
|
||||
<input name="iterations" type="text" size="10" value="1000"></p>
|
||||
<p>
|
||||
<input type="submit" value=" Calculate ">
|
||||
<input type="reset" value=" Reset form ">
|
||||
</p>
|
||||
</form>
|
||||
|
||||
<canvas id="mandelimage" width="900" height="600">
|
||||
This page needs a browser with canvas support.
|
||||
</canvas>
|
||||
</body>
|
||||
</html>
|
||||
function draw() {
|
||||
// canvas initialization if necessary
|
||||
// ...
|
||||
mandelbrot(canvas, -2, 1, -1, 1, 1000);
|
||||
// ...
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,69 +0,0 @@
|
|||
function mandelIter(cx, cy, maxIter) {
|
||||
var x = 0.0;
|
||||
var y = 0.0;
|
||||
var xx = 0;
|
||||
var yy = 0;
|
||||
var 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(canvas, xmin, xmax, ymin, ymax, iterations) {
|
||||
var width = canvas.width;
|
||||
var height = canvas.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);
|
||||
}
|
||||
|
||||
var canvas = document.createElement('canvas');
|
||||
canvas.width = 900;
|
||||
canvas.height = 600;
|
||||
|
||||
document.body.insertBefore(canvas, document.body.childNodes[0]);
|
||||
|
||||
mandelbrot(canvas, -2, 1, -1, 1, 1000);
|
||||
56
Task/Mandelbrot-set/Julia/mandelbrot-set-2.julia
Normal file
56
Task/Mandelbrot-set/Julia/mandelbrot-set-2.julia
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
using Images
|
||||
|
||||
@inline function hsv2rgb(h, s, v)
|
||||
const c = v * s
|
||||
const x = c * (1 - abs(((h/60) % 2) - 1))
|
||||
const m = v - c
|
||||
|
||||
const r,g,b =
|
||||
if h < 60
|
||||
(c, x, 0)
|
||||
elseif h < 120
|
||||
(x, c, 0)
|
||||
elseif h < 180
|
||||
(0, c, x)
|
||||
elseif h < 240
|
||||
(0, x, c)
|
||||
elseif h < 300
|
||||
(x, 0, c)
|
||||
else
|
||||
(c, 0, x)
|
||||
end
|
||||
|
||||
(r + m), (b + m), (g + m)
|
||||
end
|
||||
|
||||
function mandelbrot()
|
||||
|
||||
const w, h = 1000, 1000
|
||||
|
||||
const zoom = 0.5
|
||||
const moveX = 0
|
||||
const moveY = 0
|
||||
|
||||
const img = Array{RGB{Float64}}(h, w)
|
||||
const maxIter = 30
|
||||
|
||||
for x in 1:w
|
||||
for y in 1:h
|
||||
i = maxIter
|
||||
const c = Complex(
|
||||
(2*x - w) / (w * zoom) + moveX,
|
||||
(2*y - h) / (h * zoom) + moveY
|
||||
)
|
||||
z = c
|
||||
while abs(z) < 2 && (i -= 1) > 0
|
||||
z = z^2 + c
|
||||
end
|
||||
const r,g,b = hsv2rgb(i / maxIter * 360, 1, i / maxIter)
|
||||
img[y,x] = RGB{Float64}(r, g, b)
|
||||
end
|
||||
end
|
||||
|
||||
save("mandelbrot_set.png", img)
|
||||
end
|
||||
|
||||
mandelbrot()
|
||||
45
Task/Mandelbrot-set/Kotlin/mandelbrot-set.kotlin
Normal file
45
Task/Mandelbrot-set/Kotlin/mandelbrot-set.kotlin
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
// version 1.1.2
|
||||
|
||||
import java.awt.Graphics
|
||||
import java.awt.image.BufferedImage
|
||||
import javax.swing.JFrame
|
||||
|
||||
class Mandelbrot: JFrame("Mandelbrot Set") {
|
||||
companion object {
|
||||
private const val MAX_ITER = 570
|
||||
private const val ZOOM = 150.0
|
||||
}
|
||||
|
||||
private val img: BufferedImage
|
||||
|
||||
init {
|
||||
setBounds(100, 100, 800, 600)
|
||||
isResizable = false
|
||||
defaultCloseOperation = EXIT_ON_CLOSE
|
||||
img = BufferedImage(width, height, BufferedImage.TYPE_INT_RGB)
|
||||
for (y in 0 until height) {
|
||||
for (x in 0 until width) {
|
||||
var zx = 0.0
|
||||
var zy = 0.0
|
||||
val cX = (x - 400) / ZOOM
|
||||
val cY = (y - 300) / ZOOM
|
||||
var iter = MAX_ITER
|
||||
while (zx * zx + zy * zy < 4.0 && iter > 0) {
|
||||
val tmp = zx * zx - zy * zy + cX
|
||||
zy = 2.0 * zx * zy + cY
|
||||
zx = tmp
|
||||
iter--
|
||||
}
|
||||
img.setRGB(x, y, iter or (iter shl 7))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun paint(g: Graphics) {
|
||||
g.drawImage(img, 0, 0, this)
|
||||
}
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
Mandelbrot().isVisible = true
|
||||
}
|
||||
|
|
@ -1,55 +0,0 @@
|
|||
nomainwin
|
||||
|
||||
WindowWidth =440
|
||||
WindowHeight =460
|
||||
|
||||
open "Mandelbrot Set" for graphics_nsb_nf as #w
|
||||
|
||||
#w "trapclose [quit]"
|
||||
#w "down"
|
||||
|
||||
for x0 = -2 to 1 step .0033
|
||||
for y0 = -1.5 to 1.5 step .0075
|
||||
x = 0
|
||||
y = 0
|
||||
|
||||
iteration = 0
|
||||
maxIteration = 255
|
||||
|
||||
while ( ( x *x +y *y) <=4) and ( iteration <maxIteration)
|
||||
xtemp =x *x -y *y +x0
|
||||
y =2 *x *y +y0
|
||||
x = xtemp
|
||||
iteration = iteration + 1
|
||||
wend
|
||||
|
||||
if iteration <>maxIteration then
|
||||
c =iteration
|
||||
else
|
||||
c =0
|
||||
end if
|
||||
|
||||
call pSet x0, y0, c
|
||||
scan
|
||||
next
|
||||
next
|
||||
|
||||
#w "flush"
|
||||
|
||||
wait
|
||||
|
||||
sub pSet x, y, c
|
||||
xScreen = 10 +( x +2) /3 *400
|
||||
yScreen = 10 +( y +1.5) /3 *400
|
||||
if c =0 then
|
||||
col$ ="red"
|
||||
else
|
||||
if c mod 2 =1 then col$ ="lightgray" else col$ ="white"
|
||||
end if
|
||||
#w "color "; col$
|
||||
#w "set "; xScreen; " "; yScreen
|
||||
end sub
|
||||
|
||||
[quit]
|
||||
close #w
|
||||
end
|
||||
68
Task/Mandelbrot-set/Lua/mandelbrot-set.lua
Normal file
68
Task/Mandelbrot-set/Lua/mandelbrot-set.lua
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
local maxIterations = 250
|
||||
local minX, maxX, minY, maxY = -2.5, 2.5, -2.5, 2.5
|
||||
local miX, mxX, miY, mxY
|
||||
function remap( x, t1, t2, s1, s2 )
|
||||
local f = ( x - t1 ) / ( t2 - t1 )
|
||||
local g = f * ( s2 - s1 ) + s1
|
||||
return g;
|
||||
end
|
||||
function drawMandelbrot()
|
||||
local pts, a, as, za, b, bs, zb, cnt, clr = {}
|
||||
for j = 0, hei - 1 do
|
||||
for i = 0, wid - 1 do
|
||||
a = remap( i, 0, wid, minX, maxX )
|
||||
b = remap( j, 0, hei, minY, maxY )
|
||||
cnt = 0; za = a; zb = b
|
||||
while( cnt < maxIterations ) do
|
||||
as = a * a - b * b; bs = 2 * a * b
|
||||
a = za + as; b = zb + bs
|
||||
if math.abs( a ) + math.abs( b ) > 16 then break end
|
||||
cnt = cnt + 1
|
||||
end
|
||||
if cnt == maxIterations then clr = 0
|
||||
else clr = remap( cnt, 0, maxIterations, 0, 255 )
|
||||
end
|
||||
pts[1] = { i, j, clr, clr, 0, 255 }
|
||||
love.graphics.points( pts )
|
||||
end
|
||||
end
|
||||
end
|
||||
function startFractal()
|
||||
love.graphics.setCanvas( canvas ); love.graphics.clear()
|
||||
love.graphics.setColor( 255, 255, 255 )
|
||||
drawMandelbrot(); love.graphics.setCanvas()
|
||||
end
|
||||
function love.load()
|
||||
wid, hei = love.graphics.getWidth(), love.graphics.getHeight()
|
||||
canvas = love.graphics.newCanvas( wid, hei )
|
||||
startFractal()
|
||||
end
|
||||
function love.mousepressed( x, y, button, istouch )
|
||||
if button == 1 then
|
||||
startDrag = true; miX = x; miY = y
|
||||
else
|
||||
minX = -2.5; maxX = 2.5; minY = minX; maxY = maxX
|
||||
startFractal()
|
||||
startDrag = false
|
||||
end
|
||||
end
|
||||
function love.mousereleased( x, y, button, istouch )
|
||||
if startDrag then
|
||||
local l
|
||||
if x > miX then mxX = x
|
||||
else l = x; mxX = miX; miX = l
|
||||
end
|
||||
if y > miY then mxY = y
|
||||
else l = y; mxY = miY; miY = l
|
||||
end
|
||||
miX = remap( miX, 0, wid, minX, maxX )
|
||||
mxX = remap( mxX, 0, wid, minX, maxX )
|
||||
miY = remap( miY, 0, hei, minY, maxY )
|
||||
mxY = remap( mxY, 0, hei, minY, maxY )
|
||||
minX = miX; maxX = mxX; minY = miY; maxY = mxY
|
||||
startFractal()
|
||||
end
|
||||
end
|
||||
function love.draw()
|
||||
love.graphics.draw( canvas )
|
||||
end
|
||||
|
|
@ -1,10 +1,10 @@
|
|||
import complex
|
||||
|
||||
proc mandelbrot(a): Complex =
|
||||
proc mandelbrot(a: Complex): Complex =
|
||||
for i in 0 .. <50:
|
||||
result = result * result + a
|
||||
|
||||
iterator stepIt(start, step, iterations) =
|
||||
iterator stepIt(start, step: float, iterations: int): auto =
|
||||
for i in 0 .. iterations:
|
||||
yield start + float(i) * step
|
||||
|
||||
|
|
|
|||
7
Task/Mandelbrot-set/PARI-GP/mandelbrot-set.pari
Normal file
7
Task/Mandelbrot-set/PARI-GP/mandelbrot-set.pari
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
mandelbrot() =
|
||||
{
|
||||
forstep(y=-1, 1, 0.05,
|
||||
forstep(x=-2, 0.5, 0.0315,
|
||||
print1(((c)->my(z=c);for(i=1,20,z=z*z+c;if(abs(z)>2,return(" ")));"#")(x+y*I)));
|
||||
print());
|
||||
}
|
||||
18
Task/Mandelbrot-set/SPL/mandelbrot-set.spl
Normal file
18
Task/Mandelbrot-set/SPL/mandelbrot-set.spl
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
w,h = #.scrsize()
|
||||
sfx = -2.5; sfy = -2*h/w; fs = 4/w
|
||||
#.aaoff()
|
||||
> y, 1...h
|
||||
> x, 1...w
|
||||
fx = sfx + x*fs; fy = sfy + y*fs
|
||||
#.drawpoint(x,y,color(fx,fy):3)
|
||||
<
|
||||
<
|
||||
color(x,y)=
|
||||
zr = x; zi = y; n = 0; maxn = 150
|
||||
> zr*zr+zi*zi<4 & n<maxn
|
||||
zrn = zr*zr-zi*zi+x; zin = 2*zr*zi+y
|
||||
zr = zrn; zi = zin; n += 1
|
||||
<
|
||||
? n=maxn, <= 0,0,0
|
||||
<= #.hsv2rgb(n/maxn*360,1,1):3
|
||||
.
|
||||
|
|
@ -1,14 +1,14 @@
|
|||
func mandelbrot(z) {
|
||||
var c = z;
|
||||
{ z = (z*z + c);
|
||||
z.abs > 2 && return true;
|
||||
} * 20;
|
||||
return false;
|
||||
var c = z
|
||||
{ z = (z*z + c)
|
||||
z.abs > 2 && return true
|
||||
} * 20
|
||||
return false
|
||||
}
|
||||
|
||||
1 ^.. (-1, 0.05) -> each { |y|
|
||||
-2 ..^ (0.5, 0.0315) -> each { |x|
|
||||
print(mandelbrot(y.i + x) ? ' ' : '#');
|
||||
|
||||
for y range(1, -1, -0.05) {
|
||||
for x in range(-2, 0.5, 0.0315) {
|
||||
print(mandelbrot(x + y.i) ? ' ' : '#')
|
||||
}
|
||||
print "\n";
|
||||
print "\n"
|
||||
}
|
||||
|
|
|
|||
20
Task/Mandelbrot-set/Zkl/mandelbrot-set.zkl
Normal file
20
Task/Mandelbrot-set/Zkl/mandelbrot-set.zkl
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
fcn mandelbrot{ // lord this is slooooow
|
||||
bitmap:=PPM(640,480);
|
||||
foreach y,x in ([0..479],[0..639]){
|
||||
cx:=(x.toFloat()/640 - 0.5)*4.0; //range: -2.0 to +2.0
|
||||
cy:=((y-240).toFloat()/240.0)*1.5; //range: -1.5 to +1.5
|
||||
cnt:=0; zx:=0.0; zy:=0.0;
|
||||
do(1000){
|
||||
if(zx*zx + zy*zy > 2.0){ //z heads toward infinity
|
||||
//set color of pixel to rate it approaches infinity
|
||||
bitmap[x,y]=cnt.shiftLeft(21) + cnt.shiftLeft(10) + cnt*8;
|
||||
break;
|
||||
}
|
||||
temp:=zx*zy;
|
||||
zx=zx*zx - zy*zy + cx; //calculate next iteration of z
|
||||
zy=2.0*temp + cy;
|
||||
cnt+=1;
|
||||
}
|
||||
}
|
||||
bitmap.write(File("foo.ppm","wb"));
|
||||
}();
|
||||
Loading…
Add table
Add a link
Reference in a new issue