Advertisement
Guest User

Untitled

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