Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
40
Task/Mandelbrot-set/Ada/mandelbrot-set-1.adb
Normal file
40
Task/Mandelbrot-set/Ada/mandelbrot-set-1.adb
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
with Lumen.Binary;
|
||||
package body Mandelbrot is
|
||||
function Create_Image (Width, Height : Natural) return Lumen.Image.Descriptor is
|
||||
use type Lumen.Binary.Byte;
|
||||
Result : Lumen.Image.Descriptor;
|
||||
X0, Y0 : Float;
|
||||
X, Y, Xtemp : Float;
|
||||
Iteration : Float;
|
||||
Max_Iteration : constant Float := 1000.0;
|
||||
Color : Lumen.Binary.Byte;
|
||||
begin
|
||||
Result.Width := Width;
|
||||
Result.Height := Height;
|
||||
Result.Complete := True;
|
||||
Result.Values := new Lumen.Image.Pixel_Matrix (1 .. Width, 1 .. Height);
|
||||
for Screen_X in 1 .. Width loop
|
||||
for Screen_Y in 1 .. Height loop
|
||||
X0 := -2.5 + (3.5 / Float (Width) * Float (Screen_X));
|
||||
Y0 := -1.0 + (2.0 / Float (Height) * Float (Screen_Y));
|
||||
X := 0.0;
|
||||
Y := 0.0;
|
||||
Iteration := 0.0;
|
||||
while X * X + Y * Y <= 4.0 and then Iteration < Max_Iteration loop
|
||||
Xtemp := X * X - Y * Y + X0;
|
||||
Y := 2.0 * X * Y + Y0;
|
||||
X := Xtemp;
|
||||
Iteration := Iteration + 1.0;
|
||||
end loop;
|
||||
if Iteration = Max_Iteration then
|
||||
Color := 255;
|
||||
else
|
||||
Color := 0;
|
||||
end if;
|
||||
Result.Values (Screen_X, Screen_Y) := (R => Color, G => Color, B => Color, A => 0);
|
||||
end loop;
|
||||
end loop;
|
||||
return Result;
|
||||
end Create_Image;
|
||||
|
||||
end Mandelbrot;
|
||||
7
Task/Mandelbrot-set/Ada/mandelbrot-set-2.adb
Normal file
7
Task/Mandelbrot-set/Ada/mandelbrot-set-2.adb
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
with Lumen.Image;
|
||||
|
||||
package Mandelbrot is
|
||||
|
||||
function Create_Image (Width, Height : Natural) return Lumen.Image.Descriptor;
|
||||
|
||||
end Mandelbrot;
|
||||
155
Task/Mandelbrot-set/Ada/mandelbrot-set-3.adb
Normal file
155
Task/Mandelbrot-set/Ada/mandelbrot-set-3.adb
Normal file
|
|
@ -0,0 +1,155 @@
|
|||
with System.Address_To_Access_Conversions;
|
||||
with Lumen.Window;
|
||||
with Lumen.Image;
|
||||
with Lumen.Events;
|
||||
with GL;
|
||||
with Mandelbrot;
|
||||
|
||||
procedure Test_Mandelbrot is
|
||||
|
||||
Program_End : exception;
|
||||
|
||||
Win : Lumen.Window.Handle;
|
||||
Image : Lumen.Image.Descriptor;
|
||||
Tx_Name : aliased GL.GLuint;
|
||||
Wide, High : Natural := 400;
|
||||
|
||||
-- Create a texture and bind a 2D image to it
|
||||
procedure Create_Texture is
|
||||
use GL;
|
||||
|
||||
package GLB is new System.Address_To_Access_Conversions (GLubyte);
|
||||
|
||||
IP : GLpointer;
|
||||
begin -- Create_Texture
|
||||
-- Allocate a texture name
|
||||
glGenTextures (1, Tx_Name'Unchecked_Access);
|
||||
|
||||
-- Bind texture operations to the newly-created texture name
|
||||
glBindTexture (GL_TEXTURE_2D, Tx_Name);
|
||||
|
||||
-- Select modulate to mix texture with color for shading
|
||||
glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
|
||||
|
||||
-- Wrap textures at both edges
|
||||
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
||||
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
||||
|
||||
-- How the texture behaves when minified and magnified
|
||||
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
|
||||
-- Create a pointer to the image. This sort of horror show is going to
|
||||
-- be disappearing once Lumen includes its own OpenGL bindings.
|
||||
IP := GLB.To_Pointer (Image.Values.all'Address).all'Unchecked_Access;
|
||||
|
||||
-- Build our texture from the image we loaded earlier
|
||||
glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, GLsizei (Image.Width), GLsizei (Image.Height), 0,
|
||||
GL_RGBA, GL_UNSIGNED_BYTE, IP);
|
||||
end Create_Texture;
|
||||
|
||||
-- Set or reset the window view parameters
|
||||
procedure Set_View (W, H : in Natural) is
|
||||
use GL;
|
||||
begin -- Set_View
|
||||
GL.glEnable (GL.GL_TEXTURE_2D);
|
||||
glClearColor (0.8, 0.8, 0.8, 1.0);
|
||||
|
||||
glMatrixMode (GL_PROJECTION);
|
||||
glLoadIdentity;
|
||||
glViewport (0, 0, GLsizei (W), GLsizei (H));
|
||||
glOrtho (0.0, GLdouble (W), GLdouble (H), 0.0, -1.0, 1.0);
|
||||
|
||||
glMatrixMode (GL_MODELVIEW);
|
||||
glLoadIdentity;
|
||||
end Set_View;
|
||||
|
||||
-- Draw our scene
|
||||
procedure Draw is
|
||||
use GL;
|
||||
begin -- Draw
|
||||
-- clear the screen
|
||||
glClear (GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
|
||||
GL.glBindTexture (GL.GL_TEXTURE_2D, Tx_Name);
|
||||
|
||||
-- fill with a single textured quad
|
||||
glBegin (GL_QUADS);
|
||||
begin
|
||||
glTexCoord2f (1.0, 0.0);
|
||||
glVertex2i (GLint (Wide), 0);
|
||||
|
||||
glTexCoord2f (0.0, 0.0);
|
||||
glVertex2i (0, 0);
|
||||
|
||||
glTexCoord2f (0.0, 1.0);
|
||||
glVertex2i (0, GLint (High));
|
||||
|
||||
glTexCoord2f (1.0, 1.0);
|
||||
glVertex2i (GLint (Wide), GLint (High));
|
||||
end;
|
||||
glEnd;
|
||||
|
||||
-- flush rendering pipeline
|
||||
glFlush;
|
||||
|
||||
-- Now show it
|
||||
Lumen.Window.Swap (Win);
|
||||
end Draw;
|
||||
|
||||
-- Simple event handler routine for keypresses and close-window events
|
||||
procedure Quit_Handler (Event : in Lumen.Events.Event_Data) is
|
||||
begin -- Quit_Handler
|
||||
raise Program_End;
|
||||
end Quit_Handler;
|
||||
|
||||
-- Simple event handler routine for Exposed events
|
||||
procedure Expose_Handler (Event : in Lumen.Events.Event_Data) is
|
||||
pragma Unreferenced (Event);
|
||||
begin -- Expose_Handler
|
||||
Draw;
|
||||
end Expose_Handler;
|
||||
|
||||
-- Simple event handler routine for Resized events
|
||||
procedure Resize_Handler (Event : in Lumen.Events.Event_Data) is
|
||||
begin -- Resize_Handler
|
||||
Wide := Event.Resize_Data.Width;
|
||||
High := Event.Resize_Data.Height;
|
||||
Set_View (Wide, High);
|
||||
-- Image := Mandelbrot.Create_Image (Width => Wide, Height => High);
|
||||
-- Create_Texture;
|
||||
Draw;
|
||||
end Resize_Handler;
|
||||
|
||||
begin
|
||||
-- Create Lumen window, accepting most defaults; turn double buffering off
|
||||
-- for simplicity
|
||||
Lumen.Window.Create (Win => Win,
|
||||
Name => "Mandelbrot fractal",
|
||||
Width => Wide,
|
||||
Height => High,
|
||||
Events => (Lumen.Window.Want_Exposure => True,
|
||||
Lumen.Window.Want_Key_Press => True,
|
||||
others => False));
|
||||
|
||||
-- Set up the viewport and scene parameters
|
||||
Set_View (Wide, High);
|
||||
|
||||
-- Now create the texture and set up to use it
|
||||
Image := Mandelbrot.Create_Image (Width => Wide, Height => High);
|
||||
Create_Texture;
|
||||
|
||||
-- Enter the event loop
|
||||
declare
|
||||
use Lumen.Events;
|
||||
begin
|
||||
Select_Events (Win => Win,
|
||||
Calls => (Key_Press => Quit_Handler'Unrestricted_Access,
|
||||
Exposed => Expose_Handler'Unrestricted_Access,
|
||||
Resized => Resize_Handler'Unrestricted_Access,
|
||||
Close_Window => Quit_Handler'Unrestricted_Access,
|
||||
others => No_Callback));
|
||||
end;
|
||||
exception
|
||||
when Program_End =>
|
||||
null;
|
||||
end Test_Mandelbrot;
|
||||
114
Task/Mandelbrot-set/C++/mandelbrot-set-3.cpp
Normal file
114
Task/Mandelbrot-set/C++/mandelbrot-set-3.cpp
Normal file
|
|
@ -0,0 +1,114 @@
|
|||
/*
|
||||
* Mandelbrot Set High-Fidelity Renderer
|
||||
*
|
||||
* Key Features:
|
||||
* - 80-bit Extended Precision (long double)
|
||||
* - 8x8 Super-Sampling Anti-Aliasing (64 samples per pixel)
|
||||
* - OpenMP Parallel Processing
|
||||
* - Direct RGB-Space Integration (24-bit TrueColor)
|
||||
*
|
||||
* Original project and full source code:
|
||||
* https://github.com/Divetoxx/Mandelbrot
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <atomic>
|
||||
#include <omp.h>
|
||||
using namespace std;
|
||||
const double PI = 3.14159265358979323846;
|
||||
#pragma pack(push, 1)
|
||||
struct BMPHeader {
|
||||
uint16_t type{0x4D42};
|
||||
uint32_t size{0};
|
||||
uint16_t reserved1{0};
|
||||
uint16_t reserved2{0};
|
||||
uint32_t offBits{54};
|
||||
uint32_t structSize{40};
|
||||
int32_t width{0};
|
||||
int32_t height{0};
|
||||
uint16_t planes{1};
|
||||
uint16_t bitCount{24};
|
||||
uint32_t compression{0};
|
||||
uint32_t sizeImage{0};
|
||||
int32_t xpelsPerMeter{2834};
|
||||
int32_t ypelsPerMeter{2834};
|
||||
uint32_t clrUsed{0};
|
||||
uint32_t clrImportant{0};
|
||||
};
|
||||
#pragma pack(pop)
|
||||
int main() {
|
||||
long double absc, ordi, size_val;
|
||||
absc = -1.39966699645936; ordi = 0.0005429083913; size_val = 0.000000000000036;
|
||||
const int horiz = 1920;
|
||||
const int vert = 1920;
|
||||
const int rowSize = (horiz * 3 + 3) & ~3;
|
||||
BMPHeader h;
|
||||
h.width = horiz;
|
||||
h.height = vert;
|
||||
h.sizeImage = rowSize * vert;
|
||||
h.size = h.sizeImage + 54;
|
||||
uint8_t pal[256][3];
|
||||
for (int a = 0; a < 255; ++a) {
|
||||
pal[a][0] = (uint8_t)round(127 + 127 * cos(2 * PI * a / 255.0));
|
||||
pal[a][1] = (uint8_t)round(127 + 127 * sin(2 * PI * a / 255.0));
|
||||
pal[a][2] = (uint8_t)round(127 + 127 * sin(2 * PI * a / 255.0));
|
||||
}
|
||||
pal[255][0] = 255; pal[255][1] = 255; pal[255][2] = 255;
|
||||
long double step = size_val / (horiz << 3);
|
||||
long double absc2 = absc - step * ((horiz << 3) - 1) / 2.0;
|
||||
long double ordi2 = ordi - step * ((vert << 3) - 1) / 2.0;
|
||||
vector<uint8_t> allData(h.sizeImage, 0);
|
||||
atomic<int> linesLeft{vert};
|
||||
cout << "Starting calculation on " << omp_get_max_threads() << " threads..." << endl;
|
||||
#pragma omp parallel for schedule(dynamic)
|
||||
for (int b = 0; b < vert; ++b) {
|
||||
int nn = b << 3;
|
||||
for (int a = 0; a < horiz; ++a) {
|
||||
int mm = a << 3;
|
||||
long z_sum[3] = {0, 0, 0};
|
||||
for (int j = 0; j < 8; ++j) {
|
||||
long double n_coord = ordi2 + (nn + j) * step;
|
||||
for (int i = 0; i < 8; ++i) {
|
||||
long double m_coord = absc2 + (mm + i) * step;
|
||||
long double c_re = m_coord, d_im = n_coord;
|
||||
int t = 50000;
|
||||
long double cc, dd;
|
||||
do {
|
||||
cc = c_re * c_re;
|
||||
dd = d_im * d_im;
|
||||
d_im = 2 * c_re * d_im + n_coord;
|
||||
c_re = cc - dd + m_coord;
|
||||
t--;
|
||||
} while (t > 0 && (cc + dd <= 10000.0));
|
||||
int colorIdx = (t == 0) ? 255 : (t % 255);
|
||||
z_sum[0] += pal[colorIdx][0];
|
||||
z_sum[1] += pal[colorIdx][1];
|
||||
z_sum[2] += pal[colorIdx][2];
|
||||
}
|
||||
}
|
||||
int pixelPos = b * rowSize + a * 3;
|
||||
allData[pixelPos + 0] = (uint8_t)(z_sum[0] >> 6);
|
||||
allData[pixelPos + 1] = (uint8_t)(z_sum[1] >> 6);
|
||||
allData[pixelPos + 2] = (uint8_t)(z_sum[2] >> 6);
|
||||
}
|
||||
int current = --linesLeft;
|
||||
if (current % 10 == 0 || current < 10) {
|
||||
#pragma omp critical
|
||||
{
|
||||
cout << "Lines remaining: " << current << " \r" << flush;
|
||||
}
|
||||
}
|
||||
}
|
||||
ofstream f("Mandelbrot.bmp", ios::binary);
|
||||
if (f.is_open()) {
|
||||
f.write(reinterpret_cast<char*>(&h), 54);
|
||||
f.write(reinterpret_cast<char*>(allData.data()), allData.size());
|
||||
f.close();
|
||||
cout << "\nFinished! Mandelbrot.bmp saved." << endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
51
Task/Mandelbrot-set/COBOL/mandelbrot-set.cob
Normal file
51
Task/Mandelbrot-set/COBOL/mandelbrot-set.cob
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
IDENTIFICATION DIVISION.
|
||||
PROGRAM-ID. MANDELBROT-SET-PROGRAM.
|
||||
DATA DIVISION.
|
||||
WORKING-STORAGE SECTION.
|
||||
01 COMPLEX-ARITHMETIC.
|
||||
05 X PIC S9V9(9).
|
||||
05 Y PIC S9V9(9).
|
||||
05 X-A PIC S9V9(6).
|
||||
05 X-B PIC S9V9(6).
|
||||
05 Y-A PIC S9V9(6).
|
||||
05 X-A-SQUARED PIC S9V9(6).
|
||||
05 Y-A-SQUARED PIC S9V9(6).
|
||||
05 SUM-OF-SQUARES PIC S9V9(6).
|
||||
05 ROOT PIC S9V9(6).
|
||||
01 LOOP-COUNTERS.
|
||||
05 I PIC 99.
|
||||
05 J PIC 99.
|
||||
05 K PIC 999.
|
||||
77 PLOT-CHARACTER PIC X.
|
||||
PROCEDURE DIVISION.
|
||||
CONTROL-PARAGRAPH.
|
||||
PERFORM OUTER-LOOP-PARAGRAPH
|
||||
VARYING I FROM 1 BY 1 UNTIL I IS GREATER THAN 24.
|
||||
STOP RUN.
|
||||
OUTER-LOOP-PARAGRAPH.
|
||||
PERFORM INNER-LOOP-PARAGRAPH
|
||||
VARYING J FROM 1 BY 1 UNTIL J IS GREATER THAN 64.
|
||||
DISPLAY ''.
|
||||
INNER-LOOP-PARAGRAPH.
|
||||
MOVE SPACE TO PLOT-CHARACTER.
|
||||
MOVE ZERO TO X-A.
|
||||
MOVE ZERO TO Y-A.
|
||||
MULTIPLY J BY 0.0390625 GIVING X.
|
||||
SUBTRACT 1.5 FROM X.
|
||||
MULTIPLY I BY 0.083333333 GIVING Y.
|
||||
SUBTRACT 1 FROM Y.
|
||||
PERFORM ITERATION-PARAGRAPH VARYING K FROM 1 BY 1
|
||||
UNTIL K IS GREATER THAN 100 OR PLOT-CHARACTER IS EQUAL TO '#'.
|
||||
DISPLAY PLOT-CHARACTER WITH NO ADVANCING.
|
||||
ITERATION-PARAGRAPH.
|
||||
MULTIPLY X-A BY X-A GIVING X-A-SQUARED.
|
||||
MULTIPLY Y-A BY Y-A GIVING Y-A-SQUARED.
|
||||
SUBTRACT Y-A-SQUARED FROM X-A-SQUARED GIVING X-B.
|
||||
ADD X TO X-B.
|
||||
MULTIPLY X-A BY Y-A GIVING Y-A.
|
||||
MULTIPLY Y-A BY 2 GIVING Y-A.
|
||||
SUBTRACT Y FROM Y-A.
|
||||
MOVE X-B TO X-A.
|
||||
ADD X-A-SQUARED TO Y-A-SQUARED GIVING SUM-OF-SQUARES.
|
||||
MOVE FUNCTION SQRT (SUM-OF-SQUARES) TO ROOT.
|
||||
IF ROOT IS GREATER THAN 2 THEN MOVE '#' TO PLOT-CHARACTER.
|
||||
42
Task/Mandelbrot-set/Emacs-Lisp/mandelbrot-set-1.el
Normal file
42
Task/Mandelbrot-set/Emacs-Lisp/mandelbrot-set-1.el
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
; === Mandelbrot ============================================
|
||||
|
||||
(setq mandel-size (cons 76 34))
|
||||
(setq xmin -2)
|
||||
(setq xmax .5)
|
||||
(setq ymin -1.2)
|
||||
(setq ymax 1.2)
|
||||
(setq max-iter 20)
|
||||
|
||||
(defun mandel-iter-point (x y)
|
||||
"Run the actual iteration for each point."
|
||||
(let ((xp 0)
|
||||
(yp 0)
|
||||
(it 0)
|
||||
(xt 0))
|
||||
(while (and (< (+ (* xp xp) (* yp yp)) 4) (< it max-iter))
|
||||
(setq xt (+ (* xp xp) (* -1 yp yp) x))
|
||||
(setq yp (+ (* 2 xp yp) y))
|
||||
(setq xp xt)
|
||||
(setq it (1+ it)))
|
||||
it))
|
||||
|
||||
(defun mandel-iter (p)
|
||||
"Return string for point based on whether inside/outside the set."
|
||||
(let ((it (mandel-iter-point (car p) (cdr p))))
|
||||
(if (= it max-iter) "*" "-")))
|
||||
|
||||
(defun mandel-pos (x y)
|
||||
"Convert screen coordinates to input coordinates."
|
||||
(let ((xp (+ xmin (* (- xmax xmin) (/ (float x) (car mandel-size)))))
|
||||
(yp (+ ymin (* (- ymax ymin) (/ (float y) (cdr mandel-size))))))
|
||||
(cons xp yp)))
|
||||
|
||||
(defun mandel ()
|
||||
"Plot the Mandelbrot set."
|
||||
(dotimes (y (cdr mandel-size))
|
||||
(dotimes (x (car mandel-size))
|
||||
(if (= x 0)
|
||||
(insert(format "\n%s" (mandel-iter (mandel-pos x y))))
|
||||
(insert(format "%s" (mandel-iter (mandel-pos x y))))))))
|
||||
|
||||
(mandel)
|
||||
54
Task/Mandelbrot-set/Emacs-Lisp/mandelbrot-set-2.el
Normal file
54
Task/Mandelbrot-set/Emacs-Lisp/mandelbrot-set-2.el
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
; === Graphical Mandelbrot ============================================
|
||||
|
||||
(setq mandel-size (cons 320 300))
|
||||
(setq xmin -2)
|
||||
(setq xmax .5)
|
||||
(setq ymin -1.2)
|
||||
(setq ymax 1.2)
|
||||
(setq max-iter 20)
|
||||
|
||||
(defun mandel-iter-point (x y)
|
||||
"Run the actual iteration for each point."
|
||||
(let ((xp 0)
|
||||
(yp 0)
|
||||
(it 0)
|
||||
(xt 0))
|
||||
(while (and (< (+ (* xp xp) (* yp yp)) 4) (< it max-iter))
|
||||
(setq xt (+ (* xp xp) (* -1 yp yp) x))
|
||||
(setq yp (+ (* 2 xp yp) y))
|
||||
(setq xp xt)
|
||||
(setq it (1+ it)))
|
||||
it))
|
||||
|
||||
(defun mandel-iter (p)
|
||||
"Return string for point based on whether inside/outside the set."
|
||||
(let ((it (mandel-iter-point (car p) (cdr p))))
|
||||
(if (= it max-iter) "*" (if (cl-oddp it) "+" "-"))))
|
||||
|
||||
(defun mandel-pos (x y)
|
||||
"Convert screen coordinates to input coordinates."
|
||||
(let ((xp (+ xmin (* (- xmax xmin) (/ (float x) (car mandel-size)))))
|
||||
(yp (+ ymin (* (- ymax ymin) (/ (float y) (cdr mandel-size))))))
|
||||
(cons xp yp)))
|
||||
|
||||
(defun string-to-image (str)
|
||||
"Convert image data string to XPM image."
|
||||
(create-image (concat (format "/* XPM */
|
||||
static char * mandel[] = {
|
||||
\"%i %i 3 1\",
|
||||
\"+ c #ff0000\",
|
||||
\"- c #0000ff\",
|
||||
\"* c #000000\"," (car mandel-size) (cdr mandel-size))
|
||||
str "};") 'xpm t))
|
||||
|
||||
(defun mandel-pic ()
|
||||
"Plot the Mandelbrot set."
|
||||
(setq all "")
|
||||
(dotimes (y (cdr mandel-size))
|
||||
(setq line "")
|
||||
(dotimes (x (car mandel-size))
|
||||
(setq line (concat line (mandel-iter (mandel-pos x y)))))
|
||||
(setq all (concat all "\"" line "\",\n")))
|
||||
(insert-image (string-to-image all)))
|
||||
|
||||
(mandel-pic)
|
||||
|
|
@ -3,7 +3,7 @@ let getMandelbrotValues width height maxIter ((xMin,xMax),(yMin,yMax)) =
|
|||
let next (zr,zi) = (cr + (zr * zr - zi * zi)), (ci + (zr * zi + zi * zr))
|
||||
let rec loop = function
|
||||
| step,_ when step=maxIter->0
|
||||
| step,(zr,zi) when ((zr * zr + zi * zi) > 2.0) -> step
|
||||
| step,(zr,zi) when ((zr * zr + zi * zi) > 4.0) -> step
|
||||
| step,z -> loop ((step + 1), (next z))
|
||||
loop (0,(0.0, 0.0))
|
||||
let forPos =
|
||||
|
|
|
|||
1
Task/Mandelbrot-set/Haxe/mandelbrot-set-1.hx
Normal file
1
Task/Mandelbrot-set/Haxe/mandelbrot-set-1.hx
Normal file
|
|
@ -0,0 +1 @@
|
|||
haxe -swf mandelbrot.swf -main Mandelbrot
|
||||
49
Task/Mandelbrot-set/Haxe/mandelbrot-set-2.hx
Normal file
49
Task/Mandelbrot-set/Haxe/mandelbrot-set-2.hx
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
class Mandelbrot extends flash.display.Sprite
|
||||
{
|
||||
inline static var MAX_ITER = 255;
|
||||
|
||||
public static function main() {
|
||||
var w = flash.Lib.current.stage.stageWidth;
|
||||
var h = flash.Lib.current.stage.stageHeight;
|
||||
var mandelbrot = new Mandelbrot(w, h);
|
||||
flash.Lib.current.stage.addChild(mandelbrot);
|
||||
mandelbrot.drawMandelbrot();
|
||||
}
|
||||
|
||||
var image:flash.display.BitmapData;
|
||||
public function new(width, height) {
|
||||
super();
|
||||
var bitmap:flash.display.Bitmap;
|
||||
image = new flash.display.BitmapData(width, height, false);
|
||||
bitmap = new flash.display.Bitmap(image);
|
||||
this.addChild(bitmap);
|
||||
}
|
||||
|
||||
public function drawMandelbrot() {
|
||||
image.lock();
|
||||
var step_x = 3.0 / (image.width-1);
|
||||
var step_y = 2.0 / (image.height-1);
|
||||
for (i in 0...image.height) {
|
||||
var ci = i * step_y - 1.0;
|
||||
for (j in 0...image.width) {
|
||||
var k = 0;
|
||||
var zr = 0.0;
|
||||
var zi = 0.0;
|
||||
var cr = j * step_x - 2.0;
|
||||
while (k <= MAX_ITER && (zr*zr + zi*zi) <= 4) {
|
||||
var temp = zr*zr - zi*zi + cr;
|
||||
zi = 2*zr*zi + ci;
|
||||
zr = temp;
|
||||
k ++;
|
||||
}
|
||||
paint(j, i, k);
|
||||
}
|
||||
}
|
||||
image.unlock();
|
||||
}
|
||||
|
||||
inline function paint(x, y, iter) {
|
||||
var color = iter > MAX_ITER? 0 : iter * 0x100;
|
||||
image.setPixel(x, y, color);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,27 +1,27 @@
|
|||
using Plots
|
||||
gr(aspect_ratio=:equal, legend=false, axis=false, ticks=false, dpi=100)
|
||||
gr(aspect_ratio=:equal, legend=false, axis=false, ticks=false)
|
||||
|
||||
d, h = 400, 300 # pixel density (= image width) and image height
|
||||
n, r = 40, 1000 # number of iterations and escape radius (r > 2)
|
||||
d, h = 800, 600 # pixel density (= image width) and image height
|
||||
n, r = 20, 1000 # number of iterations and escape radius (r > 2)
|
||||
|
||||
x = range(-1.0, 1.0, length=d+1)
|
||||
y = range(-h/d, h/d, length=h+1)
|
||||
x = range(-1.0, 1.0, length=d)
|
||||
y = range(-h/d, h/d, length=h)
|
||||
|
||||
C = 2.0 .* (x' .+ y .* im) .- 0.5
|
||||
C = 1.6 .* (x' .+ y .* im) .- 0.6
|
||||
S, Z = zeros(size(C)), zero(C)
|
||||
|
||||
animation = Animation()
|
||||
smoothing = Animation()
|
||||
animation, smoothing = Animation(), Animation()
|
||||
gr(size=(d, h), margins=-8*Plots.px, dpi=100, c=:jet)
|
||||
|
||||
for k in 1:n
|
||||
M = abs.(Z) .< r
|
||||
S[M] = S[M] .+ exp.(.-abs.(Z[M]))
|
||||
Z[M] = Z[M] .^ 2 .+ C[M]
|
||||
heatmap(exp.(.-abs.(Z)), c=:jet)
|
||||
heatmap(exp.(.-abs.(Z)))
|
||||
frame(animation)
|
||||
heatmap(S .+ exp.(.-abs.(Z)), c=:jet)
|
||||
heatmap(S .+ exp.(.-abs.(Z)))
|
||||
frame(smoothing)
|
||||
end
|
||||
|
||||
gif(animation, "Mandelbrot_animation.gif", fps=2)
|
||||
gif(smoothing, "Mandelbrot_smoothing.gif", fps=2)
|
||||
gif(animation, "Mandelbrot_animation.gif", fps=1)
|
||||
gif(smoothing, "Mandelbrot_smoothing.gif", fps=1)
|
||||
|
|
|
|||
31
Task/Mandelbrot-set/Pluto/mandelbrot-set.pluto
Normal file
31
Task/Mandelbrot-set/Pluto/mandelbrot-set.pluto
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
require "bitmap"
|
||||
|
||||
local w = 800
|
||||
local h = 600
|
||||
local bmp = bitmap.of(w, h, color.black, "Mandelbrot_set")
|
||||
|
||||
local max_iters = 570
|
||||
local zoom = 150
|
||||
|
||||
local function mandelbrot()
|
||||
for x = 0, w - 1 do
|
||||
for y = 0, h - 1 do
|
||||
local zx = 0
|
||||
local zy = 0
|
||||
local c_x = (x - 400) / zoom
|
||||
local c_y = (y - 300) / zoom
|
||||
local i = max_iters
|
||||
while zx * zx + zy * zy < 4 and i > 0 do
|
||||
local tmp = zx * zx - zy * zy + c_x
|
||||
zy = 2 * zx * zy + c_y
|
||||
zx = tmp
|
||||
i -= 1
|
||||
end
|
||||
local r = math.round(i * 255 / max_iters)
|
||||
bmp:set(math.round(x), math.round(y), bitmap.rgbColor(r, r, r))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
mandelbrot()
|
||||
bmp:view(false, "", "", true)
|
||||
20
Task/Mandelbrot-set/PowerShell/mandelbrot-set.ps1
Normal file
20
Task/Mandelbrot-set/PowerShell/mandelbrot-set.ps1
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
$x = $y = $i = $j = $r = -16
|
||||
$colors = [Enum]::GetValues([System.ConsoleColor])
|
||||
|
||||
while(($y++) -lt 15)
|
||||
{
|
||||
for($x=0; ($x++) -lt 84; Write-Host " " -BackgroundColor ($colors[$k -band 15]) -NoNewline)
|
||||
{
|
||||
$i = $k = $r = 0
|
||||
|
||||
do
|
||||
{
|
||||
$j = $r * $r - $i * $i -2 + $x / 25
|
||||
$i = 2 * $r * $i + $y / 10
|
||||
$r = $j
|
||||
}
|
||||
while (($j * $j + $i * $i) -lt 11 -band ($k++) -lt 111)
|
||||
}
|
||||
|
||||
Write-Host
|
||||
}
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
import numba
|
||||
import numba.cuda as cuda
|
||||
# import numba.cuda as cuda # import numba.cuda for GPU calculations
|
||||
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
|
|
@ -8,7 +8,7 @@ import decimal as dc # decimal floating point arithmetic with arbitrary precisi
|
|||
dc.getcontext().prec = 80 # set precision to 80 digits (about 256 bits)
|
||||
|
||||
d, h = 100, 2000 # pixel density (= image width) and image height
|
||||
n, r = 80000, 100000.0 # number of iterations and escape radius (r > 2)
|
||||
n, r = 100000, 10000 # number of iterations and escape radius (r > 2)
|
||||
|
||||
a = dc.Decimal("-1.256827152259138864846434197797294538253477389787308085590211144291")
|
||||
b = dc.Decimal(".37933802890364143684096784819544060002129071484943239316486643285025")
|
||||
|
|
@ -24,19 +24,19 @@ for i in range(n + 2):
|
|||
print("The reference sequence diverges within %s iterations." % i)
|
||||
break
|
||||
|
||||
x = np.linspace(0, 2, num=d+1, dtype=np.float64)
|
||||
y = np.linspace(0, 2 * h / d, num=h+1, dtype=np.float64)
|
||||
x = np.linspace(0, 2, num=d+1)
|
||||
y = np.linspace(0, 2 * h / d, num=h+1)
|
||||
|
||||
A, B = np.meshgrid(x * np.pi, y * np.pi)
|
||||
C = (- 8.0) * np.exp((A + B * 1j) * 1j)
|
||||
|
||||
@numba.njit(parallel=True)
|
||||
def iteration_numba(S, C):
|
||||
I = np.zeros(C.shape, dtype=np.intp)
|
||||
I = np.zeros(C.shape, dtype=np.int64)
|
||||
E, Z, dZ = np.zeros_like(C), np.zeros_like(C), np.zeros_like(C)
|
||||
|
||||
def iteration(S, C):
|
||||
I = np.zeros(C.shape, dtype=np.intp)
|
||||
I = np.zeros(C.shape, dtype=np.int64)
|
||||
E, Z, dZ = np.zeros_like(C), np.zeros_like(C), np.zeros_like(C)
|
||||
|
||||
def abs2(z):
|
||||
|
|
@ -70,7 +70,7 @@ def iteration_numba(S, C):
|
|||
return I, E, Z, dZ
|
||||
|
||||
def iteration_numba_cuda(S, C):
|
||||
I = np.zeros(C.shape, dtype=np.intp)
|
||||
I = np.zeros(C.shape, dtype=np.int64)
|
||||
E, Z, dZ = np.zeros_like(C), np.zeros_like(C), np.zeros_like(C)
|
||||
|
||||
@cuda.jit()
|
||||
|
|
@ -106,7 +106,7 @@ def iteration_numba_cuda(S, C):
|
|||
return I.copy_to_host(), E.copy_to_host(), Z.copy_to_host(), dZ.copy_to_host()
|
||||
|
||||
I, E, Z, dZ = iteration_numba(S, C) # use iteration_numba or iteration_numba_cuda
|
||||
D = np.zeros(C.shape, dtype=np.float64)
|
||||
D = np.zeros(C.shape)
|
||||
|
||||
N = abs(Z) > 2 # exterior distance estimation
|
||||
D[N] = np.log(abs(Z[N])) * abs(Z[N]) / abs(dZ[N])
|
||||
|
|
|
|||
|
|
@ -1,16 +1,17 @@
|
|||
import numba
|
||||
# import cupy as cp # import cupy for GPU calculations
|
||||
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
import decimal as dc # decimal floating point arithmetic with arbitrary precision
|
||||
dc.getcontext().prec = 80 # set precision to 80 digits (about 256 bits)
|
||||
dc.getcontext().prec = 40 # set precision to 40 digits (about 128 bits)
|
||||
|
||||
d, h = 1600, 1000 # pixel density (= image width) and image height
|
||||
n, r = 80000, 100000.0 # number of iterations and escape radius (r > 2)
|
||||
n, r = 50000, 10000 # number of iterations and escape radius (r > 2)
|
||||
|
||||
a = dc.Decimal("-1.256827152259138864846434197797294538253477389787308085590211144291")
|
||||
b = dc.Decimal(".37933802890364143684096784819544060002129071484943239316486643285025")
|
||||
a, b = dc.Decimal("-1.39966699645936"), dc.Decimal("0.0005429083913")
|
||||
radius = float("0.000000000000036") / 2 # coordinates by Aokoroko
|
||||
|
||||
S = np.zeros(n + 100, dtype=np.complex128) # 100 iterations are chained
|
||||
u, v = dc.Decimal(0), dc.Decimal(0)
|
||||
|
|
@ -23,94 +24,134 @@ for i in range(n + 100):
|
|||
print("The reference sequence diverges within %s iterations." % i)
|
||||
break
|
||||
|
||||
x = np.linspace(0, 2, num=d+1, dtype=np.float64)
|
||||
y = np.linspace(0, 2 * h / d, num=h+1, dtype=np.float64)
|
||||
x = np.linspace(0, 2, num=d+1)
|
||||
y = np.linspace(0, 2 * h / d, num=h+1)
|
||||
|
||||
A, B = np.meshgrid(x - 1, y - h / d)
|
||||
C = 5.0e-35 * (A + B * 1j)
|
||||
C = radius * (A + B * 1j)
|
||||
|
||||
@numba.njit(parallel=True, fastmath=True)
|
||||
def iteration_numba_bla(S, C):
|
||||
I, J = np.zeros(C.shape, dtype=np.intp), np.zeros(C.shape, dtype=np.complex128)
|
||||
E, Z, dZ = np.zeros_like(C), np.zeros_like(C), np.zeros_like(C)
|
||||
I, J = np.zeros(C.shape, dtype=np.int64), np.zeros(C.shape, dtype=np.complex128)
|
||||
E, Z = np.zeros_like(C), np.zeros_like(C)
|
||||
|
||||
def iteration(S, dS, R, A, B, C):
|
||||
I, J = np.zeros(C.shape, dtype=np.intp), np.zeros(C.shape, dtype=np.complex128)
|
||||
E, Z, dZ = np.zeros_like(C), np.zeros_like(C), np.zeros_like(C)
|
||||
def iteration(S, R, A, B, C):
|
||||
I, J = np.zeros(C.shape, dtype=np.int64), np.zeros(C.shape, dtype=np.complex128)
|
||||
E, Z = np.zeros_like(C), np.zeros_like(C)
|
||||
|
||||
def abs2(z):
|
||||
return z.real * z.real + z.imag * z.imag
|
||||
|
||||
def iterate2(delta, index, epsilon, z, dz):
|
||||
def iterate2(delta, index, epsilon, z):
|
||||
index, epsilon = index + 1, (2 * S[index] + epsilon) * epsilon + delta
|
||||
z, dz = S[index] + epsilon, 2 * z * dz + 1
|
||||
index, epsilon = index + 1, (2 * S[index] + epsilon) * epsilon + delta
|
||||
z, dz = S[index] + epsilon, 2 * z * dz + 1
|
||||
return index, epsilon, z, dz
|
||||
z = S[index] + epsilon
|
||||
return index, epsilon, z
|
||||
|
||||
def skip100(delta, index, e, z, dz):
|
||||
de = dz - dS[index] # no catastrophic cancellation (don't try that with e)
|
||||
# for l in range(100): # skip 100 iterations (using linear approximations)
|
||||
# index, e, de = index + 1, 2 * S[index] * e + delta, 2 * S[index] * de
|
||||
index, e, de = index + 100, A[index] * e + B[index] * delta, A[index] * de
|
||||
z, dz = S[index] + e, dS[index] + de
|
||||
return index, e, z, dz
|
||||
def skip100(delta, index, epsilon, z):
|
||||
# for k in range(100): # skip 100 iterations (using linear approximations)
|
||||
# index, epsilon = index + 1, 2 * S[index] * epsilon + delta
|
||||
index, epsilon = index + 100, A[index] * epsilon + B[index] * delta
|
||||
z = S[index] + epsilon
|
||||
return index, epsilon, z
|
||||
|
||||
for k in range(len(C)):
|
||||
delta, index, epsilon, z, dz = C[k], I[k], E[k], Z[k], dZ[k]
|
||||
delta, index, epsilon, z = C[k], I[k], E[k], Z[k]
|
||||
|
||||
i, j = 0, 0
|
||||
while i + j < n:
|
||||
if abs2(z) < abs2(r):
|
||||
if abs2(epsilon) < abs2(1e-10 * R[index]):
|
||||
index, epsilon, z, dz = skip100(delta, index, epsilon, z, dz)
|
||||
if abs2(epsilon) < abs2(1e-8 * R[index]): # accuracy
|
||||
index, epsilon, z = skip100(delta, index, epsilon, z)
|
||||
j = j + 100
|
||||
else:
|
||||
if abs2(z) < abs2(epsilon):
|
||||
index, epsilon = 0, z # reset the reference orbit
|
||||
index, epsilon, z, dz = iterate2(delta, index, epsilon, z, dz)
|
||||
index, epsilon, z = iterate2(delta, index, epsilon, z)
|
||||
i = i + 2
|
||||
else:
|
||||
break
|
||||
|
||||
I[k], E[k], Z[k], dZ[k], J[k] = index, epsilon, z, dz, complex(i + j, j)
|
||||
I[k], J[k], E[k], Z[k] = index, complex(i + j, j), epsilon, z
|
||||
|
||||
return I, E, Z, dZ, J
|
||||
return I, J, E, Z
|
||||
|
||||
A, B = np.ones(n, dtype=np.complex128), np.zeros(n, dtype=np.complex128)
|
||||
R, aS = np.full(n, 2, dtype=np.float64), np.where(np.abs(S) < 2, np.abs(S), 0)
|
||||
dS = np.zeros(n + 100, dtype=np.complex128)
|
||||
|
||||
for i in range(1, n + 100): # derivation of the series (accuracy is not required)
|
||||
dS[i] = 2 * S[i - 1] * dS[i - 1] + 1
|
||||
|
||||
for i in numba.prange(n): # coefficients und radii for the bilinear approximation
|
||||
for l in range(100):
|
||||
A[i], B[i] = 2 * S[i + l] * A[i], 2 * S[i + l] * B[i] + 1
|
||||
R[i] = min(R[i], aS[i + l]) # validity radii and skip barriers (zeros)
|
||||
for i in numba.prange(n): # coefficients and radii for the bilinear approximation
|
||||
for k in range(100):
|
||||
A[i], B[i] = 2 * S[i + k] * A[i], 2 * S[i + k] * B[i] + 1
|
||||
R[i] = min(R[i], aS[i + k]) # validity radii and skip barriers (zeros)
|
||||
|
||||
for i in numba.prange(C.shape[0]):
|
||||
I[i, :], E[i, :], Z[i, :], dZ[i, :], J[i, :] = iteration(S, dS, R, A, B, C[i, :])
|
||||
I[i, :], J[i, :], E[i, :], Z[i, :] = iteration(S, R, A, B, C[i, :])
|
||||
|
||||
return I, E, Z, dZ, J
|
||||
return I, J, E, Z
|
||||
|
||||
I, E, Z, dZ, J = iteration_numba_bla(S, C)
|
||||
D, T = np.zeros(C.shape, dtype=np.float64), J.real.copy()
|
||||
def iteration_cupy_cuda(S, C):
|
||||
S, C = cp.asarray(S, dtype=np.complex64), cp.asarray(C, dtype=np.complex64)
|
||||
I, J = cp.zeros(C.shape, dtype=np.int32), cp.zeros(C.shape, dtype=np.float32)
|
||||
E, Z = cp.zeros_like(C), cp.zeros_like(C)
|
||||
|
||||
iteration = cp.RawKernel("""
|
||||
#include <cupy/complex.cuh>
|
||||
|
||||
extern "C" __global__
|
||||
void iterate(int dim_x, int dim_y, int n, int r,
|
||||
complex<float> *S, complex<float> *C,
|
||||
int *I, float *J, complex<float> *E, complex<float> *Z) {
|
||||
|
||||
int x = blockIdx.x * blockDim.x + threadIdx.x;
|
||||
int y = blockIdx.y * blockDim.y + threadIdx.y;
|
||||
|
||||
if (x < dim_x and y < dim_y) {
|
||||
int x_y = x * dim_y + y; // cupy arrays are in row-major order
|
||||
|
||||
complex<float> delta = C[x_y];
|
||||
int index = I[x_y];
|
||||
complex<float> e = E[x_y];
|
||||
complex<float> z = Z[x_y];
|
||||
|
||||
float abs2_r = float(r) * float(r);
|
||||
|
||||
int i = 0;
|
||||
while (i < n) {
|
||||
float abs2_z = z.real() * z.real() + z.imag() * z.imag();
|
||||
if (abs2_z < abs2_r) {
|
||||
float abs2_e = e.real() * e.real() + e.imag() * e.imag();
|
||||
if (abs2_z < abs2_e) {
|
||||
e = z; index = 0; // reset the reference orbit
|
||||
}
|
||||
e = (float(2) * S[index] + e) * e + delta; index = index + 1;
|
||||
e = (float(2) * S[index] + e) * e + delta; index = index + 1;
|
||||
z = S[index] + e;
|
||||
i = i + 2;
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
I[x_y] = index; J[x_y] = float(i); E[x_y] = e; Z[x_y] = z;
|
||||
}
|
||||
}
|
||||
""", "iterate")
|
||||
|
||||
griddim, blockdim = ((C.shape[0] - 1) // 32 + 1, (C.shape[1] - 1) // 32 + 1), (32, 32)
|
||||
iteration(griddim, blockdim, (C.shape[0], C.shape[1], n, r, S, C, I, J, E, Z))
|
||||
return I.get(), J.get(), E.get(), Z.get()
|
||||
|
||||
I, J, E, Z = iteration_numba_bla(S, C) # use iteration_numba_bla or iteration_cupy_cuda
|
||||
T = J.real.copy()
|
||||
|
||||
skipped = J.imag.sum() / J.real.sum()
|
||||
print("%.1f%% of all iterations were skipped." % (skipped * 100))
|
||||
|
||||
N = abs(Z) > 2 # exterior distance estimation
|
||||
D[N] = np.log(abs(Z[N])) * abs(Z[N]) / abs(dZ[N])
|
||||
|
||||
plt.imshow(D ** 0.15, cmap=plt.cm.turbo, origin="lower")
|
||||
plt.savefig("Mandelbrot_deep_zoom.png", dpi=200)
|
||||
|
||||
N = abs(Z) >= r # normalized iteration count
|
||||
T[N] = T[N] - np.log2(np.log(abs(Z[N])) / np.log(r))
|
||||
|
||||
T = np.minimum(T, n) # truncation
|
||||
T = (T - T.min()) / (T.max() - T.min()) # scaling
|
||||
T = np.maximum(n - T, 0) # inversion and truncation
|
||||
T = T / T.max() # scaling
|
||||
|
||||
plt.imshow(T ** 0.2, cmap=plt.cm.jet, origin="lower")
|
||||
plt.savefig("Mandelbrot_deep_time.png", dpi=200)
|
||||
plt.imshow(T ** 2.0 % (1/64), cmap=plt.cm.turbo, origin="lower")
|
||||
plt.savefig("Mandelbrot_deep_zoom.png", dpi=200)
|
||||
|
|
|
|||
|
|
@ -1,13 +1,8 @@
|
|||
dx=800; dy=600 # define grid size
|
||||
library(caTools); dx=800; dy=600 # write.gif, grid size
|
||||
jet.colors = colorRampPalette(c("#00007F", "blue", "#007FFF",
|
||||
"cyan", "#7FFF7F", "yellow", "#FF7F00", "red", "#7F0000"))
|
||||
C = complex(real=rep(seq(-2.2, 1.0, length.out=dx), each=dy),
|
||||
imag=rep(seq(-1.2, 1.2, length.out=dy), dx))
|
||||
C = matrix(C, dy, dx) # convert from vector to matrix
|
||||
Z = 0 # initialize Z to zero
|
||||
X = array(0, c(dy, dx, 20)) # allocate memory for all the frames
|
||||
for (k in 1:20) { # perform 20 iterations
|
||||
Z = Z^2+C # the main equation
|
||||
X[, , k] = exp(-abs(Z)) # store magnitude of the complex number
|
||||
}
|
||||
library(caTools) # load library with write.gif function
|
||||
jetColors = colorRampPalette(c("#00007F", "blue", "#007FFF", "cyan", "#7FFF7F", "yellow", "#FF7F00", "red", "#7F0000"))
|
||||
write.gif(X, "Mandelbrot.gif", col=jetColors, delay=100, transparent=0)
|
||||
imag=rep(seq(-1.2, 1.2, length.out=dy), dx))
|
||||
C = matrix(C, dy, dx); Z = 0; X = array(0, c(dy, dx, 20))
|
||||
for (k in 1 : 20) {Z = Z ^ 2 + C; X[, , k] = exp(- abs(Z))}
|
||||
write.gif(X, "Mandelbrot_set.gif", col=jet.colors, delay=100)
|
||||
|
|
|
|||
56
Task/Mandelbrot-set/Rebol/mandelbrot-set.rebol
Normal file
56
Task/Mandelbrot-set/Rebol/mandelbrot-set.rebol
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
Rebol [
|
||||
title: "Rosetta code: Mandelbrot set"
|
||||
file: %Mandelbrot_set.r3
|
||||
url: https://rosettacode.org/wiki/Mandelbrot_set
|
||||
needs: 3.0.0
|
||||
]
|
||||
|
||||
ascii-mandelbrot: function [
|
||||
"Integer ASCII Mandelbrot generator"
|
||||
][
|
||||
;; Define edges and steps
|
||||
leftEdge: -420
|
||||
rightEdge: 300
|
||||
topEdge: 300
|
||||
bottomEdge: -300
|
||||
xStep: 7
|
||||
yStep: 15
|
||||
maxIter: 200
|
||||
|
||||
;; Prebuild character map for iterations 0–10
|
||||
chars: "0123456789@"
|
||||
|
||||
;; Loop over rows (y0 from topEdge down to bottomEdge)
|
||||
for y0 topEdge bottomEdge negate yStep [
|
||||
;; Loop over columns (x0 from leftEdge up to rightEdge)
|
||||
for x0 leftEdge rightEdge xStep [
|
||||
;; Initialize iteration state
|
||||
x: y: i: 0 theChar: SP
|
||||
;; Iterate until divergence or maxIter
|
||||
while [i < maxIter] [
|
||||
;; Compute scaled squares
|
||||
x_x: to integer! (x * x / 200)
|
||||
y_y: to integer! (y * y / 200)
|
||||
|
||||
;; Check escape condition
|
||||
either x_x + y_y > 800 [
|
||||
;; Choose character based on iteration count
|
||||
theChar: pick chars either i > 9 [10] [i]
|
||||
;; Break by setting i to maxIter
|
||||
i: maxIter
|
||||
][
|
||||
;; Continue Mandelbrot iteration
|
||||
y: to integer! ((x * y) / 100) + y0
|
||||
x: x_x - y_y + x0
|
||||
i: i + 1
|
||||
]
|
||||
]
|
||||
;; Print character for this point
|
||||
prin theChar
|
||||
]
|
||||
;; Newline at end of row
|
||||
print ""
|
||||
]
|
||||
]
|
||||
;; evaluate....
|
||||
ascii-mandelbrot
|
||||
63
Task/Mandelbrot-set/Tiny-BASIC/mandelbrot-set.basic
Normal file
63
Task/Mandelbrot-set/Tiny-BASIC/mandelbrot-set.basic
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
1 GOTO 100
|
||||
2 PR".";
|
||||
3 RETURN
|
||||
4 PR",";
|
||||
5 RETURN
|
||||
6 PR"'";
|
||||
7 RETURN
|
||||
8 PR"~";
|
||||
9 RETURN
|
||||
10 PR"=";
|
||||
11 RETURN
|
||||
12 PR"+";
|
||||
13 RETURN
|
||||
14 PR":";
|
||||
15 RETURN
|
||||
16 PR";";
|
||||
17 RETURN
|
||||
18 PR"*";
|
||||
19 RETURN
|
||||
20 PR"%";
|
||||
21 RETURN
|
||||
22 PR"&";
|
||||
23 RETURN
|
||||
24 PR"$";
|
||||
25 RETURN
|
||||
26 PR"O";
|
||||
27 RETURN
|
||||
28 PR"X";
|
||||
29 RETURN
|
||||
30 PR"B";
|
||||
31 RETURN
|
||||
32 PR"#";
|
||||
33 RETURN
|
||||
34 PR"@";
|
||||
33 RETURN
|
||||
100 PR "Integer Mandelbrot"
|
||||
140 F=50
|
||||
150 Y=-12
|
||||
160 X=-49
|
||||
170 C=X*229/100
|
||||
180 D=Y*416/100
|
||||
190 A=C
|
||||
193 B=D
|
||||
196 I=0
|
||||
200 Q=B/F
|
||||
205 S=B-(Q*F)
|
||||
210 T=((A*A)-(B*B))/F+C
|
||||
220 B=2*((A*Q)+(A*S/F))+D
|
||||
230 A=T
|
||||
233 P=A/F
|
||||
236 Q=B/F
|
||||
240 IF ((P*P)+(Q*Q))>=5 GOTO 280
|
||||
250 I=I+1
|
||||
255 IF I<16 GOTO 200
|
||||
260 PR" ";
|
||||
270 GOTO 290
|
||||
280 GOSUB (I+1)*2
|
||||
290 X=X+1
|
||||
295 IF X<30 GOTO 170
|
||||
300 PR
|
||||
310 Y=Y+1
|
||||
315 IF Y<13 GOTO 160
|
||||
320 END
|
||||
|
|
@ -1,9 +1,9 @@
|
|||
Size ← 800
|
||||
Cs ← ÷ 5[5_5_5 4_5_5 4_5_5 3_5_5 5_3_5 3_3_5 2_5_0 5_2_2 2_2_5 0_0_0]
|
||||
Cs ← ÷ 5[5_5_5 4_5_5 4_5_5 3_5_5 5_3_5 3_3_5 2_5_0 5_2_2 2_2_5 0_0_0]
|
||||
# Initialise complex co-ordinates.
|
||||
×2.5 ⊞ℂ:-1/4. ÷:-÷2,⇡.⟜(↯:0⊟.)Size
|
||||
×2.5 ˜⊞ℂ⊸-1/4 ˜÷-÷2⊃(∘|⇡|∘|˜↯0˙⊟)Size
|
||||
# Iterate 50 times (a, b, got_there) -> (a*a+b, b, got_there)
|
||||
# got_there counts when corresponding value in b hits 2.
|
||||
⍥⊃(+×.|⋅∘|+<2⌵⊙◌)50 0
|
||||
# Scale the results down and display
|
||||
⊏:Cs⌈×9÷:⟜(/↥/↥)ₙ2◌◌
|
||||
⍥⊃(+˙×|⋅∘|+<2⌵⊙◌)50 0
|
||||
# Scale the results down, colourise, and display
|
||||
˜⊏Cs⌈×9˜÷⟜(/↥/↥)°ₑ₂◌◌
|
||||
|
|
|
|||
263
Task/Mandelbrot-set/VBScript/mandelbrot-set.vbs
Normal file
263
Task/Mandelbrot-set/VBScript/mandelbrot-set.vbs
Normal file
|
|
@ -0,0 +1,263 @@
|
|||
option explicit
|
||||
|
||||
' Raster graphics class in VBSCRIPT by Antoni Gual
|
||||
'--------------------------------------------
|
||||
' An array keeps the image allowing to set pixels, draw lines and boxes in it.
|
||||
' at class destroy a bmp file is saved to disk and the default viewer is called
|
||||
' The class can work with 8 and 24 bit bmp. With 8 bit uses a built-in palette or can import a custom one
|
||||
|
||||
|
||||
'Declaration :
|
||||
' Set MyObj = (New ImgClass)(name,width,height, orient,bits_per_pixel,palette_array)
|
||||
' name:path and name of the file created
|
||||
' width, height of the canvas
|
||||
' orient is the way the coord increases, 1 to 4 think of the 4 cuadrants of the caterian plane
|
||||
' 1 X:l>r Y:b>t 2 X:r>l Y:b>t 3 X:r>l Y:t>b 4 X:l>r Y:t>b
|
||||
' bits_per_pixel can bs only 8 and 24
|
||||
' palette array only to substitute the default palette for 8 bits, else put a 0
|
||||
' it sets the origin at the corner of the image (bottom left if orient=1)
|
||||
|
||||
Class ImgClass
|
||||
Private ImgL,ImgH,ImgDepth,bkclr,loc,tt
|
||||
private xmini,xmaxi,ymini,ymaxi,dirx,diry
|
||||
public ImgArray() 'rgb in 24 bit mode, indexes to palette in 8 bits
|
||||
private filename
|
||||
private Palette,szpal
|
||||
|
||||
Public Property Let depth (x)
|
||||
if depth=8 or depth =24 then
|
||||
Imgdepth=depth
|
||||
else
|
||||
Imgdepth=8
|
||||
end if
|
||||
bytepix=imgdepth/8
|
||||
end property
|
||||
|
||||
Public Property Let Pixel (x,y,color)
|
||||
If (x>=ImgL) or x<0 then exit property
|
||||
if y>=ImgH or y<0 then exit property
|
||||
ImgArray(x,y)=Color
|
||||
End Property
|
||||
|
||||
Public Property Get Pixel (x,y)
|
||||
If (x<ImgL) And (x>=0) And (y<ImgH) And (y>=0) Then
|
||||
Pixel=ImgArray(x,y)
|
||||
End If
|
||||
End Property
|
||||
|
||||
Public Property Get ImgWidth ()
|
||||
ImgWidth=ImgL-1
|
||||
End Property
|
||||
|
||||
Public Property Get ImgHeight ()
|
||||
ImgHeight=ImgH-1
|
||||
End Property
|
||||
|
||||
'constructor (fn,w*2,h*2,32,0,0)
|
||||
Public Default Function Init(name,w,h,orient,dep,bkg,mipal)
|
||||
'offx, offy posicion de 0,0. si ofx+ , x se incrementa de izq a der, si offy+ y se incrementa de abajo arriba
|
||||
dim i,j
|
||||
ImgL=w
|
||||
ImgH=h
|
||||
tt=timer
|
||||
set0 0,0 'origin blc positive up and right
|
||||
redim imgArray(ImgL-1,ImgH-1)
|
||||
bkclr=bkg
|
||||
if bkg<>0 then
|
||||
for i=0 to ImgL-1
|
||||
for j=0 to ImgH-1
|
||||
imgarray(i,j)=bkg
|
||||
next
|
||||
next
|
||||
end if
|
||||
Select Case orient
|
||||
Case 1: dirx=1 : diry=1
|
||||
Case 2: dirx=-1 : diry=1
|
||||
Case 3: dirx=-1 : diry=-1
|
||||
Case 4: dirx=1 : diry=-1
|
||||
End select
|
||||
filename=name
|
||||
ImgDepth =dep
|
||||
'load user palette if provided
|
||||
if imgdepth=8 then
|
||||
loadpal(mipal)
|
||||
end if
|
||||
set init=me
|
||||
end function
|
||||
|
||||
private sub loadpal(mipale)
|
||||
if isarray(mipale) Then
|
||||
palette=mipale
|
||||
szpal=UBound(mipale)+1
|
||||
Else
|
||||
szpal=256
|
||||
'Default palette recycled from ATARI
|
||||
|
||||
End if
|
||||
End Sub
|
||||
public sub set0 (x0,y0) 'origin can be changed during drawing
|
||||
if x0<0 or x0>=imgl or y0<0 or y0>imgh then err.raise 9
|
||||
xmini=-x0
|
||||
ymini=-y0
|
||||
xmaxi=xmini+imgl-1
|
||||
ymaxi=ymini+imgh-1
|
||||
|
||||
end sub
|
||||
|
||||
|
||||
Private Sub Class_Terminate
|
||||
if err <>0 then wscript.echo "Error " & err.number
|
||||
wscript.echo "writing bmp to file"
|
||||
savebmp
|
||||
wscript.echo "opening " & filename
|
||||
CreateObject("Shell.Application").ShellExecute filename
|
||||
wscript.echo timer-tt & " seconds"
|
||||
End Sub
|
||||
|
||||
|
||||
'writes a 32bit integr value as binary to an utf16 string
|
||||
function long2wstr( x) 'falta muy poco!!!
|
||||
dim k1,k2,x1
|
||||
k1= (x and &hffff&)' or (&H8000& And ((X And &h8000&)<>0)))
|
||||
k2=((X And &h7fffffff&) \ &h10000&) Or (&H8000& And (x<0))
|
||||
long2wstr=chrw(k1) & chrw(k2)
|
||||
end function
|
||||
|
||||
function int2wstr(x)
|
||||
int2wstr=ChrW((x and &h7fff) or (&H8000 And (X<0)))
|
||||
End Function
|
||||
|
||||
|
||||
Public Sub SaveBMP
|
||||
'Save the picture to a bmp file
|
||||
Dim s,ostream, x,y,loc
|
||||
|
||||
const hdrs=54 '14+40
|
||||
dim bms:bms=ImgH* 4*(((ImgL*imgdepth\8)+3)\4) 'bitmap size including padding
|
||||
dim palsize:if (imgdepth=8) then palsize=szpal*4 else palsize=0
|
||||
|
||||
with CreateObject("ADODB.Stream") 'auxiliary ostream, it creates an UNICODE with bom stream in memory
|
||||
.Charset = "UTF-16LE" 'o "UTF16-BE"
|
||||
.Type = 2' adTypeText
|
||||
.open
|
||||
|
||||
'build a header
|
||||
'bmp header: VBSCript does'nt have records nor writes binary values to files, so we use strings of unicode chars!!
|
||||
'BMP header
|
||||
.writetext ChrW(&h4d42) ' 0 "BM" 4d42
|
||||
.writetext long2wstr(hdrs+palsize+bms) ' 2 fiesize
|
||||
.writetext long2wstr(0) ' 6 reserved
|
||||
.writetext long2wstr (hdrs+palsize) '10 image offset
|
||||
'InfoHeader
|
||||
.writetext long2wstr(40) '14 infoheader size
|
||||
.writetext long2wstr(Imgl) '18 image length
|
||||
.writetext long2wstr(imgh) '22 image width
|
||||
.writetext int2wstr(1) '26 planes
|
||||
.writetext int2wstr(imgdepth) '28 clr depth (bpp)
|
||||
.writetext long2wstr(&H0) '30 compression used 0= NOCOMPR
|
||||
|
||||
.writetext long2wstr(bms) '34 imgsize
|
||||
.writetext long2wstr(&Hc4e) '38 bpp hor
|
||||
.writetext long2wstr(&hc43) '42 bpp vert
|
||||
.writetext long2wstr(szpal) '46 colors in palette
|
||||
.writetext long2wstr(&H0) '50 important clrs 0=all
|
||||
|
||||
'write bitmap
|
||||
'precalc data for orientation
|
||||
Dim x1,x2,y1,y2
|
||||
If dirx=-1 Then x1=ImgL-1 :x2=0 Else x1=0:x2=ImgL-1
|
||||
If diry=-1 Then y1=ImgH-1 :y2=0 Else y1=0:y2=ImgH-1
|
||||
|
||||
Select Case imgdepth
|
||||
|
||||
Case 32
|
||||
For y=y1 To y2 step diry
|
||||
For x=x1 To x2 Step dirx
|
||||
'writelong fic, Pixel(x,y)
|
||||
.writetext long2wstr(Imgarray(x,y))
|
||||
Next
|
||||
Next
|
||||
|
||||
Case 8
|
||||
'palette
|
||||
For x=0 to szpal-1
|
||||
.writetext long2wstr(palette(x)) '52
|
||||
Next
|
||||
'image
|
||||
dim pad:pad=ImgL mod 4
|
||||
For y=y1 to y2 step diry
|
||||
For x=x1 To x2 step dirx*2
|
||||
.writetext chrw((ImgArray(x,y) and 255)+ &h100& *(ImgArray(x+dirx,y) and 255))
|
||||
Next
|
||||
'line padding
|
||||
if pad and 1 then .writetext chrw(ImgArray(x2,y))
|
||||
if pad >1 then .writetext chrw(0)
|
||||
Next
|
||||
|
||||
Case Else
|
||||
WScript.Echo "ColorDepth not supported : " & ImgDepth & " bits"
|
||||
End Select
|
||||
|
||||
'use a second stream to save to file starting past the BOM the first ADODB.Stream has added
|
||||
Dim outf:Set outf= CreateObject("ADODB.Stream")
|
||||
outf.Type = 1 ' adTypeBinary
|
||||
outf.Open
|
||||
.position=2 'remove bom (1 wchar)
|
||||
.CopyTo outf
|
||||
.close
|
||||
outf.savetofile filename,2 'adSaveCreateOverWrite
|
||||
outf.close
|
||||
end with
|
||||
End Sub
|
||||
End Class
|
||||
|
||||
function mandelpx(x0,y0,maxit)
|
||||
dim x,y,xt,i,x2,y2
|
||||
i=0:x2=0:y2=0
|
||||
Do While i< maxit
|
||||
i=i+1
|
||||
xt=x2-y2+x0
|
||||
y=2*x*y+y0
|
||||
x=xt
|
||||
x2=x*x:y2=y*y
|
||||
If (x2+y2)>=4 Then Exit do
|
||||
loop
|
||||
if i=maxit then
|
||||
mandelpx=0
|
||||
else
|
||||
mandelpx = i
|
||||
end if
|
||||
end function
|
||||
|
||||
Sub domandel(x1,x2,y1,y2)
|
||||
Dim i,ii,j,jj,pix,xi,yi,ym
|
||||
ym=X.ImgHeight\2
|
||||
'get increments in the mandel plane
|
||||
xi=Abs((x1-x2)/X.ImgWidth)
|
||||
yi=Abs((y2-0)/(X.ImgHeight\2))
|
||||
j=0
|
||||
For jj=0. To y2 Step yi
|
||||
i=0
|
||||
For ii=x1 To x2 Step xi
|
||||
pix=mandelpx(ii,jj,256)
|
||||
'use simmetry
|
||||
X.imgarray(i,ym-j)=pix
|
||||
X.imgarray(i,ym+j)=pix
|
||||
i=i+1
|
||||
Next
|
||||
j=j+1
|
||||
next
|
||||
End Sub
|
||||
|
||||
'main------------------------------------
|
||||
Dim i,x
|
||||
'custom palette
|
||||
dim pp(255)
|
||||
for i=1 to 255
|
||||
pp(i)=rgb(0,0,255*(i/255)^.25) 'VBS' RGB function is for the web, it's bgr for Windows BMP !!
|
||||
next
|
||||
|
||||
dim fn:fn=CreateObject("Scripting.FileSystemObject").GetSpecialFolder(2)& "\mandel.bmp"
|
||||
Set X = (New ImgClass)(fn,580,480,1,8,0,pp)
|
||||
domandel -2.,1.,-1.2,1.2
|
||||
Set X = Nothing
|
||||
|
|
@ -10,7 +10,7 @@ for Y:= 0 to 480-1 do \for all points on the screen...
|
|||
[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
|
||||
loop [if Zx*Zx + Zy*Zy > 4.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
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ fcn mandelbrot{ // lord this is slooooow
|
|||
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
|
||||
if(zx*zx + zy*zy > 4.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;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue