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

In summary, the code looks like you may be trying to rotate an image using cosine, but you may not be using the right API's.
  • #1
eNathan
352
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
  • #2
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 [itex]\theta[/itex] radians clockwise about the origin, you can multiply
[tex]
[x \; y] \times \left [ \begin{array}{lr} cos(\theta) \quad &-sin(\theta) \\
sin(\theta) &cos(\theta) \end{array} \right ] [/tex]

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

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

So you can multiply each point of your image by the same matrix and the entire image will be rotated.
 
  • #3
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 :)
 
  • #4
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);
 
  • #5
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 :)
 
  • #6
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?
 
  • #7
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.
 
  • #8
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.
 
  • #9
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.
 

1. How do I rotate an image in Photoshop?

In Photoshop, you can rotate an image by selecting the "Transform" tool and then clicking and dragging the rotation handle at the top of the image until it is at the desired angle. You can also go to the "Image" menu and select "Image Rotation" to choose from various rotation options.

2. What is the best way to rotate an image in Microsoft Word?

In Microsoft Word, you can rotate an image by selecting it and then using the rotation handle to rotate it manually. You can also go to the "Format" tab and click on "Rotate" to choose from various rotation options.

3. Is it possible to rotate an image without losing its quality?

Yes, it is possible to rotate an image without losing its quality. This can be done by using software that supports lossless rotation, such as Adobe Photoshop or GIMP. These programs use algorithms that preserve the original image quality even after rotating it.

4. How do I rotate an image using CSS?

To rotate an image using CSS, you can use the "transform" property with the "rotate" function. For example, "transform: rotate(45deg);" will rotate the image by 45 degrees. You can also use other values such as "90deg" or "180deg" to rotate the image to different angles.

5. Can I rotate an image using JavaScript?

Yes, it is possible to rotate an image using JavaScript. This can be done by accessing the image element in the HTML document and using the "style" property to set the "transform" property with the "rotate" function. You can also use other methods to rotate an image, such as using a canvas element or a third-party library like jQuery.

Similar threads

  • Computing and Technology
Replies
7
Views
818
  • Computing and Technology
Replies
4
Views
1K
  • Introductory Physics Homework Help
Replies
14
Views
310
  • Computing and Technology
Replies
25
Views
2K
Replies
4
Views
851
Replies
7
Views
971
Replies
10
Views
711
  • Calculus and Beyond Homework Help
Replies
4
Views
696
  • Other Physics Topics
Replies
4
Views
2K
  • Classical Physics
Replies
10
Views
869
Back
Top