Jump to content
  • Advertisement
Sign in to follow this  
fir

0..99 or 0..100

This topic is 2040 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

I have my own routines for FillRect, DrawLine etc and I wonder if

I should make tehe coordinates in a way of FillRect(0,0,99,99)

or FillRect(0,0,100,100) (when setting rectangle of 100x100 pixels

 

It seems that I am gatting closer to the first option, it seems that

some routines are easier to catch in this version

 

for example

inline int RectangleOverlapsFrame(int x1, int y1, int x2, int y2)
{
  extern int frame_x;
  extern int frame_y;
 
   return x1<=frame_x && x2>=0 && y1<=frame_y && y2>=0 ;
}

this is for checking if rectangle x1,y1 - x2,y2 overlaps the screen
(called frame here) 
the comparsions here are physical -
 
if left of the rectangle (x1) lies on right of the screen (x1==frame)
or left to that (x1<=frame) etc... other 3 checks
 
if x2, y2 and frame_x, frame_y would be treated as values off the
regions this is probably nonsimetric and harder to understand
 
thought i suspect most api uses the 0..100 approach?
 
what are the opinions?
Edited by fir

Share this post


Link to post
Share on other sites
Advertisement

The only difference I can think of is when you want to get the width/height:

0..100:

width = x2 - x1 = 100 - 0

compared to the 0..99 case, where you also have to add 1 to get the width:

width = x2 - x1 + 1 = 99 - 0 + 1

Share this post


Link to post
Share on other sites

This is normally referred to as inclusive or exclusive and each API makes its own decision. As long as you are consistent, it doesn't matter. There is no "right" way to do it. The chances are that <= is just as fast as < in the CPU sense.

 

OK, though imo this is some topic to say a bit about.. I didnt know the exclusive / inclusive terminology this is ok.

 

I thought MOST api though uses exclusive (0..100) is it not true?

 

And as i said inclusive algorithm may be easier to read IMO, so 

isnt the inclusive better?

Share this post


Link to post
Share on other sites

 

The only difference I can think of is when you want to get the width/height:

0..100:

width = x2 - x1 = 100 - 0

compared to the 0..99 case, where you also have to add 1 to get the width:

width = x2 - x1 + 1 = 99 - 0 + 1

 

You don't need the '+1' in any of the cases.  That is why Aarvajk mentioned that the operator '<' and '<=' (or '>' for that matter) takes the same amount of time on the CPU.  Going from '<' to '<=' basically means '+1' in integer math.

 

 

This is normally referred to as inclusive or exclusive and each API makes its own decision. As long as you are consistent, it doesn't matter. There is no "right" way to do it. The chances are that <= is just as fast as < in the CPU sense.

 

OK, though imo this is some topic to say a bit about.. I didnt know the exclusive / inclusive terminology this is ok.

 

I thought MOST api though uses exclusive (0..100) is it not true?

 

And as i said inclusive algorithm may be easier to read IMO, so 

isnt the inclusive better?

 

 

You can really do what you want, but when you see a '0' bound, it is usually inclusive, otherwise they would just say '1'.  Upper bound may vary.  Also, terminology may give a hint.  'Count', 'length', 'size', usually means exclusive, since you normally start counting from 0 in the programmer world.  (Arrays go from 0 to one less than the size.)  If you make 0 a dummy value, and start from 1, you suddenly have a inclusive end.  If you see 'stop', 'end' etc, it often is inclusive.  Morale: never really suppose or assume something.   In either case you have to check the documentation, because you can never be 100% sure.

 

Edit: And I forgot to mention what Endurion said above.  Although you can do what you want, I think what he says is the normal 'convention'.

Edited by aregee

Share this post


Link to post
Share on other sites

I thought MOST api though uses exclusive (0..100) is it not true?

If you are so worried about it, just do what Direct3D did and support them both:
ID3DXLine::SetGLLines()
 
 

This is normally referred to as inclusive or exclusive

OpenGL actually uses the “Diamond Exit” method, which is very similar to exclusion, but more complex and doesn’t make a lot of sense.
3.4.1 Basic Line Segment Rasterization
 
 
L. Spiro

Share this post


Link to post
Share on other sites

You could argue that if you set the end value to be exclusive, you will not be able to use the full integer range for your values, since you must always pass in the desired value plus 1, which wont work when the desired value is the maximum possible.

 

If both are inclusive you can use the full range, and IME more intuitive too.

Share this post


Link to post
Share on other sites

You could argue that if you set the end value to be exclusive, you will not be able to use the full integer range for your values, since you must always pass in the desired value plus 1, which wont work when the desired value is the maximum possible.

 

If both are inclusive you can use the full range, and IME more intuitive too.

 

I could say that say drawing the line form 10, 10 ,mto 17, 19

who really draws from 10,10 to 16, 18 is silly

 

also 0,0 to 1, 1 which really draws a dot 0,0

 

isnt it silly?

 

ps I am not talking about numbering from 1  Numbering from 0

is better, also c table syntax which numbers by length, may have not

to do much here like

 

int tab[100] ;//OK

 

is ok

but if defined by bound it should be

 

int tab[0..99]; //OK

not

 

int tab[0..100]; //WRONG

 

(side note)

Share this post


Link to post
Share on other sites


OpenGL actually uses the “Diamond Exit” method, which is very similar to exclusion, but more complex and doesn’t make a lot of sense.

 

Yeah, triangle rasterizers have to be slightly different (this is aimed at the thread, not you Spiro, quite sure you don't need this information :)) as the system for including/excluding pixels when the triangles are rendered has to be done in such a way that two triangles sharing a side don't overwrite each other to avoid needless pixel writing and blending artifacts.

 

In that case you can argue that one way or another is "better" but which way you choose to express rectangle co-ordinates for filling with pixels or for doing collision tests is entirely a matter of personal preference.

 

Just do whichever feels right to you, fir. Its fine, really.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!