• Advertisement

Archived

This topic is now archived and is closed to further replies.

rectangular logic

This topic is 6556 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is there a problem with my logic here? this is an exerpt from my PutSprite routine, where I pass in (among other things) dx, dy (the position to put to), and a rectangle is made from other parameters that need not be shown here. These are my lines of code to take care of what happens if the sprite would be partway off the screen. if (rect.bottom + dy > 479) rect.bottom = rect.top + (479-(rect.top+dy)); if (rect.right - rect.left + dx > 639) rect.right = rect.left + (639-(rect.left+dx)); if (dy < 0) //put off top of screen { rect.top = rect.top - dy; //clips off part that''s off screen dy = 0; //sets to put at y=0 } if (dx < 0) { rect.bottom = rect.bottom - dx;//see above dx = 0; //sets to put at x=0 } The first two check if it is off the bottom or right of the screen, and they work. But the second two, for the top and left, don''t. Any suggestions?

Share this post


Link to post
Share on other sites
Advertisement
I''m not sure if this is your problem, but from what I''ve understood, you have a rect wich will be offset by dx, dy.
when you compare dx < 0 , maybe you should use dx + rect.left.
Again I''m not sure what you use your rect for, or what results are you expecting/getting. from a brief look at your code, I''d say that if your rect has a .left and .top values > 0 then the top clip won''t work.
2nd note: are you sure you shouldnt be using 640 and 480?
remember that for DX rects are exclusive, the considered area ranges from left to right -1, and from top to bottom -1.

Alexandre Moura
alexmoura@hotmail.com

Share this post


Link to post
Share on other sites
yeah, the key to rects is that there is that the bottom-right corner is one pixel past the corner as seen on the screen. When you draw a rect, it fills in pixels in the top, left, right-1 and bottom-1 planes.

The reason it''s done this way is so that top - bottom = height and right - left = width. So if you have a 10x10 rectangle, the lines are (0,0) - (0,9) - (9,9) - (9,0). top = left = 0, right = bottom = 10.

This took me forever (and many off-by-one errors) to figure out. Once you get the hang of it, it''s not so bad.

Also, another tip with rectangles: if you''re trying to figure out how to do some formula, ALWAYS experiment with a 2x2 or 3x3 triangle on graph paper, then extrapolate to the full-screen. For example, I had to draw a sine wave, kept getting these off-by-one errors. When I did it on paper with a 2x2 rect, I got the formulas right and then plugged it into my real window.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Unsigned integers .... wil never go negative, make sure your rect.coordiantes are SIGNED variables

-kertropp

Share this post


Link to post
Share on other sites

  • Advertisement