Archived

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

Craazer

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

Recommended Posts

I would need to calculate coords of 2D array like this: (y + x * width) The problem is that i need to do this also: (y + x) and after that multiply whit width, hope u know what i mean? obviously ((y + x) * width) doesnt work.

Share this post


Link to post
Share on other sites
I explain better:
I have used
(x * width + y)
i have also used
(y * width + x),(x + y * width)
they ALL give the SAME result (just difrend order)

Ok
What i need to do is calculate (x + y) before anything else becose x and y are floats, u know?

Share this post


Link to post
Share on other sites
Does this have to do with operator precendence? (x * width + y) is NOT the same as (y * width + x), because multiplication takes precedence over addition, i.e the multiplication will happen before the addition. However, (y * width + x) and (x + y * width) are equivalent, for the same reason as above.

While I don''t understand why you''d need to add x and y first, or why they are floats - floating-point numbers make no sense as indices into a 2D array - the way you''d do it is put parenthesis around the addition of x and y, like (x + y). However, you already have that. If you want to truncate x and y first, cast them to integers first. I''m sorry, but I really don''t understand your problem.

Let me ask you a few questions though:
1) Why exactly do you need to add x and y first?
2) Why are x and y floating point if they are 2D array indices? Is this array supposed to be a "rough" lookup table for real-number coordinates?

Share this post


Link to post
Share on other sites
quote:
Original post by Zipster
Does this have to do with operator precendence? (x * width + y) is NOT the same as (y * width + x), because multiplication takes precedence over addition, i.e the multiplication will happen before the addition. However, (y * width + x) and (x + y * width) are equivalent, for the same reason as above.

While I don't understand why you'd need to add x and y first, or why they are floats - floating-point numbers make no sense as indices into a 2D array - the way you'd do it is put parenthesis around the addition of x and y, like (x + y). However, you already have that. If you want to truncate x and y first, cast them to integers first. I'm sorry, but I really don't understand your problem.

Let me ask you a few questions though:
1) Why exactly do you need to add x and y first?
2) Why are x and y floating point if they are 2D array indices? Is this array supposed to be a "rough" lookup table for real-number coordinates?



They need to be floats (or doubles) becose im doing rotation
now whit my current math i have 98% accurate rotation, but if i could first count the floats to gether it should be 100% accurate...

How ever, i allready have 100% accurate rotation, its just kinda slow, so this is my funky optimazation.

And please lets stay in the queston not the reason, i would in any case, like to know is it bossible.



[edited by - Craazer on December 27, 2002 9:54:26 AM]

Share this post


Link to post
Share on other sites
quote:
And please lets stay in the queston not the reason, i would in any case, like to know is it bossible.

I ask what the reason is to get a better understanding of the question. You ask if you can do (x + y), but is that really even a question? Yes, you can add two things together? And yes, you can truncate the result to an integer afterwards? Why wouldn't you be able to do this? I really don't see where your problem is, and as of yet I don't know why you are trying to do this. You said rotation, but what are x and y? Rotation angles? Coordinates? Where does the 2D array come in? I only ask how everything fits together because I want to help you out. I have a feeling there's more to your problem then adding two floats and truncating the result. Or maybe that was your problem? You see, I really don't know, because there's no context for me to work with.

I'm not trying to grill you here, but there's not enough information for me to work with.

[edited by - Zipster on December 27, 2002 6:09:21 PM]

Share this post


Link to post
Share on other sites
Let me guess what you''re trying to do, and anticipate the answer:

You have two floats x and y, which are coordinates in the unit plane (x and y are between 0 and 1) which you want to scale by the size of your square array, denoted by width. So you want:

array2d[ (int)(x*width) ][ (int)(y*width) ]

which in a 1-D array would be

array1d[ (int)(x*width) + (int)(y*width)*width ]

The problem with using floats in (x+y*width) is that the decimal part of the y component becomes a horizontal shift, which you don''t want.

