how to convert co-ordinates into spiral# thing.?

Posted by noshenim on July 12, 2010, 12:36 p.m.

\~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/

Comments

Juju 14 years, 4 months ago

Interesting question. I'll work on it now.

sirxemic 14 years, 4 months ago

Interesting challenge. Might work on it later this day.

noshenim 14 years, 4 months ago

thanks ^.^

sirxemic 14 years, 4 months ago

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;

}

Juju 14 years, 4 months ago

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.

sirxemic 14 years, 4 months ago

Quote:
Mathematical abs() is the same as programming floor()
Wut.

Also, wut @ your maths. I didn't approach it that way =P

EDIT: I just realized your maths is useful for calculating the other way around.

Juju 14 years, 4 months ago

Whoops, I mean ipart not abs.

noshenim 14 years, 4 months ago

genius :o

I wonder how to make this work in 3 dimensions.

noshenim 14 years, 4 months ago

I forgot that I need to reverse this function… any help?

Quote:

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;

}

Avenger 14 years, 4 months ago

"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.