# Given a grid, starting at the center and spiraling out, and an Position what is the ID?

Related:

Challenge:

Given a grid, with an ID starting at the center and spiraling out, what is the ID given a position in the fewest number of bytes?

Grid:

+---------------+---------------+---------------+---------------+---------------+
| id:  20       | id:  19       | id:  18       | id:  17       | id:  16       |
| pos: (-2, -2) | pos: (-1, -2) | pos: (0, -2)  | pos: (1, -2)  | pos: (2, -2)  |
+---------------+---------------+---------------+---------------+---------------+
| id:  21       | id:  6        | id:  5        | id:  4        | id:  15       |
| pos: (-2, -1) | pos: (-1, -1) | pos: (0, -1)  | pos: (1, -1)  | pos: (2, -1)  |
+---------------+---------------+---------------+---------------+---------------+
| id:  22       | id:  7        | id:  0        | id:  3        | id:  14       |
| pos: (-2, 0)  | pos: (-1, 0)  | pos: (0, 0)   | pos: (1, 0)   | pos: (2, 0)   |
+---------------+---------------+---------------+---------------+---------------+
| id:  23       | id:  8        | id:  1        | id:  2        | id:  13       |
| pos: (-2, 1)  | pos: (-1, 1)  | pos: (0, 1)   | pos: (1, 1)   | pos: (2, 1)   |
+---------------+---------------+---------------+---------------+---------------+
| id:  24       | id:  9        | id:  10       | id:  11       | id:  12       |
| pos: (-2, 2)  | pos: (-1, 2)  | pos: (0, 2)   | pos: (1, 2)   | pos: (2, 2)   |
+---------------+---------------+---------------+---------------+---------------+


Tests:

f(0, 0) = 0
f(1, 1) = 2
f(0, -1) = 5
f(2, 0) = 14
f(-2, -2) = 20

f(x, y) = id


### Notes:

• Grid will always be square (height == width)
• Grid can be infinite in size
• How can the grid be both square and infinite in size? – Jonathan Frech Mar 27 '18 at 23:00
• Sorry, just trying to say it can be bigger than the example grid provided. – Justin808 Mar 27 '18 at 23:02
• Sorta odd you chose to make the bottom right 1,1 and the top left -1,-1. Then again I guess Java does this in java.awt.* – Magic Octopus Urn Mar 27 '18 at 23:04
• @MagicOctopusUrn - makes you think a little more? It's just the grid setup I've been using in my codebase. I didn't want to use the same old grid in my game. – Justin808 Mar 27 '18 at 23:06
• The last case should output 20, not 24 – Luis Mendo Mar 27 '18 at 23:46

# JavaScript (ES6), 64 bytes

Takes input in currying syntax (x)(y).

x=>y=>(d=(A=Math.abs)(A(x)-A(y))+A(x)+A(y))*d+(d-x+y)*(-y>x||-1)


Try it online!

Heavily inspired by this answer from math.stackexchange.com.

# JavaScript (Node.js), 58 bytes

y=>x=>(m=Math.max)(4*x*x-m(x+y,3*x-y),4*y*y+m(-x-y,x-3*y))


Try it online!

Use the formula from my previous answer, with some edit (because Javascript doesn't vectorize).

I came up with the formula myself. Basically, knowing that the formula must be quadratic in each 1/4 grid section, I find out the formula mostly by trial-and-error and then find a way to fit those together.

# MATL, 15 bytes

|sQtE1YLqwG+X{)


# J, 37 bytes

(g@[-+<.-+[+[)>.(g=:4**:)@]-+>.]+]+-~


Try it online!

3. -+> and :).