Delphi offers the IntToHex() function to convert integer values to hex string representations but what if you need to convert a number to a binary not a hex? Well, here’s the code for converting byte values to strings:
function IntToBin(btValue: Byte): string;
function Next(btJ: Byte): string;
begin
if btValue and btJ = 0 then
Result := '0'
else
Result := '1';
end;
begin
Result := Next(128) + Next(64) + Next(32) + Next(16) + Next(8) + Next(4) + Next(2) + Next(1);
end;
Categories: Math Tags: binary string, btj, byte string, byte value, byte values, Delphi, hex string, integer values, representations, string function, string representation
This source code shows you how to draw a Madelbrot fractal on the form’s Canvas. Although some of the Integer variables could be changed to Byte or Word variables they have not been changed because the Integer type is a native x86 type and calculations on Integers are the fastest.
This function is also (as tip 6.3) based on the source code written by Marc Capuano. This source is free for non-commercial use. The original source has been cleaned up and has been optimized for speed and size.
The function seems to have the same speed as tip 6.3.
uses
Graphics;
...
procedure DrawMandelbrot;
const
aColors: array[0..14] of TColor = (clBlack, clMaroon, clGreen, clNavy,
clPurple, clTeal, clGray, clSilver, clRed, clLime, clBLue, clFuchsia,
clAqua, clWhite, clBlack);
var
iI, iJ, iNewColor : Integer;
rU, rV, rX, rY, rZ: Real;
begin
for iI := 0 to ClientWidth - 2 do
for iJ := 0 to ClientHeight - 2 do begin
rX := -0.8 + 3 * iI / ClientWidth;
rY := -1.4 + 2.8 * iJ / ClientHeight;
iNewColor := 0;
rU := 0;
rV := 0;
repeat
rZ := Sqr(rU) - Sqr(rV) - rX;
rV := 2 * rU * rV - rY;
rU := rZ;
Inc(iNewColor);
until (Sqr(rU) + Sqr(rV) > 9) or (iNewColor = 14);
Canvas.Pixels[iI + 1, iJ + 1] := aColors[iNewColor];
end;
end;
Categories: Math Tags: array, byte, canvas, capuano, integer type, integer variables, integers, mandelbrot fractal, native x86, original source, ru, rv, rx, ry, rz, source code, sqr