Advertisement
Guest User

Untitled

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