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

Re: [pygame] substract rects



Pete Shinners wrote on 2003-06-17:

> Gerrit Holl wrote:
> > has anyone written code to substract rects yet?
> > I could do it myself but don't want to reinvent the wheel.
> > I didn't find it on the Code Repository.
>
> there is no subtract. just as useful could be an 'add'. it seems both
> these functions would take two rectangles and return a small list or
> tuple of 0 to 4 rectangles.
>
> i've always thought these would be cool, but never really had a strong
> need for them. there's even placeholders for them in the rect.c source. :]
>
I think the coolest abstraction would be a class masquarading as a Set
of point coordinates.  Like a "geometric locus of points".  Of course,
it would be implemented in some smart way, probably involving a
more-or-less minimal representation as a set of rects -- but it should
allow transparent set-like access.  This means the full range of
union/intersection/difference/... operations.  It should transparently
accept rects and treat them as sets of their contained points.  Then
one can do checks like ``(x, y) in locus`` to check if a point is
inside, ``locus > some_rect`` to check if a whole rect is covered,
``locus & some_rect`` to check whether there is a common point, etc.

It should also have pygame-aware additions: produce a sequence of
non-overlapping rects to feed into e.g. `display.update`, render
itself into an alpha mask, move the whole locus, scale it (the last is
problematic to define well).

Ideally, it would define a protocol so that you can create custom
implementations optimized for specific kinds of locuses (like the
rotated rectangle case that was discussed recently), and be able to
mix such classes freely.

-- 
Beni Cherniavsky <cben@tx.technion.ac.il>
If I don't hack on it, who will?  And if I don't GPL it, what am I?
And why not now?