2d rotation leaving holes.....

Started by
3 comments, last by baddrizz 22 years, 1 month ago
hello i''m trying to rotate a bitmap in 2d but my code leaves out some pixels and makes it look sorta messy so i''m hoping somebody could help me out with it ohh i''m using win32 and it''s just a simple red square if that matters any


for(int y = 0; y < 40; y++)
{
     for(int x = 0; x < 40; x++)
     {
	DWORD color = GetPixel(mdc, x, y);
	
        int a, b;
					
	a = (20-x)*cos(angle) - (20-y)*sin(angle)+20;
	b = (20-x)*sin(angle) + (20-y)*cos(angle)+20;
					
	SetPixel(cdc, 100+a, 100+b, color);
     }
}

 
thx for any help
Advertisement

Yeah... You have to do "reverse-mapping" so there are no
holes. Start with the transformed coordinates and work
"backwards" to get the source coordinates and then fill in
the pixel.


Premature optimizations can only slow down your project even more.
神はサイコロを振らない!
The reason you're getting the holes is because your a and b variables are being rounded to the closest pixel in the destination surface. If you want a perfect image, you can work through the destination HDC, and use the sin and cos funtions to find the source colour for each pixel. This way, you'll have no holes whatsoever.

    for DestY := 0 to SrcHeight dobegin      for DestX := 0 to SrcWidth do      begin           {determine the source pixel to use for this destination pixel}           SrcX:=CenterX + (DestX - CenterX)*Cos(angle) -                      (DestY - CenterY)*Sin(angle);           SrcY:=CenterY + (DestX - CenterX)*Sin(angle) +                      (DestY - CenterY)*Cos(angle);           {if this pixel is within the source image...}           if (SrcX > 0) and (SrcX < SrcWidth) and              (SrcY > 0) and (SrcY < SrcHeight) then           {...copy it to the destination}           DestImg[DestX][DestY]:=SrcImg[SrcX][SrcY];      end;end;    

Beat me to it Tangentz.

Edited by - MarkyD on February 20, 2002 3:17:51 PM
quote:Original post by baddrizz
hello i''m trying to rotate a bitmap in 2d but my code leaves out some pixels and makes it look sorta messy so i''m hoping somebody could help me out with it ohh i''m using win32 and it''s just a simple red square if that matters any

for(int y = 0; y < 40; y++){     for(int x = 0; x < 40; x++)     {	DWORD color = GetPixel(mdc, x, y);	        int a, b;						a = (20-x)*cos(angle) - (20-y)*sin(angle)+20;	b = (20-x)*sin(angle) + (20-y)*cos(angle)+20;						SetPixel(cdc, 100+a, 100+b, color);     }}  


thx for any help



Instead of mapping source pixels to dest coords, try doing the reverse, ie calculate the rotated end points, then interpolate the coords down and across the edges and lookup the source pixel. Well, basically the same algorithm as affine texture mapping.
thx guys that helped alot actually it work perfect like u said thx again

This topic is closed to new replies.

Advertisement