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

Discussion Overview

The discussion revolves around using the Cosine function in Delphi to rotate an image. Participants explore the mathematical principles behind image rotation, specifically focusing on how to apply trigonometric functions to compute new pixel positions. The conversation includes technical explanations, code snippets, and troubleshooting efforts related to image manipulation in Delphi.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant seeks clarification on how to use the Cosine function for image rotation, expressing a need for practical examples.
  • Another participant suggests using a rotation matrix to calculate new coordinates for points in the image, providing a mathematical formulation for the transformation.
  • A request is made for specific equations for X and Y coordinates to facilitate pixel manipulation in the bitmap.
  • Subsequent replies provide equations for calculating new pixel positions based on the rotation angle.
  • One participant shares their code attempt for rotating an image but reports that it does not yield visible results, prompting questions about the output.
  • Concerns are raised about the use of degrees versus radians in the calculations, with suggestions to verify the cosine values being used.
  • Another participant highlights potential issues with pixel coordinates going off-screen and suggests the need for translating points after rotation.
  • Clarifications about the syntax of Delphi's SetPixel and GetPixel functions are discussed, with some participants expressing confusion over the code structure.
  • A participant inquires about modifying equations to incorporate the built-in pi constant for angle calculations.

Areas of Agreement / Disagreement

Participants express varying levels of understanding regarding the implementation of image rotation in Delphi, with some agreeing on the mathematical principles while others remain uncertain about the coding aspects and syntax. The discussion does not reach a consensus on the correct implementation or resolution of the issues raised.

Contextual Notes

Participants note potential limitations related to the coordinate system in Delphi, the handling of angles in degrees versus radians, and the correct usage of pixel manipulation functions. There are unresolved questions about the proper application of the SetPixel and GetPixel APIs.

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 [itex]\theta[/itex] radians clockwise about the origin, you can multiply
[tex] [x \; y] \times \left [ \begin{array}{lr} cos(\theta) \quad &-sin(\theta) \\<br /> 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}) \\<br /> 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.
 
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
2K
Replies
4
Views
1K
  • · Replies 10 ·
Replies
10
Views
2K
  • · 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