Guest User

SquareSpiral

a guest
Aug 12th, 2015
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.57 KB | None | 0 0
  1. # https://www.reddit.com/r/dailyprogrammer/comments/3ggli3/20150810_challenge_227_easy_square_spirals/
  2.  
  3. import math
  4.  
  5.  
  6. def spiral(*args):
  7.  
  8.     origin = [math.ceil(args[0]/2), math.ceil(args[0]/2)]
  9.     if len(args) == 2:  # when the input is a single value in the square spiral
  10.         x = math.sqrt(args[1])  # finding the value of the next lower odd number squared
  11.         y = math.floor(x)
  12.         if y % 2 == 1:
  13.             if x != y:
  14.                 pass
  15.             else:
  16.                 y -= 2  # when x is an odd number and when the input value is an odd number squared
  17.         else:
  18.             y -= 1  # when x is an even number
  19.  
  20.         # adding from the value of the next lower odd number sqaured, i.e. 25, 49, etc.
  21.         # and going around the square
  22.         if y**2 < args[1] and args[1] <= y**2 + y + 1:  # right side of square
  23.             origin[0] = origin[0] + math.ceil(y/2)
  24.             origin[1] = int(origin[1] + 0.5*(2*y**2 + y + 1) - args[1])
  25.  
  26.         elif y**2 + y + 1 < args[1] and args[1] <= y**2 + 2*y + 2:  # top
  27.             origin[0] = int(origin[0] + 0.5*(2*y**2+3*y+3) - args[1])
  28.             origin[1] = origin[1] - math.ceil(y/2)
  29.  
  30.         elif y**2 + 2*y + 2 < args[1] and args[1] <= y**2 + 3*y + 3:  # left
  31.             origin[0] = origin[0] - math.ceil(y/2)
  32.             origin[1] = int(origin[1] - 0.5*(2*y**2 + 5*y + 5) + args[1])
  33.  
  34.         elif y**2 + 3*y + 3 < args[1] and args[1] <= y**2 + 4*y + 4:  # bottom
  35.             origin[0] = int(origin[0] - 0.5*(2*y**2+7*y+7) + args[1])
  36.             origin[1] = origin[1] + math.ceil(y/2)
  37.  
  38.         print(origin)
  39.  
  40.     else:  # when the inputs are coordinates
  41.         x = args[1]-origin[0]  # finding coordinates relative to center
  42.         y = -args[2]+origin[1]
  43.         z = max([abs(x), abs(y)])
  44.         value = 2*z - 1
  45.         value1 = value ** 2  # value of the next lower odd number squared
  46.         value2 = value+2
  47.         value3 = (value2**2-value**2)/4  # increment
  48.         # print(x,y,z,value,value1,value2,value3)
  49.  
  50.         if abs(y) >= abs(x) and y > 0:  # top side of square
  51.             answer = value1 + 1.5 * value3 - x
  52.  
  53.         elif abs(y) >= abs(x) and y < 0:  # bottom
  54.             answer = value1 + 3.5 * value3 + x
  55.  
  56.         elif abs(y) < abs(x) and x > 0:  # right
  57.             answer = value1 + 0.5 * value3 + y
  58.  
  59.         elif abs(y) < abs(x) and x < 0:  # left
  60.             answer = value1 + 2.5 * value3 - y
  61.  
  62.         print(answer)
  63.  
  64. spiral(3, 8)
  65. spiral(11, 50)
  66. spiral(1024716039, 557614022)
  67. print()
  68. spiral(7, 1, 1)
  69. spiral(9, 6, 8)
  70. spiral(234653477, 11777272, 289722)
Add Comment
Please, Sign In to add comment