[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [pygame] line boundingbox



> I'm using Draw.Line() and trying to manage my dirty rects
> properly. Line() in sopposed to return the "bouding rectangle
> of the effected area." However, it's not doing what I expect:
> 
> An example line (x1, y1, x2, y2) is:
> 
>     [316.9968184989844, 238.00478049529437] [323.00477415393209,
> 242.99521823643315]
> 
> The return value from line is:
> 
>     <rect(0, 0, 324, 243)>

i've already fixed the line bounding box problems. they were
definitely 'incorrect' for the 1.0 release. you have two
options. grab the latest CVS version of pygame, or wait for
the pygame-1.1 release which isn't far away.

if you'd like something that works with 1.0 and you "know"
that you aren't using clipping rectangles on your destination
surface, you can cheat it with something like this...

pygame.draw.line(screen, color, pt1, pt2)
boundingbox = Rect(pt1, (1,1).union(pt2, (1,1))

i've just gone back and retested that the current pygame
line drawing does work. i've attached a sample program
you can test with, here is the output...

D:\src\test>python testline.py
POINT 0 : [316.9968184989844, 238.00478049529437]
POINT 1 : [323.00477415393209, 242.99521823643315]
BOUNDBOX: <rect(316, 238, 8, 5)>

exactly the numbers i would expect



> Also, it would be nice if Rect operations (such as union)
> were in-place (destructive) operations rather than creating
> new objs -- it's easy enough to copy them, but in this case
> (keeping a running Union) it would be more efficient.

well, this is a tough spot. there are times you definitely
want both "inplace" and "nondestructive" operation on these
rects. i chose to go with the way they are, because it ends
up being easier to do both. (easier than it is to get both
types of operation when only having the inplace version,
that is)

the functions are going to remain the way they are, but
one option that's been tickling my head is to add a second
set of functions that do the same things but put the results
inplace. either some sort of prefix/suffix, or a change to
the "verb" tense...

rect.ip_union(), rect.union_ip(), rect.self_union()
rect.unioning(), rect.unioned()

the Rect is a very handy utility class, and i'm not opposed
to making it a bit more flexible. i don't think these additional
functions would make it too "overwhelming" either?
any opinions out there?

#test a lines boundingbox

from pygame import *
import pygame.draw as draw


init()
screen = display.set_mode((640, 480))

points = ([316.9968184989844, 238.00478049529437],
          [323.00477415393209, 242.99521823643315])

box = draw.line(screen, (100, 250, 100), *points)
display.flip()

print 'POINT 0 :', points[0]
print 'POINT 1 :', points[1]
print 'BOUNDBOX:', box

time.delay(500)