Mega Code Archive

 
Categories / Delphi / Graphic
 

Drawing at an angle

Title: Drawing at an angle Question: Rotating an image Answer: paste a button and a image on a form and select a bitmap for the bitmap prefarely not too big about 100x100 and paste this unit. unit drawunit; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls; type TForm1 = class(TForm) Image1: TImage; Button1: TButton; procedure Button1Click(Sender: TObject); procedure FormClose(Sender: TObject; var Action: TCloseAction); private { Private declarations } public rotateimage:timage; end; var Form1: TForm1; implementation {$R *.DFM} procedure TForm1.Button1Click(Sender: TObject); const rotation = 2/3*pi; var x,y : integer; newx,newy : integer; radius,a : real; begin form1.WindowState:=wsMaximized; image1.Visible:=false; button1.Visible:=false; rotateimage:=timage.Create(self); rotateimage.parent:=self; rotateimage.Left:=0; rotateimage.Top:=0; rotateimage.width:=740; rotateimage.Height:=540; for x:=1 to image1.Picture.Width do begin for y:=1 to image1.Height do begin radius:=Sqrt(Sqr(X)+Sqr(Y)); a:=Arctan(Y/X); newx:=round(Radius*Cos(A+Rotation)+300); newy:=round(Radius*Sin(A+Rotation)+300); rotateimage.Canvas.Pixels[newx,newy]:=image1.Canvas.Pixels[x,y]; end;//nested for do end;//for do end; procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction); begin rotateimage.free; end; end.