Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: algorithm of rectangular spiral #40

Closed
rogerz opened this issue Jun 8, 2014 · 1 comment
Closed

Question: algorithm of rectangular spiral #40

rogerz opened this issue Jun 8, 2014 · 1 comment

Comments

@rogerz
Copy link

rogerz commented Jun 8, 2014

@jasondavies , I'm a bit curious on how the rectangular spiral is generated from the formula (sqrt(1+4*t) - 1) & 3.

https://github.com/jasondavies/d3-cloud/blob/master/d3.layout.cloud.js#L347

Is there any reference link for further reading?

@jasondavies
Copy link
Owner

Technically it’s a square spiral, but with the step sizes scaled to match the rectangular extent.

For a given value of our integer parameter t, we’d like to find the direction of the corresponding leg.

First, examine the length of each “leg” of the spiral (every time it changes direction): the sequence is 1, 1, 3, 3, 6, 6, 10, 10, …

Very similar to triangular numbers, except that the length of a leg changes every 2nd leg, so the leg length Li = Tfloor(i/2).

If we can determine the step i for our parameter t, then we can determine the direction.

The curious formula is similar to finding the triangular root, but modified to take into account that the leg length changes every second step. We perform a bitwise operation on the root x, which gives us the two least significant bits of floor(x) and hence one of four directions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants