Advertisement
konchin_shih

0819 21:20

Aug 19th, 2021
1,271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.17 KB | None | 0 0
  1. import math
  2.  
  3. table = []
  4.  
  5. d = [(1, 0), (0, 1), (-1, 0), (0, -1)]
  6.  
  7.  
  8. def check(x, y, xLimit, yLimit):
  9.     return x < 0 or y < 0 or x >= xLimit or y >= yLimit
  10.  
  11.  
  12. def f(radius, x, y, xLimit, yLimit):
  13.     minm = 0
  14.     minp = (x, y)
  15.     for dx, dy in d:
  16.         if check(x + radius * dx, y + radius * dy, xLimit, yLimit):
  17.             continue
  18.         Srr, Szz, Srz = 0, 0, 0
  19.         rSum, zSum = 0, 0
  20.         curminv = 1e9
  21.         curminp = (x, y)
  22.         for r in range(radius):
  23.             rSum += r
  24.             zSum += table[x + r * dx][y + r * dy]
  25.             if table[x + r * dx][y + r * dy] < curminv:
  26.                 curminv = table[x + r * dx][y + r * dy]
  27.                 curminp = (x + r * dx, y + r * dy)
  28.         rAvg = rSum / radius
  29.         zAvg = zSum / radius
  30.         for r in range(radius):
  31.             Srr += (r - rAvg)**2
  32.             Szz += (table[x + r * dx][y + r * dy] - zAvg)**2
  33.             Srz += (r - rAvg) * (table[x + r * dx][y + r * dy] - zAvg)
  34.         if not Szz:
  35.             continue
  36.         curm = Srz / (math.sqrt(Srr) * math.sqrt(Szz))
  37.         if curm < minm:
  38.             minm = curm
  39.             minp = curminp
  40.  
  41.     return minp[0], minp[1]
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement