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

Re: [pygame] issue with pygame.rect.inflate_ip()



David,

The inflate() method is designed such that r1.center == r1.inflate(x,y).center. You can easily prove this to yourself by running code, it works in practice. Additionally (r1.size[0]+x, r1.size[1]+y) == r2.inflate(x,y).size.

If you have a line that's 10 units long and whose start point is at 0 and end point is at 10, then the center is 5. If you inflate it by 5 units, the total width is now 15, and the line starts at point 0 and ends at 15. But now the center is at 7 instead of 5. In order to adjust you move the line over by 5/2 units, in this case 2 units, so now the line starts at -2 and ends at 13 with the center again at 5.

If you used your second code section instead of the real thing, what would happen when you tried to inflate a line by 5 is that the total width changes to 12 and it only gets shifted back by 2. The new center is therefore at 4, instead of 5, which is not what you want. Also the bottomright corner is represented by x+width and y+height.

On Sun, Feb 5, 2012 at 3:13 AM, david BERTRAND <dvd.brtrnd@xxxxxxxxx> wrote:
Hi Lenard,

I agree with you (and with the theory), but in practice, the new object size and the new center position are not correct ( in both case, the error is (x/2, y/2).

regards,
David

2012/2/4 Lenard Lindstrom <len-l@xxxxxxxxx>
On 04/02/12 07:12 AM, david BERTRAND wrote:
Hi,

I'm new here but I'm working whith pygame (1.9.2) since few month.
When I use inflate() or inflate_ip, the resulting size and the new rect.center are never as expected.

reading the sources (1.9.1, rect.c), I found :

self->r.x -= x / 2;
self->r.y -= y / 2;
self->r.w += x;
self->r.h += y;

if (r.w, r.h) is the size of the rect this is correct.
But it woks like if (r.w, r.h) Ârepresents the rect.bottomright corner.
Andin this case, it seems better to write something like :

self->r.x -= x / 2;
self->r.y -= y / 2;
self->r.w += x / 2;
self->r.h += y / 2;

Please, excuse me if I am wrong : I am not a great C reader.

Hi David,

A rectangle is stored as the position of the top-left corner (r.x, r.y), and the size (r.w, r.h). It is the same with rectangles in Python: Rect(left, top, width, height).

Lenard Lindstrom