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
quote:
Original post by billybob
you mean like ((int)(y + x) * width)?


Yes.

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]
Advertisement
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
"E-mail is for geeks and pedophiles." -Cruel Intentions
quote:
Original post by Zipster
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.

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''

Here is some example code:

  // this array holds the world cellsint World[SomeNumber];// for and example here, bomb hits to a 1,56 | 5,76 in world coordinatesfloat 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...
Advertisement
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.
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...

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.
If at first you don't succeed, redefine success.
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.

This topic is closed to new replies.

Advertisement