Advertisement
atulshanbhag

allpoly.py

Jul 13th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. from math import fabs
  2.  
  3. def ahead(x, y, z):
  4. x1 = (x[2] - y[2]) / (y[1] - x[1])
  5. x2 = (y[2] - z[2]) / (z[1] - y[1])
  6.  
  7. cmpe = int(x1 > x2)
  8.  
  9. return y[0] == cmpe
  10.  
  11. def areaL(x, y, z):
  12. x1 = (x[2] - y[2]) / (y[1] - x[1])
  13. x2 = (y[2] - z[2]) / (z[1] - y[1])
  14.  
  15. return (y[2] * (x1 - x2))
  16.  
  17. test = int(raw_input())
  18. for _ in xrange(test):
  19. n = int(raw_input())
  20. sq = (3 ** 0.5)
  21. inf = float('inf')
  22.  
  23. x = [0.0 for _ in xrange(n)]
  24. y = [0.0 for _ in xrange(n)]
  25.  
  26. for i in xrange(n):
  27. xt, yt = map(float, raw_input().split())
  28.  
  29. x[i] = (xt - sq*yt) / 2
  30. y[i] = (sq*xt + yt) / 2
  31.  
  32. v = []
  33. for i in xrange(n):
  34. m = (y[i] - y[i-1]) / (x[i] - x[i-1])
  35. c = y[i] - m*x[i]
  36.  
  37. v.append((int(x[i] > x[i-1]), m, c))
  38.  
  39. v.sort()
  40. idx = 0
  41. for i in xrange(n):
  42. maxC, minC = v[i][2], v[i][2]
  43.  
  44. while i < n-1 and v[i][0] == v[i+1][0] and fabs(v[i][1] - v[i+1][1]) < 1e-10*v[i+1][1]:
  45. i += 1
  46. maxC = max(maxC, v[i][2])
  47. minC = min(minC, v[i][2])
  48.  
  49. if v[i][0]:
  50. v[idx] = (v[i][0], v[i][1], minC)
  51. else:
  52. v[idx] = (v[i][0], v[i][1], maxC)
  53. idx += 1
  54.  
  55. v = v[:idx]
  56. v.append(v[0])
  57. pol = [0, ]
  58.  
  59. for i in xrange(2, idx+1):
  60. prev = i - 1
  61. while len(pol) >= 1 and not ahead(v[pol[-1]], v[prev], v[i]):
  62. prev = pol[-1]
  63. pol.pop()
  64. pol.append(prev)
  65.  
  66. if len(pol) <= 2:
  67. print 0.0
  68.  
  69. else:
  70. start, area = 0, 0.0
  71.  
  72. while ahead(v[pol[start]], v[pol[-1]], v[pol[start + 1]]):
  73. start += 1
  74.  
  75. for j in xrange(start, len(pol)-2):
  76. area += areaL(v[pol[j]], v[pol[j+1]], v[pol[j+2]])
  77. area += areaL(v[pol[-1]], v[pol[0]], v[pol[1]]);
  78. area += areaL(v[pol[-2]], v[pol[-1]], v[pol[0]]);
  79.  
  80. print '%.10f' % (area / 8e14, )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement