Advertisement

calculating this whit difrend way: (y + x * width)

Started by December 24, 2002 05:46 PM
24 comments, last by Craazer 22 years, 1 month ago
I''d say you are doing this on purpose, but what the heck...


// WIDTH and HEIGHT are #defines
int world[WIDTH*HEIGHT];

// some random hit location
double hitx = 3.5; // which is less than WIDTH
double hity = 2.2; // which is ledd than HEIGHT

// we can''t have decimals so we have to round the hit
int index_x = round( hitx );
int index_y = round( hity );

// and we only have single index on the world array
int index = index_x + index_y * WIDTH;

// and then you put your bomb down
world[ index ] = BOMB;


If you don''t understand that, go back to elementary school, do not pass GO





---------------------------
I may be getting older, but I refuse to grow up
I may be getting older, but I refuse to grow up
quote:
Original post by Craazer
hmmmm...

x=3.7
y=2.4

x + y = 6.1

// (x+0.5) = 4.2 | 4 (y + 0.5) = 2.9 | 2
4 + 3 = 7


when you cast, it just drops the decimal. 4.2 becomes 4, 4.999999999 becomes 4. 2.9 = 2, 2.1 = 2. when you add .5, it makes the computer round correctly.
Advertisement
quote:
Original post by billybob
Original post by Craazer
hmmmm…

x=3.7
y=2.4

x + y = 6.1

// (x+0.5) = 4.2 | 4 (y + 0.5) = 2.9 | 2
4 + 3 = 7


when you cast, it just drops the decimal. 4.2 becomes 4, 4.999999999 becomes 4. 2.9 = 2, 2.1 = 2. when you add .5, it makes the computer round correctly.



Thats what i though. Thanks!
I did not know that ''rounding'' thing, i wonder why its so unaccurate in combuters?
Well nevermind about that, thanks again.

quote:
Original post by Dreamforger
I''d say you are doing this on purpose, but what the heck...


// WIDTH and HEIGHT are #defines
int world[WIDTH*HEIGHT];

// some random hit location
double hitx = 3.5; // which is less than WIDTH
double hity = 2.2; // which is ledd than HEIGHT

// we can''t have decimals so we have to round the hit
int index_x = round( hitx );
int index_y = round( hity );

// and we only have single index on the world array
int index = index_x + index_y * WIDTH;

// and then you put your bomb down
world[ index ] = BOMB;


If you don''t understand that, go back to elementary school, do not pass GO





---------------------------
I may be getting older, but I refuse to grow up



Yes yes im not doing it on purpose, I just didnt know that rounding in first place.
And about that elementary school, sigh, im working on it.

Perhaps it would help if you explain how you think the value of (x+y) is relevant to anything.
quote:
Original post by Bagel Man
Perhaps it would help if you explain how you think the value of (x+y) is relevant to anything.


It doesnt mather any more, problem solved.

This topic is closed to new replies.

Advertisement