[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Tiling a Rotated Bitmap



Kamilche wrote:
Hi all. I'm trying to create a rotated tilable bitmap out of a tilable bitmap, but it's not working. My first attempt was to tile the bitmap across a larger picture, rotate the larger picture, and then cut a tile out of the middle, but that didn't work - the resulting picture wasn't tilable.

I see that the problem is a lot more complex than I thought, and I don't have a handle on how to do it.

I would expect you could do this. You'll need to rotate the surface, and then blit it back onto itself 8 times with offsets. The offsets will be the rotated corners of the original image rect.

There will likely be interpolation and small pixel seams along the edges. You could probably cheat it by bringing the blit offsets back 1 pixel?


# rotate surface
angle = 15
rotsurf = pygame.transform.rotate(surf, angle)

# compute the offsets needed
c = cos(radians(angle))
s = sin(radians(angle))
widthx = (surf.width() - 1) * c
widthy = (surf.width() - 1) * a
heightx = (surf.height() - 1) * a
heighty = (surf.height() - 1) * c

# the 8 positions, corners first
positions = [
    (widthx + heightx, widthy + heighty),
    (widthx - heightx, widthy - heighty),
    (-widthx + heightx, -widthy + heighty),
    (-widthx - heightx, -widthy - heighty),
    (widthx, widthy),
    (-widthx, -widthy),
    (heightx, heighty),
    (-heightx, -heighty),
]

# apply the blits
for pos in positions:
    rotsurf.blit(rotsurf, pos)


This is all theoretical. In my mind it seems like this should fly.