V
V
Vyacheslav2015-05-19 20:36:55
Delphi
Vyacheslav, 2015-05-19 20:36:55

How to rotate by a given angle of the image?

Good day!
I recently started to learn graphics in Delphi, but I ran into the problem of rotating an image (created on the image component using lineto and moveto) by a given angle. I can not figure out how to write this procedure correctly.
Here is the required code:

procedure tform1.ship(x,y:integer;color,win:tcolor ; dx,dy:integer); // Создание корабля
begin
with image1.canvas do
begin
pen.color:=color;
pen.width:=2;
brush.Color:=color;
brush.style:=bssolid;
//korpys
moveto(x,y);
lineto (x,y-3*dy);
lineto(x+15*dx,y-3*dy);
lineto(x+10*dx,y);
lineto(x,y);
//rubka
moveto(x+3*dx,y-3*dy);
lineto(x+3*dx,y-8*dy);
lineto(x+8*dx,y-8*dy);
lineto(x+8*dx,y-3*dy);
end;
end;

procedure TForm1.Button5Click(Sender: TObject);  // Задаём корабля с клавиатуры
begin
x:=strtoint(inputbox('Значение Х','Введите значения X',''));
if x>image1.ClientWidth then
begin
showmessage('Превышает ширину рамки');
exit;
end;
y:=strtoint(inputbox('Значение Y','Введите значения Y',''));
if y>image1.Clientheight then
begin
showmessage('Превышает высоту рамки');
exit;
end;
dx:=10;
dy:=10;
ship(x,y,clred,clyellow,dx,dy);
end;

procedure TForm1.Button11Click(Sender: TObject); // Та самая процедура
const a=30;
var x0,y0:integer;
begin
x0:=x+7*dx;
y0:=y-3*dy;
x:=round((x-x0)*cos(a))+round((y-y0)*sin(a))+x0;
y:=round(-(x-x0)*sin(a))+round((y-y0)*cos(a))+y0;
ship(x,y,clred,clyellow,dx,dy);
end;

I clicked on the "Create"
c78bada2072c466eb594c631dd6de9c1.png
button: I clicked on the "Rotate by a corner" button:
de5cb97abab247a4b8f316245a1ec847.png
I understand that the program is a complete perversion, but still I ask for help :)
Any suggestions?
Thanks in advance for the replies.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
bobrovskyserg, 2015-05-19
@Dimentre

You're only changing the coordinate of the starting point of the drawing.
Rewrite tform1.ship so that each call to moveto/lineto uses the transformed coordinates (or write coordinate-replacing wrappers for these functions).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question