The only other possible way to make the algorithm more "accurate" is to round to the nearest integer, instead of simple truncation.

Tom

Share this post


Link to post
Share on other sites
quote:
Original post by Zipster
[quote]And please lets stay in the queston not the reason, i would in any case, like to know is it bossible.

I ask what the reason is to get a better understanding of the question. You ask if you can do (x + y), but is that really even a question? Yes, you can add two things together? And yes, you can truncate the result to an integer afterwards? Why wouldn''t you be able to do this? I really don''t see where your problem is, and as of yet I don''t know why you are trying to do this. You said rotation, but what are x and y? Rotation angles? Coordinates? Where does the 2D array come in? I only ask how everything fits together because I want to help you out. I have a feeling there''s more to your problem then adding two floats and truncating the result. Or maybe that was your problem? You see, I really don''t know, because there''s no context for me to work with.

I''m not trying to grill you here, but there''s not enough information for me to work with.

[edited by - Zipster on December 27, 2002 6:09:21 PM]

Its ok. I try to descripe it better, my apolizes.

Share this post


Link to post
Share on other sites
quote:
Original post by ParadigmShift
Let me guess what you''re trying to do, and anticipate the answer:

You have two floats x and y, which are coordinates in the unit plane (x and y are between 0 and 1) which you want to scale by the size of your square array, denoted by width. So you want:

array2d[ (int)(x*width) ][ (int)(y*width) ]

which in a 1-D array would be

array1d[ (int)(x*width) + (int)(y*width)*width ]

The problem with using floats in (x+y*width) is that the decimal part of the y component becomes a horizontal shift, which you don''t want.

The only other possible way to make the algorithm more "accurate" is to round to the nearest integer, instead of simple truncation.

Tom


You are close but not quite, let me try to end this ''mystery''

Share this post


Link to post
Share on other sites
Here is some example code:

  

// this array holds the world cells

int World[SomeNumber];

// for and example here, bomb hits to a 1,56 | 5,76 in world coordinates


float hitx=1.56;
float hity=1.76;

//So what i to do is calculate were the bomb hitted.

World[int(hitx * map_width + hity)] = SomeThingUnUseful;

//Ok that forks some how but its not accurate enough!




So i hope i made my self clear now, i dont know any other way to say this...

Share this post


Link to post
Share on other sites
I see. What you would want to do is truncate the hit coordinates. You can then plug in those numbers into the array as integers.

int iHitX = (int)(hitx);
int iHitY = (int)(hity);


Then iHitx and iHitY become your array coordinates. That''s as accurate as you can get it, because that''s the resolution of your world array.

World[ (iHitY * MAP_WIDTH) + iHitX ] = Something;

...is the correct logic.

Hope that helps. If you want to increase the resolution of your world array, I could post some info on how to work with that.

Share this post


Link to post
Share on other sites
quote:
Original post by Zipster
I see. What you would want to do is truncate the hit coordinates. You can then plug in those numbers into the array as integers.

int iHitX = (int)(hitx);
int iHitY = (int)(hity);


Then iHitx and iHitY become your array coordinates. That''s as accurate as you can get it, because that''s the resolution of your world array.

World[ (iHitY * MAP_WIDTH) + iHitX ] = Something;

...is the correct logic.

Hope that helps. If you want to increase the resolution of your world array, I could post some info on how to work with that.


I know that solution my self too, and i need more accurate...
Thats why the floats should be added to gether before anything else (hitx+hity) would be most accurate...

Share this post


Link to post
Share on other sites
quote:
Original post by python_regious
Are you talking about casting to an int just truncating the floats?

If you want to round to the nearest int, just add 0.5 to the floats before casting...

Death of one is a tragedy, death of a million is just a statistic.



hmmmm...

x=3.7
y=2.4

x + y = 6.1

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

Thats wery unaccurate.

But did i calculate that right? im not actualy sure how does the int truncation happen.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
quote:
Original post by billybob
[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.



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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites