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

Started by
24 comments, last by Craazer 21 years, 3 months ago
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.
Advertisement
y * width + x
quote:Original post by billybob
y * width + x



I meant that i have to: (y+x) then (y+x)*width



a=(x+y); b=a*width;?
Keys to success: Ability, ambition and opportunity.
accessing [i,j] object in 2D array stored as 1D one is, as far as I remember

a = array[i*width + j];
--------Dave[ Math Studio ] A Computer Algebra System
It should be the sum of the horizontal offset and the product of the width and the vertical offset.

I don''t really understand the original poster''s question and/or problem, so...
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?
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?
you mean like ((int)(y + x) * width)?
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]

This topic is closed to new replies.

Advertisement