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

Re: [pygame] Rect.union and rectangle positions



On 2 Dec 2006 at 9:42, Brian Fisher wrote:

> It seems to consider left and top just fine to me and seems to be a
> proper union exactly like I'd expect. Have you had problems with it?
> 
> attached is a quick test I used.
> 
> On 12/1/06, Lenard Lindstrom <len-l@xxxxxxxxx> wrote:
> > Does Rect.union ignore rectangle positions? That is, does it only
> > return a rectangle whose width and height is the maximum of the
> > widths and heights of the two compared rectangles rather than a
> > rectangle that will actually cover the two rectangles as they are
> > positioned?
> >
From your exampIe I see Rect.union returns a boundary rectangle 
enclosing its two source rectangles. That is what I want. I confused 
myself by exchanging the position and size arguments to Rect when 
doing an interactive test. Thanks to your help I have solved my 
problem.

For anyone who is curious this is the problem code that led to my 
question:

    foreground_rect = foreground.get_rect()
    shadow_rect = shadow.get_rect()
    dx, dy = shadow_offset
    shadow_rect.center = foreground_rect.center
    if dx > 0:
        shadow_rect.move_ip((dx, 0))
    elif dx < 0:
        foreground_rect.move_ip((-dx, 0))
    if dy > 0:
        shadow_rect.move_ip((0, dy))
    elif dy < 0:
        foreground_rect.move_ip((0, -dy))
    rect = foreground_rect.union(shadow_rect)
    image = pygame.Surface(rect.size, pygame.SRCALPHA, 32)
    image.blit(shadow, shadow_rect)
    image.blit(foreground, foreground_rect)

where "foreground" and "shadow" are both surfaces originally 
positioned at the origin. The intension was to have "image" contain 
both the "foreground" image as well as its shifted "shadow". But the 
"foreground" image was being clipped. I thought maybe Rect.union was 
not returning a large enough rectangle. But that is not the problem. 
Instead I failed to adjust the "foreground" and "shadow" positions 
when blitting to "image". The following changes solve the problem:

    image.blit(shadow, (shadow_rect.left - rect.left,
                        shadow_rect.top - rect.top))
    image.blit(foreground, (foreground_rect.left - rect.left,
                            foreground_rect.top - rect.top))

Now if only tuples supported array arithmetic
( shadow_rect.topleft - rect.topleft ) or Rects had a negative move
method.  :-)

Lenard Lindstrom
<len-l@xxxxxxxxx>