Advertisement
MagicWinnie

Untitled

Nov 19th, 2019
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.47 KB | None | 0 0
  1. import math
  2.  
  3. coordinates = []
  4. coordinates_new = []
  5.  
  6. def less(a, b):
  7.     if (a[0] - center[0] >= 0 and b[0] - center[0] < 0):
  8.         return True
  9.     if (a[0] - center[0] < 0 and b[0] - center[0] >= 0):
  10.         return False
  11.     if (a[0] - center[0] == 0 and b[0] - center[0] == 0):
  12.         if (a[1] - center[1] >= 0 or b[1] - center[1] >= 0):
  13.             return a[1] > b[1]
  14.         return b[1] > a[1]
  15.  
  16.     det = (a[0] - center[0]) * (b[1] - center[1]) - (b[0] - center[0]) * (a[1] - center[1])
  17.     if (det < 0):
  18.         return True
  19.     if (det > 0):
  20.         return False
  21.    
  22.     d1 = (a[0] - center[0]) * (a[0] - center[0]) + (a[1] - center[1]) * (a[1] - center[1])
  23.     d2 = (b[0] - center[0]) * (b[0] - center[0]) + (b[1] - center[1]) * (b[1] - center[1])
  24.     return d1 > d2
  25.  
  26. def bubble_sort(nums):
  27.     swapped = True
  28.     while swapped:
  29.         swapped = False
  30.         for i in range(len(nums) - 1):
  31.             if less(nums[i], nums[i + 1]):
  32.                 nums[i], nums[i + 1] = nums[i + 1], nums[i]
  33.                 swapped = True
  34.  
  35. def PolygonArea(corners):
  36.     n = len(corners)
  37.     area = 0.0
  38.     for i in range(n):
  39.         j = (i + 1) % n
  40.         area += corners[i][0] * corners[j][1]
  41.         area -= corners[j][0] * corners[i][1]
  42.     area = abs(area) / 2.0
  43.     return area
  44.  
  45. def calc(xa, ya, xb, yb):
  46.     if ya==yb:
  47.         x=50
  48.         y=(x-xa)*(yb-ya)/(xb-xa)+ya
  49.         x1=-50
  50.         y1=(x1-xa)*(yb-ya)/(xb-xa)+ya
  51.     else:
  52.         y=50
  53.         x=(y-ya)*(xb-xa)/(yb-ya)+xa
  54.         y1=-50
  55.         x1=(y1-ya)*(xb-xa)/(yb-ya)+xa
  56.         if (x<-50 or x>50) and (x1<-50 or x1>50):
  57.             x=50
  58.             y=(x-xa)*(yb-ya)/(xb-xa)+ya
  59.             x1=-50
  60.             y1=(x1-xa)*(yb-ya)/(xb-xa)+ya
  61.         elif (x<-50 or x>50) and (x1>=-50 or x1<=50):
  62.             x=50
  63.             y=(x-xa)*(yb-ya)/(xb-xa)+ya
  64.             if y<-50 or y>50:
  65.                 x=-50
  66.                 y=(x-xa)*(yb-ya)/(xb-xa)+ya
  67.         elif (x1<-50 or x1>50) and (x>=-50 or x<=50):
  68.             x1=50
  69.             y1=(x1-xa)*(yb-ya)/(xb-xa)+ya
  70.             if y1<-50 or y1>50:
  71.                 x1=-50
  72.                 y1=(x1-xa)*(yb-ya)/(xb-xa)+ya
  73.     temp = ((x, y), (x1, y1))
  74.     coordinates.append(temp)
  75.  
  76.  
  77. def del_corners(x, y, x1, y1):
  78.     if (y==y1) or (x==x1):
  79.         if y==y1:
  80.             if y>0:
  81.                 xa=50
  82.                 ya=50
  83.                 xb=-50
  84.                 yb=50
  85.             else:
  86.                 xa=50
  87.                 ya=-50
  88.                 xb=-50
  89.                 yb=-50
  90.         else:
  91.             if x>0:
  92.                 xa=50
  93.                 ya=50
  94.                 xb=50
  95.                 yb=-50
  96.             else:
  97.                 xa=-50
  98.                 ya=50
  99.                 xb=-50
  100.                 yb=-50
  101.         return ((xa,ya),(xb,yb))
  102.     elif ((y==50 and y1!=-50) or (y==-50 and y1!=50)) or ((y1==50 and y!=-50) or (y1==-50 and y!=50)):
  103.         if x==50 or x==-50:
  104.             xa=x
  105.             ya=y1
  106.         else:
  107.             xa=x1
  108.             ya=y1
  109.         return ((xa,ya), (0,0))
  110.     else:
  111.         if (y==50 and y1==-50) or (y==-50 and y1==50):
  112.             ya=y
  113.             yb=y1
  114.             if abs(50-x)+abs(50-x1)<abs(-50-x)+abs(-50-x1):
  115.                 xa=50
  116.                 xb=50
  117.             else:
  118.                 xa=-50
  119.                 xb=-50
  120.         else:
  121.             xa=x
  122.             xb=x1
  123.             if abs(50-y)+abs(50-y1)<abs(-50-y)+abs(-50-y1):
  124.                 ya=50
  125.                 yb=50
  126.             else:
  127.                 ya=-50
  128.                 yb=-50
  129.         return ((xa,ya), (xb,yb))
  130.  
  131.  
  132.  
  133. routes = eval(input())
  134.  
  135. for i in range(len(routes)):
  136.     calc(routes[i][0][0], routes[i][0][1], routes[i][1][0], routes[i][1][1])
  137.  
  138. for i in range(len(coordinates)):
  139.     a = coordinates[i][0][1] - coordinates[i][1][1]
  140.     b = coordinates[i][1][0] - coordinates[i][0][0]
  141.     c = coordinates[i][0][0] * coordinates[i][1][1] - coordinates[i][1][0] * coordinates[i][0][1]
  142.  
  143.     for j in range(len(coordinates)):
  144.         if coordinates[i]==coordinates[j]:
  145.             continue
  146.         a1 = coordinates[j][0][1] - coordinates[j][1][1]
  147.         b1 = coordinates[j][1][0] - coordinates[j][0][0]
  148.         c1 = coordinates[j][0][0] * coordinates[j][1][1] - coordinates[j][1][0] * coordinates[j][0][1]
  149.  
  150.         y = (a*c1 - a1*c)/(a1*b - a*b1)
  151.         x = (-b1*y-c1) / a1
  152.         coord = (coordinates[j][1][0]-coordinates[j][0][0], coordinates[j][1][0]-coordinates[j][0][0])
  153.         if a*x + b*y + c == 0:
  154.             if x >= 0 and max(coordinates[i][0], coordinates[i][1])[0] > x:
  155.                 if (x,y) not in coordinates_new:
  156.                     coordinates_new.append((x,y))
  157.                 coordinates_new.append((min(coordinates[i][0], coordinates[i][1])))
  158.             elif x < 0 and min(coordinates[i][0], coordinates[i][1])[0] < x:
  159.                 if (x,y) not in coordinates_new:
  160.                     coordinates_new.append((x,y))
  161.                 coordinates_new.append((max(coordinates[i][0], coordinates[i][1])))
  162.             else:
  163.                 coordinates_new.append(coordinates[i][0])
  164.                 coordinates_new.append(coordinates[i][1])
  165.         else:
  166.             coordinates_new.append(coordinates[i][0])
  167.             coordinates_new.append(coordinates[i][1])
  168.  
  169. map = [(50, 50), (-50, -50), (-50, 50), (50, -50)]
  170. corn = []
  171. # coordinates_new.append((-50,50))
  172. # coordinates_new.append((-50,-50))
  173.  
  174. center = (sum(i for i, _ in coordinates_new)/len(coordinates_new), sum(j for _, j in coordinates_new)/len(coordinates_new))
  175.  
  176. bubble_sort(coordinates_new)
  177.  
  178. for i in range(len(coordinates_new)):
  179.     try:
  180.         del map[map.index(del_corners(coordinates_new[i][0], coordinates_new[i][1], coordinates_new[i+1][0], coordinates_new[i+1][0])[0])]
  181.         del map[map.index(del_corners(coordinates_new[i][0], coordinates_new[i][1], coordinates_new[i+1][0], coordinates_new[i+1][0])[1])]
  182.     except ValueError:
  183.         pass
  184.     except IndexError:
  185.         try:
  186.             del map[map.index(del_corners(coordinates_new[i][0], coordinates_new[i][1], coordinates_new[0][0], coordinates_new[0][0])[0])]
  187.             del map[map.index(del_corners(coordinates_new[i][0], coordinates_new[i][1], coordinates_new[0][0], coordinates_new[0][0])[1])]
  188.         except ValueError:
  189.             pass
  190. coordinates_new += map
  191.  
  192. center = (sum(i for i, _ in coordinates_new)/len(coordinates_new), sum(j for _, j in coordinates_new)/len(coordinates_new))
  193.  
  194.  
  195. bubble_sort(coordinates_new)
  196.  
  197. #print(coordinates_new)
  198.  
  199. print(PolygonArea(coordinates_new))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement