Rotate Image Using Cosine Function in Delphi: A How-To Guide with Explanation

  • Thread starter Thread starter eNathan
  • Start date Start date
  • Tags Tags
    Image Rotate
Click For Summary
SUMMARY

The discussion focuses on using the Cosine function in Delphi to rotate images. A user seeks clarification on the mathematical principles behind image rotation, specifically using a rotation matrix. The correct equations for new pixel positions are established as x_new=(x_old)*cos(theta)+(y_old)*sin(theta) and y_new=(y_old)*cos(theta)-(x_old)*sin(theta). Additionally, the importance of using radians instead of degrees for angle measurements is emphasized, along with considerations for pixel coordinate systems in Delphi.

PREREQUISITES
  • Understanding of Delphi programming language
  • Knowledge of trigonometric functions, specifically cosine and sine
  • Familiarity with 2D graphics concepts and pixel manipulation
  • Basic understanding of matrix multiplication for transformations
NEXT STEPS
  • Research "Delphi graphics programming" for advanced image manipulation techniques
  • Learn about "Delphi coordinate systems" to understand pixel positioning
  • Explore "trigonometric functions in Delphi" for accurate angle calculations
  • Study "matrix transformations in computer graphics" for deeper insights into image rotation
USEFUL FOR

Developers working with Delphi who are interested in image processing, graphic designers looking to implement rotation features, and anyone seeking to understand mathematical transformations in programming.

eNathan
Messages
351
Reaction score
2
I am using the Delphi language, and I want to learn how to use the Cosine function to rotate an image. Can somebody please explain how and why the math works. I know what cosine is, I just don't know how to use it in any real situations, can somebody please tell me how to do this?

Delphi = VB + C++
 
Computer science news on Phys.org
I don't know that you can rotate an image using cosine alone, and I don't know how your image is coded, but maybe you can translate this into something you can use.

Suppose a given point of your image is coded as a row vector [x y], if you want to rotate that point through an angle of \theta radians clockwise about the origin, you can multiply
<br /> [x \; y] \times \left [ \begin{array}{lr} cos(\theta) \quad &amp;-sin(\theta) \\<br /> sin(\theta) &amp;cos(\theta) \end{array} \right ]

to get the new coordinates for that point. For example, the point [1 2] rotated 90^o (\frac{\pi}{2} \, radians:

<br /> [ 1\quad 2] \times \left [ \begin{array}{lr} cos(\frac{\pi}{2}) \quad &amp;-sin(\frac{\pi}{2}) \\<br /> sin(\frac{\pi}{2}) &amp;cos(\frac{\pi}{2}) \end{array} \right ] = [2 \;-1]

So you can multiply each point of your image by the same matrix and the entire image will be rotated.
 
Sorry for my lack of understanding mathematical signs, but can you make 2 equations (1 for the X, 1 for the Y) that will give the "new" position of each pixel? And from there, I can just loop through every pixel and set its new position on the Bitmap.

thx :)
 
Sure, let's say you want to rotate some angle theta then do the following:

x_new=(x_old)*cos(theta)+(y_old)*sin(theta);
y_new=(y_old)*cos(theta)-(x_old)*sin(theta);
 
For i:= 0 to Image1.Width Do
For p:= 0 to Image1.Height do begin
x:=Round((i)*cos(45)+(p)*sin(45));
y:=Round((p)*cos(45)-(i)*sin(45));
SetPixel(GetDC(Image2.Picture.Bitmap.Canvas.Handle), x, y, GetPixel(GetDC(Image1.Picture.Bitmap.Canvas.Handle), i, p))
End;

Well that didn't work :P Thanks for the math though, I am sure I will get it sooner or later :)
 
What did the result of that look like? Did you end up with any image at all?

A couple of thoughts: you expressed the angle in degrees. I don't know about Delphi, but I'm guessing you should use radians. Test it by printing the value of cos(45). If you get .707107, fine. If you get .525322, that's a problem. Then you have to use cos(pi/4) to get the cosine of 45 degrees. And you may not have a built-in pi constant. In C++ you could declare
Code:
[b]const double[/b] pi = acos(-1);
Maybe you can do something equivalent in Delphi. Or you can just approximate it with 3.14159.

Also, the image may be rotating off your screen. Are you allowed negative coordinates for the pixels? I'm just starting to learn about this myself, but I think I saw someplace that the origin for the screen is either in the upper left corner (with the positive y-axis pointing down) or the lower left corner (with the positive y-axis going up). There seems to be no place in that system for negative numbers, so you probably have to translate your points laterally and vertically after you rotate them, to get back on the screen.

Does any of that make sense?
 
yes, when I do Cos(45) I get .525322 :( What I see on the screen is nothing, it does not even draw an pixels anywhere. Am I using the SetPixel and GetPixel right? I am pretty sure I am. And in Delphi, the X and Y cords start at the upper left corner of the screen starting with 0, and increasing by +1 for every pixel. And yes, delphi has a built in pi Const. I am going to work with it some more, please respond with any coments.
 
As I said, I don't know Delphi so I have no idea how the syntax of SetPixel and GetPixel is supposed to work. But your code looks strange to me: it seems you have an inner loop and an outer loop (which makes sense), but only one begin/end. Is that right?

If you post code, use code tags. Put your code between [co de]put code here[/co de] (without the spaces in "code") to preserve indentation & make it easier to read.
 
You only use the begin statement when it is a "Code block" for isntance

For i:= 1 to 10 do
x:=i;
//There is no need for the "Begin" keyword because there is just one line of code after the for

For i:= 1 to 10 do begin
x:=i;
y:=i*10;
End;
//Because I have 2 lines of code, it is a requirment to use the "Begin" statement.

Anyway, I think the problem is that I am not using the GetPixel and SetPixed API's right. I am going to google for graphics in Delphi and see what I can figure out.
 
  • #10
You said "Then you have to use cos(pi/4) to get the cosine of 45 degrees." Can you changethe equation to get this?
 
  • #11
I don't understand your question.

If you have the constant pi already built in, just write pi/4 everyplace where you were writing 45.

Enter cos(pi/4) & see what you get.
 

Similar threads

  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 7 ·
Replies
7
Views
1K
Replies
4
Views
597
  • · Replies 10 ·
Replies
10
Views
815
  • · Replies 4 ·
Replies
4
Views
2K
Replies
14
Views
2K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K