Real Chebyshev Rotation This is a challenge inspired by Chebyshev Rotation. I suggest looking at answers there to get inspiration for this challenge.

Given a point on the plane there is a unique square (a rectangle with equal sides) that is centered on the origin and intersects that point (interactive demo):



Given a point p and a distance d, return the point obtained by moving distance d from p, counter-clockwise (and clockwise for negative  d), along the perimeter of the square centered on the origin that intersects p. Your answer must be accurate to at least 4 decimal digits.

Testcases:

(0, 0), 100 -&gt; (0, 0)
(1, 1), 81.42 -&gt; (-0.4200, 1.0000)
(42.234, 234.12), 2303.34 -&gt; (-234.1200, 80.0940)
(-23, -39.234), -234.3 -&gt; (39.2340, -21.8960)


The following test cases are from the original challenge by Martin Ender, and all are with d = 1:

(0, 0)       -&gt; (0, 0)
(1, 0)       -&gt; (1, 1)
(1, 1)       -&gt; (0, 1)
(0, 1)       -&gt; (-1, 1)
(-1, 1)      -&gt; (-1, 0)
(-1, 0)      -&gt; (-1, -1)
(-1, -1)     -&gt; (0, -1)
(0, -1)      -&gt; (1, -1)
(1, -1)      -&gt; (1, 0)
(95, -12)    -&gt; (95, -11)
(127, 127)   -&gt; (126, 127)
(-2, 101)    -&gt; (-3, 101)
(-65, 65)    -&gt; (-65, 64)
(-127, 42)   -&gt; (-127, 41)
(-9, -9)     -&gt; (-8, -9)
(126, -127)  -&gt; (127, -127)
(105, -105)  -&gt; (105, -104)
Python 2, 363 335 296 266 262 258 256 233 Bytes

Woo, 130 bytes lost! Thanks to Neil for saving 4 bytes, Nathan Merrill for saving 2 bytes, and xnor for saving a ridiculous 23 bytes!



The general idea is this: We can reduce the distance traveled by taking it's the modulus of it against the perimeter of the square. The perimeter is defined as 8 times the largest of the two coordinates, since the point must rest on it. Then, after modulus is taken we are guaranteed to have no overlap. It also guarantees we only ever have to move counter-clockwise, since modulus gives a positive result.

From there, I simply use what we know from the given x and y-coordinates to figure out where we are: top, bottom, left, right, or in a corner, and determine the direction, which can be one of 0, 1, 2, 3:

0 --&gt; we are on the 'top', moving 'left'
1 --&gt; we are on the 'left', moving 'down'
2 --&gt; we are on the 'bottom', moving 'right'
3 --&gt; we are on the 'right', moving 'up'


After this, it is as simple as looping while we still have distance to travel, and based on the direction we subtract or add to the appropriate coordinate, and tell the loop which direction we are going next.

p,d=input()
x,y=p
s=max(x,y,-x,-y)
d=d%(s*8or 1)
r=[(y&lt;s)*[2,[3,x&gt;-s][x&lt;s]][y&gt;-s],[2*(y&lt;0),3*(y&lt;=0)][x&gt;0]][y*y==x*x]
while s&gt;0&lt;d:f=1-2*(r&lt;2);m=abs(f*s-p[r%2]);j=d&gt;m;p[r%2]=[p[r%2]+f*d,f*s][j];r=-~r%4;d=(d-m)*j
print"%.4f "*2%tuple(p)


While quite long, it certainly works. Here's some example I/O:

[0, 0], 100 --&gt; 0.0000 0.0000
[1, 1], 81.42 --&gt; -0.4200 1.0000
[42.234, 234.12], 2303.34 --&gt; -234.1200 80.0940
[-23, -39.234], -234.3 --&gt; 39.2340 -21.8960


Try it online or run test cases.