PDA

Ver la Versión Completa : Seleccionar pixels


Unkger
29-12-2017, 03:08:54
Hola, tengo un formulario con un image1, un StringGrid1 y dos botones. Lo que hace el programa es dibujar N pixeles y guardar sus coordenadas (x, y) en el StringGrid1.
Bueno, primero presiono el Button1 para dibujar un pixel, luego de haberlo dibujado presiono el Button2 para seleccionar el pixel, si clickeo cerca del pixel me muestra un mensaje que dice en que "renglón" del StringGrid1 se encontraron las coordenadas de ese pixel, esto solo funciona con un solo pixel, pero lo que yo quiero es que cuando dibuje N pixels, y de click en cualquier pixel me muestre un mensaje que diga en que "columnas" del StringGrid1 se encontraron las coordenadas (x, y) de ese pixel.

Por ejemplo dibujo 10 pixels, y las coordenadas de esos 10 pixels son guardados en el Grid1 en un solo renglon. Asi las columnas pares son la coordenada X, y las columnas impares son la coordenada Y.
|X|Y|X|Y|X|Y|......

El problema en esto es que al buscar si existen las coordenadas del pixel en el Grid se confunde con las coordenadas de los demas, por ejemplo puedo tener:
|156|56|183|56|214|56|238|56|....... // aquí todas las coordenadas Y son iguales, cuando doy click cerca del primer pixel (156, 56) que son las columnas (0, 1) me debe de salir el mensaje que se encuentra en esas columnas (0, 1), pero me pude salir que están en las columnas (0, 1), (0, 3), (0, 5) o (0, 7). ¿Como puedo diferenciar cada pixel para que no se confunda con los otros?, espero me haya explicado.


var
Form1: TForm1;
iCol, iCol2 : Integer;
iRow : Integer;
pixel : Integer;
implementation
{$R *.dfm}


procedure TForm1.Button1Click(Sender: TObject); // Dibujar pixel
begin
pixel := 0;
if iRow = StringGrid1.RowCount then
begin
StringGrid1.ColCount := StringGrid1.ColCount+2;
iCol := iCol+2;
iCol2 := iCol2+2;
iRow := 0;
end;
end;

procedure TForm1.Button2Click(Sender: TObject); // Seleccionar pixel
begin
pixel := 1;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
image1.Canvas.FillRect(image1.Canvas.ClipRect);
iCol := 0;
iCol2 := 1;
iRow := 0;
pixel := 999;
end;

procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
i, dx, dy : Integer;
begin
case pixel of
0:
begin
image1.Canvas.Pixels[x, y] := clRed;
StringGrid1.Cells[iCol, iRow] := inttostr(x);
StringGrid1.Cells[iCol2, iRow] := inttostr(y);
iRow := iRow+1;
if iRow = StringGrid1.RowCount then
begin
pixel := 999;
end;
end;
1: // aquí creo es el problema
begin
for i := 0 to StringGrid1.RowCount - 1 do
begin
dx := x-(strtoint(StringGrid1.Cells[0, i]));
dy := y-(strtoint(StringGrid1.Cells[1, i]));
if (abs(dx) < 5) and (abs(dy) < 5) then
begin
showMessage('Encontrado en renglon ' + inttostr(i));
end;
end;
end;
end;
end;

ecfisa
29-12-2017, 18:15:27
Hola.

No entiendo bien tu dificultad, pero hice una prueba con los valores que indicas en tu mensaje:156|56|183|56|214|56|238|56| y no me dió ningún problema.

El código de la prueba:

...

implementation {$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
const
VP: array[0..3] of string = ('156','183','214','238');
var
sg: TStringGrid;
cv: TCanvas;
i : Integer;
begin
sg := StringGrid1;
sg.FixedCols := 0;
sg.FixedRows := 0;
sg.ColCount := 2;
sg.RowCount := 4;
cv := Image1.Picture.Bitmap.Canvas;
for i := 0 to 3 do
begin
cv.Pixels[StrToInt(VP[i]), 56] := clRed;
sg.Cells[0, i] := VP[i];
sg.Cells[1, i] := '56';
end;
end;


procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
dx, dy, r : Integer;
sg: TStringGrid;
begin
// (quité el case ya que no se necesita para la prueba)
sg := StringGrid1;
for r := 0 to StringGrid1.RowCount - 1 do
begin
dx := X - (StrToInt(StringGrid1.Cells[0, r]));
dy := Y - (strToInt(StringGrid1.Cells[1, r]));
if (abs(dx) < 5) and (abs(dy) < 5) then
begin
sg.Selection := TGridRect(Rect(0, r, sg.ColCount, r));
Label1.Caption := 'Encontrado en renglon ' + inttostr(r);
end;
end;
end;
...


Salida:
https://s17.postimg.org/5119y1srj/Unkger.gif
La única situación en que se me ocurre que podría haber confusión es cuando se solapen los valores de X e Y dada la tolerancia de 5 píxeles que les das.

Saludos :)

Unkger
29-12-2017, 21:39:19
¿Y como seria de forma horizontal?, en lugar de que me diga en que renglón esta, que me diga en que columnas se encontró, con el código que pusiste, si le cambio un poco, el mensaje que muestra en el label no corresponde al pixel que selecciono. Me muestra:
"Encontrado en columnas 0, 7", debería de ser "Encontrado en columnas 0, 1"
"Encontrado en columnas 2, 7", debería de ser "Encontrado en columnas 2, 3"
"Encontrado en columnas 4, 7", debería de ser "Encontrado en columnas 4, 5".
"Encontrado en columnas 6, 7", este si esta bien.

...

implementation {$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
const
VP: array[0..3] of string = ('156','183','214','238');
var
sg: TStringGrid;
cv: TCanvas;
i : Integer;
j, j2 : Integer;
begin
image1.Canvas.FillRect(image1.Canvas.ClipRect);
j := 0;
j2 := 1;
sg := StringGrid1;
sg.FixedCols := 0;
sg.FixedRows := 0;
sg.ColCount := 8;
sg.RowCount := 1;
cv := Image1.Picture.Bitmap.Canvas;
for i := 0 to 3 do
begin
cv.Pixels[StrToInt(VP[i]), 56] := clRed;
sg.Cells[j, 0] := VP[i];
sg.Cells[j2, 0] := '56';
j := j+2;
j2 := j2+2;
end;
end;

procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
dx, dy, r : Integer;
sg: TStringGrid;
c, c2 : Integer;
begin
sg := StringGrid1;
for r := 0 to StringGrid1.RowCount - 1 do
for c := 0 to StringGrid1.ColCount - 1 do
for c2 := 1 to StringGrid1.ColCount -1 do
begin
dx := X - (StrToInt(StringGrid1.Cells[c, r]));
dy := Y - (strToInt(StringGrid1.Cells[c2, r]));
if (abs(dx) < 5) and (abs(dy) < 5) then
begin
sg.Selection := TGridRect(Rect(0, r, sg.ColCount, r));
Label1.Caption := 'Encontrado en columnas ' + inttostr(c) + ', ' + inttostr(c2);
end;
end;
end;
...

ecfisa
30-12-2017, 18:19:15
Hola.

Claro... pero habías solicitado mostrar el renglón.

Tal vez no esté interpretando bien tu planteo, pero si el StringGrid tiene dos columnas donde se almacenan las coordenadas X/Y del píxel, usando el ejemplo del mensaje #2 (http://www.clubdelphi.com/foros/showpost.php?p=523800&postcount=2) podrias hacer:

procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
dx, dy, r : Integer;
sg: TStringGrid;
begin
// (quité el case ya que no se necesita para la prueba)
sg := StringGrid1;
for r := 0 to StringGrid1.RowCount - 1 do
begin
dx := X - (StrToInt(StringGrid1.Cells[0, r]));
dy := Y - (strToInt(StringGrid1.Cells[1, r]));
if (abs(dx) < 5) and (abs(dy) < 5) then
begin
sg.Selection := TGridRect(Rect(0, r, sg.ColCount, r));
Label1.Caption := Format('Encontrado en columna: %d y fila: %d',[0, r]);
end;
end;
end;

donde se evidencia que el valor de la columna no se altera sea cual fuere el pìxel seleccionado.

Saludos :)