Message Board
Message Board > Fenix / Bennu / Gemix / DIV > Whats wrong with my rotation code? |
May 22, 2007, 09:19 | |
Quiest
now with more happynes 142 posts |
Just copy, paste & compile it please, you`ll see what happens... Code: program rotate; private graph1; graph2; int h=75, w=75; int x_mod, y_mod; int new_w, new_h; int rot; int dist; int ang_mod; int time=0; int shift=8; begin set_mode(320, 240, 16); set_fps(45, 0); write_int(0,5,5,0, &fps); write_int(0,5,15,0, &rot); graph1 = new_map(h, w, 16); map_clear(0, graph1, rgb(0, 255, 0)); new_h = sqrt(h*h + w*w); new_w = new_h; graph2 = new_map( new_w, new_h, 16); map_clear(0, graph2, rgb(0, 0, 255)); rot = 0; while ( !key(_esc) ) clear_Screen(); map_clear(0, graph2, rgb(0, 0, 255)); if(time==0) rot+=500; else time--; end; if(key(_left)) rot += 500; time=90; end; if(key(_right)) rot -= 500; time=90; end; for ( x=0; x<w; x++ ) for ( y=0; y<h; y++ ) ang_mod = rot + fget_angle(x, y, w/2, h/2); dist = fget_dist(x<<shift, y<<shift, (w/2)<<shift, (h/2)<<shift); x_mod = get_distx(ang_mod , dist) + (new_w/2<<shift); y_mod = get_disty(ang_mod , dist) + (new_h/2<<shift); map_put_pixel(0, graph2, x_mod>>shift, y_mod>>shift, map_get_pixel(0, graph1, x, y)); end; end; put(0, graph1, 80, 120); put(0, graph2, 220, 120); frame; end; end; EDIT: wohoooooo! the code sections got fixed! [Edited on May 22, 2007 by Quiest] ____________ Roundhousekick to the face, baby! |
# |
May 22, 2007, 10:18 | |
Eckolin
Quite Whiskered 388 posts |
You'll want to treat the target area as a square and work out the corresponding source pixels.
____________ Maker of Games... Wisdom is supreme; therefore get wisdom. Need help with coding? I probably wrote something similar. |
# |
May 22, 2007, 15:31 | |
Sandman
F3n!x0r 1194 posts |
You probably have a good reason, but I still wonder why you don't use map_xput(), which is five times faster. Are you just fiddling around? Or maybe coding this in C or even ASM one day?
____________ BennuWiki Yes, my avatar has grey borders in IE (so get a decent browser) ROOFLEZ ROOFLEZ |
# |
May 24, 2007, 08:03 | |
Quiest
now with more happynes 142 posts |
Yes, I wanna get this code over to C++ as I`m doing some Fenix like functions in SDL... tho this will be just for loading rotated images, not for real time rotating. And thanks again Ecko, i still haven`t had the time to try it your way, but I will today. ____________ Roundhousekick to the face, baby! |
# |
May 24, 2007, 10:31 | |
PEader
お前はもう死んでいる 1486 posts |
I can't run this so I'm working with assumptions. Is the problem that you have gaps in the rotated image? When you rotate the original image some of the pixels cover more then one pixel in the target image? Code: ---------------------------------------------------------------------- Section 3. 2D Image/Pixel Computations ---------------------------------------------------------------------- Subject 3.01: How do I rotate a bitmap? The easiest way, according to the comp.graphics faq, is to take the rotation transformation and invert it. Then you just iterate over the destination image, apply this inverse transformation and find which source pixel to copy there. A much nicer way comes from the observation that the rotation matrix: R(T) = { { cos(T), -sin(T) }, { sin(T), cos(T) } } is formed my multiplying three matrices, namely: R(T) = M1(T) * M2(T) * M3(T) where M1(T) = { { 1, -tan(T/2) }, { 0, 1 } } M2(T) = { { 1, 0 }, { sin(T), 1 } } M3(T) = { { 1, -tan(T/2) }, { 0, 1 } } Each transformation can be performed in a separate pass, and because these transformations are either row-preserving or column-preserving, anti-aliasing is quite easy. Reference: Paeth, A. W., "A Fast Algorithm for General Raster Rotation", Proceedings Graphics Interface '89, Canadian Information Processing Society, 1986, 77-81 [Note - e-mail copies of this paper are no longer available] [Gems I] There is some information on gamedev.net as well. ____________ I see 57,005 people. |
# |
Message Board > Fenix / Bennu / Gemix / DIV > Whats wrong with my rotation code?