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

Re: [pygame] Native PyGame method for automatically scaling inputs to a surface resolution?

Christopher Night wrote:

I've been thinking about this, and even though in the past I've rounded to int when passing coordinates to drawing functions, I actually think the correct behavior is to truncate. This makes sense if you consider the pixel "at" (0,0) to actually occupy a 1x1 rectangle, extending from (0,0) to (1,1). So the point (0.7, 0.7) should actually be considered to be within the pixel at (0,0).

If your intention is to draw a 1x1 rectangle at some location
on the screen, the correct approach would be to calculate the
transformed coordinates of all four sides of the rectangle,
round them to ints, and then fill the resulting rect.

One of the problems with truncating is that it behaves badly
when you've calculated what should be a float approximation of
an integer coordinate -- if it's slightly off in the negative
direction, you end up a whole pixel out.

In my experience, rounding is almost always the right thing
to do, and if it seems not to be, then you're not thinking
about the problem the right way.