\~There's a way to convert an integral co-ordinate field into a spiral index number..
I want to convert 10,000 different co-ordinates with a range of over 1000 ( from (0,0) to more than (1000,1000) ) into single integers (just 'cause :P).The number and maximum range of the co-ordinates is variable.Does anyone know what this is called or how to do this efficiently?So (2,1) would become 9, (1,1)–>8, (2,2)–>24, etc. reminder: http://fuldans.se/how to convert co-ordinates into spiral# thing.?
Posted by noshenim on July 12, 2010, 12:36 p.m.
Interesting question. I'll work on it now.
Interesting challenge. Might work on it later this day.
thanks ^.^
Solution:
if (abs(x)>abs(y)){if (x < 0) return 4*x*x-x+y; else return 4*x*x-3*x-y;}else{if (y < 0) return 4*y*y+y-x; else return 4*y*y+3*y+x;}0 lies at co-ordinate (0,0)
1 lies at co-ordinate (1,0)2 lies at co-ordinate (1,1)The numbers 1 to 8 inclusive lie on ring 1.The numbers 9 to 24 inclusive lie on ring 2.The numbers along the x-axis (that is: 1, 10, 27, 52, 85 et cetera) are defined by 4n^2 - 3n where n is the ring number.The formula for working out which ring n a number x lies on is as follows: n = ipart( ( 3 + sqrt(9 + 16x) ) / 8 )Mathematical ipart() is the same as programming floor()That's the maths behind it anyway. sirXemic has got the implementation.Whoops, I mean ipart not abs.
genius :o
I wonder how to make this work in 3 dimensions.I forgot that I need to reverse this function… any help?
"return 4*x*x-x+y; else return 4*x*x-3*x-y;"
Just wondering, does the x-3 need to be in parentheses or not? Otherwise, you could have -12*x*x*x-y.