Minimum spanning tree con Delphi (Parte II)

En esta segunda parte, os dejo la aplicación Thundax RandomImage, que es con la que creo las imágenes aleatorias creando un mapa de pixels. Mediante ésta aplicación, podemos generar lienzos con píxeles aleatorios y luego entregarlos a la aplicación ThundaxSpanningTree_v2 y resolverlos mediante el algoritmo del Prim que tengo implementado.

De esta manera, podemos crear el fichero en formato bmp, y pasarlo a la otra aplicación para ver el resultado:


He modificado la aplicación para que tolere el calculo de más de 100 puntos, hasta 1000. Aquí os dejo parte del código fuente de la aplicación que genera las imágenes para mostrar como podemos trabajar con TCanvas. Podemos crear las imágenes manualmente, luego haciendo click sobre el lienzo se irán añadiendo los pixels nuevos o podemos hacerlo todo con el Random y generará imágenes aleatorias.




procedure TForm3.DrawRectangle(image1: TImage; BackGroundColor: Integer; RectangleColor: integer);
begin
image1.Canvas.Brush.Style := bsSolid;
image1.Canvas.Brush.Color := BackGroundColor;
image1.Canvas.Pen.Width := 3;
image1.Canvas.Pen.Color := RectangleColor;
image1.Canvas.Rectangle(0, 0, image1.Width, image1.Height);
end;

procedure TForm3.Button1Click(Sender: TObject);
begin
img.Canvas.CleanupInstance;
img.Width := StrToInt(Edit2.text);
img.Height := StrToInt(Edit3.text);
img.Canvas.Brush.Style := bsSolid;
img.Canvas.Brush.Color := cxColorCombobox1.ColorValue;
img.Canvas.Pen.Width := 3;
img.Canvas.Pen.Color := cxColorCombobox1.ColorValue;
img.Canvas.Rectangle(0, 0, StrToInt(Edit2.text), StrToInt(Edit3.text));
created := true;
end;

procedure TForm3.Button4Click(Sender: TObject);
var
num: integer;
i, int1, int2: integer;
begin
num := StrToInt(Edit1.text);
for i := 0 to num - 1 do
begin
int1 := 1 + Random(img.width);
int2 := 1 + Random(img.Height);
DrawPoint(img, Point(int1, int2), clYellow);
end;
end;

procedure TForm3.Button5Click(Sender: TObject);
begin
img.Canvas.CleanupInstance;
img.Width := 200 + Random(600);
Edit2.text := IntToStr(img.width);
img.Height := 300 + Random(600);
Edit3.text := IntToStr(img.Height);
img.Picture.Bitmap.SetSize(StrToInt(Edit2.text), StrToInt(Edit3.text));
img.Canvas.Brush.Style := bsSolid;
img.Canvas.Brush.Color := RGB(Random(256), Random(256), Random(256));
img.Canvas.Pen.Width := 3;
img.Canvas.Pen.Color := img.Canvas.Brush.Color;
img.Canvas.Rectangle(0, 0, img.Width, img.Height);
created := true;
Button6Click(Sender);
end;

procedure TForm3.Button6Click(Sender: TObject);
var
num: integer;
i, int1, int2: integer;
begin
if not created then
exit;
num := StrToInt(Edit1.text);
for i := 0 to num - 1 do
begin
int1 := 1 + Random(img.width);
int2 := 1 + Random(img.Height);
DrawPoint(img, Point(int1, int2), cxColorCombobox2.ColorValue);
end;
end;

procedure TForm3.Button7Click(Sender: TObject);
begin
img.Picture.Bitmap.SetSize(StrToInt(Edit2.text), StrToInt(Edit3.text));
img.Picture.Bitmap.SaveToFile(Edit4.text);
end;

procedure TForm3.DrawPoint(image1: TImage; point: TPoint; colorPoint: integer);
begin
image1.Canvas.Pen.Color := colorPoint;
image1.Canvas.Pen.Width := 2;
image1.Canvas.Pixels[point.x, point.Y] := colorPoint;
end;

procedure TForm3.imgMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
DrawPoint(img, Point(x, y), clyellow);
end;





aqui os dejo un ejemplo con unos 900 puntos: (resuelto en menos de 1 segundo)



Comments

Popular Posts