We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,379 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Rotating Delphi Canvas Art 360 Degrees

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!

5
Contributors
8
Replies
4 Months
Discussion Span
1 Year Ago
Last Updated
9
Views
Question
Answered
DelphiGuy
Light Poster
41 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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 ?

pritaeas
Posting Prodigy
Moderator
9,313 posts since Jul 2006
Reputation Points: 1,178
Solved Threads: 1,465
Skill Endorsements: 86

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

DelphiGuy
Light Poster
41 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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.

pritaeas
Posting Prodigy
Moderator
9,313 posts since Jul 2006
Reputation Points: 1,178
Solved Threads: 1,465
Skill Endorsements: 86

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);
pritaeas
Posting Prodigy
Moderator
9,313 posts since Jul 2006
Reputation Points: 1,178
Solved Threads: 1,465
Skill Endorsements: 86

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;

anijacity
Newbie Poster
4 posts since Oct 2011
Reputation Points: 11
Solved Threads: 1
Skill Endorsements: 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.

Morten Brendefu
Light Poster
44 posts since Mar 2011
Reputation Points: 38
Solved Threads: 2
Skill Endorsements: 0

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)
Morten Brendefu
Light Poster
44 posts since Mar 2011
Reputation Points: 38
Solved Threads: 2
Skill Endorsements: 0

-Nevermind for now-

DelphiGuy
Light Poster
41 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 1 Year Ago by pritaeas, Morten Brendefu and anijacity

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0827 seconds using 2.7MB