31st
Май

Как Image переместить к точке по прямой?

Posted by Chas under Пост-обзор

Суть моей проблемы в том, что имеется Image с определенными координатами начала движения, и есть координаты точки окончания его движения. Мне надо сделать так, что Image двигался в таймере по прямой от точки начала движения к точке конца. Не подскажете, как сделать?

phomm:

var sxpos, fxpos, sypos, fypos: integer;
cx, cy, dx, dy : extended;
...
procedure button1click(sender : tobject);
begin
sxpos := image1.left;
sypos := image1.top;
fypos := strtoint(Edit1.Text);
fxpos := strtoint(Edit1.Text); // где-то уже получили координаты, рассчитываем смещение

if abs(fxpos - sxpos) > abs(fypos - sypos)
then begin
dy := (fypos - sypos)/abs(fxpos - sxpos);
dx := 1;
end
else begin
dx := (fxpos - sxpos)/abs(fypos - sypos);
dy := 1;
end;
cy := sypos;
cx := sxpos;
timer1.enabled := true;
end;

procedure timer1.timer(sender : tobject);
begin
cy := cy + dy;
cx := cx + dx;
image1.left := round(cx);
image1.top := round(cy);
if ( image1.left = fxpos) and ( image1.top = fypos)
then Timer2.Enabled := false;
end;

тема на форуме

Похожие статьи

Теги: |