shlomi-fish-homepage / t2 / mandel.m

function ret = mandel(x,y,steps,init_value)

% mandel - a function to generate the Mandelbrot Set.
% Written by Shlomi Fish, 2001
% This file is under the public domain.

% Assign some default values to the parameters
% nargin is an internal Matlab variable that specifies how many
% parameters were passed to the function.
if nargin < 1
    x = 100;

if nargin < 2
    y = 100;

if nargin < 3
    steps = 10;

if nargin < 4
    init_value = 0;

% Generate the coordinates in the complex plane
% Combine them into a matrix of complex numbers
% Retrieve the dimensions of Z
dims = size(Z);
% The length in the x direction
x_len = dims(1);
% The length in the y direction
y_len = dims(2);
% value is initialized to init_value in every point of the plane
value = ones(x_len,y_len) * init_value;
% In the beginning all points are considered as part of the Mandelbrot
% set. Thus, they are initialized to zero.
ret   = zeros(x_len,y_len);
% The mask which indicates which points have already overflowed, is set
% to zero, to indicate that none have so far.
mask  = zeros(x_len,y_len);
% Perform the check "steps" times
for step=1:steps
    % For every point with a mandel value of "v" and a coordinate of "z"
    % perform  v <- (v ^ 2) + z
    % .* is an element-by-element multiplication of two matrixes of the same size.
    % (regular * indicates matrix multiplication)
    value = (value .* value) + Z;
    % Retrieve the points that overflowed in this iteration
    % An overflowed point has a mandel value with an absolute value greater
    % than 2.
    current_mask = (abs(value) > 2);
    % Update the mask. We use "or" in order to avoid a situation where
    % 1 and 1 become two or so.
    mask = or(mask, current_mask);
    % Upgrade the points in the mask to a greater value in the returned
    % Mandelbrot-map. 
    ret = ret + mask;
    % Zero the points that have overflowed, so they will not propagate
    % to infinity.
    value = value .* not(current_mask);

% Now ret is ready for prime time so we return it.
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.