Originally Posted by
fre
Nah still don't work properly
something is wrong with the loops.
Simba Code:
Height := High(d2ColorsBmp[0]);//Has to be.
writeln(Height);
Width := High(d2ColorsBmp);
writeln(width);
ColorsBMP := [];
SetLength(ColorsBmp, ((Width+1) * (Height+1))); //One time resize instead of every loop. Resizing an array is expensive. It creates a larger one and then copies the smaller one to it.
for I := 0 to Width do
begin
for J := 0 to Height do
begin
writeln('adding color ' + tostr(d2ColorsBMP[I][J]));
colorsBmp[Loop] := d2ColorsBMP[I][J]; //Indexing. //Can still use Inc(Loop); if you want instead..
Inc(Loop);
writeln(ColorsBmp);
end;
end;
Changed it back to 'loop' increments and works fine now.
@fre, here you go.. Several ways below.
Single loop version:
Simba Code:
function T2DIntegerArray.Merge(): TIntegerArray; overload;
var
i, j: Integer;
begin
j := Length(self[0]);
SetLength(Result, (j * Length(self)));
for i := 0 to High(Result) do
Result[i] := self[(i div j)][(i mod j)];
end;
var
matrix: T2DIntegerArray;
arr: TIntegerArray;
begin
ClearDebug;
matrix := [[0,1], [2,3], [4,5], [6,7], [8,9]];
arr := matrix.Merge();
WriteLn(arr, ' <= ', matrix);
matrix := [[0,1,2,3,4], [5,6,7,8,9]];
arr := matrix.Merge();
WriteLn(arr, ' <= ', matrix);
matrix := [[1,2,3], [4,5,6], [7,8,9]];
arr := matrix.Merge();
WriteLn(arr, ' <= ', matrix);
end;
Double loop without incrementing:
Simba Code:
function T2DIntegerArray.Merge(): TIntegerArray; overload;
var
w, h, x, y: Integer;
begin
h := Length(self);
w := Length(self[0]);
SetLength(Result, (w * h));
for y := 0 to (h - 1) do
for x := 0 to (w - 1) do
Result[(x + (y * w))] := self[y][x];
end;
var
matrix: T2DIntegerArray;
arr: TIntegerArray;
begin
ClearDebug;
matrix := [[0,1], [2,3], [4,5], [6,7], [8,9]];
arr := matrix.Merge();
WriteLn(arr, ' <= ', matrix);
matrix := [[0,1,2,3,4], [5,6,7,8,9]];
arr := matrix.Merge();
WriteLn(arr, ' <= ', matrix);
matrix := [[1,2,3], [4,5,6], [7,8,9]];
arr := matrix.Merge();
WriteLn(arr, ' <= ', matrix);
end;
Double loop version with incrementing:
Simba Code:
function T2DIntegerArray.Merge(): TIntegerArray; overload;
var
w, h, x, y, z: Integer;
begin
h := Length(self);
w := Length(self[0]);
z := 0;
SetLength(Result, (w * h));
for y := 0 to (h - 1) do
for x := 0 to (w - 1) do
begin
Result[z] := self[y][x];
z := (z + 1);
end;
end;
var
matrix: T2DIntegerArray;
arr: TIntegerArray;
begin
ClearDebug;
matrix := [[0,1], [2,3], [4,5], [6,7], [8,9]];
arr := matrix.Merge();
WriteLn(arr, ' <= ', matrix);
matrix := [[0,1,2,3,4], [5,6,7,8,9]];
arr := matrix.Merge();
WriteLn(arr, ' <= ', matrix);
matrix := [[1,2,3], [4,5,6], [7,8,9]];
arr := matrix.Merge();
WriteLn(arr, ' <= ', matrix);
end;
Edit: Noticed you already had similar solutions worked out.