1.11M Members

Rotating Delphi Canvas Art 360 Degrees

 
0
 

Heya guys, basically here is some code for a simple example program:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls;

type
  TForm1 = class(TForm)
    img1: TImage;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
  Image : TBitmap;
begin
  Image := TBitmap.create;
  Image.Width := 800;
  Image.Height := 600;
  img1.Picture.Graphic := Image;
  With img1.Picture.Bitmap.Canvas
    do
      begin
        Pen.Color := clRed;
        Pen.Width := 5;
        MoveTo(100,100);
        LineTo(100,120);
        MoveTo(95,120);
        LineTo(105,120);
        MoveTo(100,110);
        LineTo(110,113);
        MoveTo(100,110);
        LineTo(90,113);
      end;
end;

end.

It's basically a program which simply draws some random canvas art. Here is a picture of the program once it is run:
[IMG]http://i55.tinypic.com/24pxoqv.png[/IMG]

The problem I have is rotating this art. Rather than going the long winded way of increasing and decreasing numbers in the MoveTo and LineTo procedures is there a way I can rotate this canvas art at any angle (e.g. right now it is facing north or 0 degrees but rotating it 45 degrees would make it face north east). Now I'm not good at copying functions from the internet so if you are going to help me, please paste the additional code inside the code that I have provided above and provide a brief explanation of how it works.

Thanks guys, your awesome!

 
1
 

Rotating means recalculating your points, based on the angle and rotation point. Do you want to rotate from the center of the canvas, or from the bottom-left ?

 
0
 

Anyway is fine really, I guess I would prefer from the center.

 
0
 

I'll try to make an example on how to rotate a single point around the center. Just need some time though, I don't have it laying around.

 
1
 

I'll give you just the math, perhaps it will get you started. This assumes the point of rotation to be (0, 0). Assume point P(10, 20).

If you want to rotate this point 45 degrees, then you have to do the following:

angle := 45 * pi / 180; // conversion of degrees to radials

Q(x, y) is calculated as follows:

x := 10 * cos(angle) - 20 * sin(angle); // 10 = Px, 20 = Py
y := 20 * cos(angle) + 10 * sin(angle);
 
1
 

You have to create two images first. I'm sure anyone could convert this code to use just on image by using a TBitmap.

Procedure TurnImage(Src, Dst: TImage);
var x,y: integer;
begin
Dst.Width:= Src.Height; Dst.Height:= Src.Width;
For x:= 0 to Src.Width-1 do begin
For y:= 0 to Src.Height-1 do begin
Dst.Canvas.Pixels[(Src.Height-1)-y,x]:= Src.Canvas.Pixels[x,y];
end;
end;
end;

 
0
 

I managed to dig up useful code for this problem.
I'll try to upload a file for this.
I made everything using Delphi 9.
Best regards.

 
1
 

Hmm.. it looks like WinRar or a RAR file can not be added.
I am trying to add Zip file now :-)

Enjoy.

Attachments Example_of_Rotate_and_some_code.zip (289.71KB)
 
0
 

-Nevermind for now-

Question Answered as of 2 Years Ago by pritaeas, Morten Brendefu and anijacity
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: