![]() |
|
#1
|
||||
|
||||
Rotate ArrayI was working on some code to rotate simple image arrays in FLTK and ended up doing a lot of questing through math sites looking for a reasonable way to do it. Can anyone direct me (or explain) a better way to do this? Unfortunately trig was never one of my best subjects. Also, could someone explain in simple terms what a transformation matrix is. I can't seem to wrap my brain around it. I've read and read and while they seemed to be what I was looking for I really just couldn't make any sense of the math.
BACKGROUND: FLTK uses a static char array for it's internal representation of images. What it boils down to is a 3 or 4 char per bit array that is very similar to a xbm or xpm image representation. Since I intend on using common base images that will sometimes be rotated (for a battleship type game) I wanted to avoid having each rotation be a seperate image. By only declaring a new image buffer if I need one, I can have the base image as the only part stored in the exe. Here is the code, which as FLTK does, is more C than C++. If the comments are confusing just ask for clarification. CPP / C++ / C Code:
Code:
It seems to be working as I would like. When I refer to the image the top left is set at (0,0) and after the rotation it is "slid" back to that location. Any pointers about a better method would be appreciated. Any explanations about math would be greatly appreciated. Mark __________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work." --Thomas Alva Edison "Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety." --Benjamin Franklin "A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes." --Hugh Downs |
||||
|
#2
|
|||
|
|||
Re: Rotate ArrayQuote:
I have been trying to figure out how to express myself without contradicting my usual inclination to encourage people to look at things from a mathematical point of view. For this program, I would simply start with the desired result and "think my way back" to whatever steps would give that effect. In fact the equations that define rotation of objects (or, equivalently, rotation of coordinate systems) are the basis of "why it works", but the expressions themselves have to take into account that the equations were based on x increasing to the right and y increasing in the upward direction, whereas I think you are using y increasing in the down direction. That is to say that the coordinate (0,0) is the upper left hand corner of your matrix, corresponding to row 0 and column 0. Also, of course, you are simply listing the pixel values at each point on a display, rather than actually moving something from one place to another on a 2-dimensional linear scale (in centimeters or whatever). At least I think that's what you mean when you say that you slide it back to the (0,0) reference point. In fact, as your program shows, since we are rotating in multiples of 90 degrees (pi/2 radians), the sin and cosine factors are always zero, plus one, or minus one, so it's not necessary actually to call on the mathematical functions to do any of the calculations. Also, since the matrices aren't necessarily square, rotations of odd multiples of 90 degrees, which result in exchanging rows with columns, give an output matrix that is a different dimension than the original. Of course the total number of elements is always the same, and you are representing the pixel values in a 1-Dimensional array. I (probably) would have just looked at the results of some rotations in matrix form and induced program steps required to do the deed. See footnote. For example, if I changed your display function to show rows and columns of the various results, and if I loaded the original matrix with values that make it easy to see where things go, I might expect something like the following output for a test case. I perform the sequence Rotate 90 degrees lockwise, Rotate 90 degrees counter-clockwise, Rotate 180 degrees Code:
So, the cases for new_x and new_y in terms of old_x and old_y are seen (by inspection) to be something like: CPP / C++ / C Code:
Furthermore, it seems to me to be unnecessarily cluttersome to convert triplets of chars back and forth to rgb pixel values, so why not just work on arrays of pixels? This also makes it a little cleaner, in that you don't have quite so many arrays to allocate in the functions. So, I have no real quarrel with your organization or implementation, but you did ask for suggestions. CPP / C++ / C Code:
Output was listed previously. You might try to find places to optimize some more if it seem appropriate, but my experience is that I usually optimize the wrong thing, or I go to a lot of trouble trying to "help" the compiler and then the compiler completely blows my stuff away and does something more clever than I would have imagined. Bottom line: it's not a "better way" of doing it, it's just a slightly different way of looking at it. The method is exactly the same as yours. Regards, Dave Footnote Richard Feynman's Problem Solving Methodology: 1. Write down the problem. 2. Think real hard. 3. Write down the solution. ---Murray Gell-Mann Quoted in a New York Times interview |
|
#3
|
||||
|
||||
Re: Rotate ArrayFirst off, thanks very much Dave. There are a few nuggets in your explanation that really helps me to get a grip (with my en.wikipedia.org) on minor points I was struggling with. Perhaps I will be able to implement some, perhaps not. But at the very least I will be able to learn from them. I really do appreciate another's point of view. Your example program made me scratch my head and wonder, why did I make things so complex?
When I started poking around with this a few weeks ago I, in typical fashion, took a simple little problem and let it lead me by the nose down some long and winding roads. For me, that is the joy of programming. I approach it kind of like a short story that has the last few pages written (the desired result) but a bunch of blank pages on how to get there. Filling the blank pages, even if it requires stapling extra ones in is the joy of it. Quote:
There is also the fact that I will be working with x and y coordinates since anything will be picked from an FLTK canvas widget. That's my story and I'm sticking with it. Quote:
Quote:
NOTES: There is an error in the first post. Quote:
xbm or xpm should have been pbm or ppm I used the information on www.fileformat.info for those formats. Mark __________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work." --Thomas Alva Edison "Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety." --Benjamin Franklin "A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes." --Hugh Downs |
Recent GIDBlog
Vista ?Widgets? on Windows XP by LocalTech
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to sort in C++ alphabetically | wilen | C++ Forum | 5 | 20-Apr-2007 14:43 |
| 1-D array | jack999 | C Programming Language | 16 | 16-May-2006 12:38 |
| Need help deleting the last element in the array | headphone69 | C++ Forum | 2 | 15-Mar-2006 19:31 |
| Pointer Usage in C++: Beginner to Advanced | varunhome | C++ Forum | 0 | 19-Aug-2005 09:25 |
| template comiling problems - need expert debugger! | crq | C++ Forum | 1 | 01-Feb-2005 21:26 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The