Archived

This topic is now archived and is closed to further replies.

2d rotation leaving holes.....

This topic is 5778 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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 do
begin
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites