Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- coordinates = []
- coordinates_new = []
- def less(a, b):
- if (a[0] - center[0] >= 0 and b[0] - center[0] < 0):
- return True
- if (a[0] - center[0] < 0 and b[0] - center[0] >= 0):
- return False
- if (a[0] - center[0] == 0 and b[0] - center[0] == 0):
- if (a[1] - center[1] >= 0 or b[1] - center[1] >= 0):
- return a[1] > b[1]
- return b[1] > a[1]
- det = (a[0] - center[0]) * (b[1] - center[1]) - (b[0] - center[0]) * (a[1] - center[1])
- if (det < 0):
- return True
- if (det > 0):
- return False
- d1 = (a[0] - center[0]) * (a[0] - center[0]) + (a[1] - center[1]) * (a[1] - center[1])
- d2 = (b[0] - center[0]) * (b[0] - center[0]) + (b[1] - center[1]) * (b[1] - center[1])
- return d1 > d2
- def bubble_sort(nums):
- swapped = True
- while swapped:
- swapped = False
- for i in range(len(nums) - 1):
- if less(nums[i], nums[i + 1]):
- nums[i], nums[i + 1] = nums[i + 1], nums[i]
- swapped = True
- def PolygonArea(corners):
- n = len(corners)
- area = 0.0
- for i in range(n):
- j = (i + 1) % n
- area += corners[i][0] * corners[j][1]
- area -= corners[j][0] * corners[i][1]
- area = abs(area) / 2.0
- return area
- def calc(xa, ya, xb, yb):
- if ya==yb:
- x=50
- y=(x-xa)*(yb-ya)/(xb-xa)+ya
- x1=-50
- y1=(x1-xa)*(yb-ya)/(xb-xa)+ya
- else:
- y=50
- x=(y-ya)*(xb-xa)/(yb-ya)+xa
- y1=-50
- x1=(y1-ya)*(xb-xa)/(yb-ya)+xa
- if (x<-50 or x>50) and (x1<-50 or x1>50):
- x=50
- y=(x-xa)*(yb-ya)/(xb-xa)+ya
- x1=-50
- y1=(x1-xa)*(yb-ya)/(xb-xa)+ya
- elif (x<-50 or x>50) and (x1>=-50 or x1<=50):
- x=50
- y=(x-xa)*(yb-ya)/(xb-xa)+ya
- if y<-50 or y>50:
- x=-50
- y=(x-xa)*(yb-ya)/(xb-xa)+ya
- elif (x1<-50 or x1>50) and (x>=-50 or x<=50):
- x1=50
- y1=(x1-xa)*(yb-ya)/(xb-xa)+ya
- if y1<-50 or y1>50:
- x1=-50
- y1=(x1-xa)*(yb-ya)/(xb-xa)+ya
- temp = ((x, y), (x1, y1))
- coordinates.append(temp)
- routes = eval(input())
- for i in range(len(routes)):
- calc(routes[i][0][0], routes[i][0][1], routes[i][1][0], routes[i][1][1])
- map = [0,0,0,0]
- #coordinates_new = [(50, 50), (-50, -50), (-50, 50), (50, -50)]
- #print(coordinates)
- for i in range(len(coordinates)):
- if (coordinates[i][0][0] >= 50 and coordinates[i][0][1] >= 50 ) or (coordinates[i][1][0] >= 50 and coordinates[i][1][1] >= 50):
- map[0] = 1
- print(coordinates[i], 1)
- if (coordinates[i][0][0] >= 50 and coordinates[i][0][1] <= -50) or (coordinates[i][1][0] >= 50 and coordinates[i][1][1] <= -50):
- map[3] = 1
- print(coordinates[i], 3)
- if (coordinates[i][0][0] <= -50 and coordinates[i][0][1] >= 50) or (coordinates[i][1][0] <= -50 and coordinates[i][1][1] >= 50):
- map[2] = 1
- if (coordinates[i][0][0] <= -50 and coordinates[i][0][1] <= -50) or (coordinates[i][1][0] <= -50 and coordinates[i][1][1] <= -50):
- map[1] = 1
- if map[0] == 1:
- coordinates_new.append((50,50))
- if map[1] == 1:
- coordinates_new.append((-50,-50))
- if map[2] == 1:
- coordinates_new.append((-50,50))
- if map[3] == 1:
- coordinates_new.append((50,-50))
- for i in range(len(coordinates)):
- a = coordinates[i][0][1] - coordinates[i][1][1]
- b = coordinates[i][1][0] - coordinates[i][0][0]
- c = coordinates[i][0][0] * coordinates[i][1][1] - coordinates[i][1][0] * coordinates[i][0][1]
- for j in range(len(coordinates)):
- if coordinates[i]==coordinates[j]:
- continue
- a1 = coordinates[j][0][1] - coordinates[j][1][1]
- b1 = coordinates[j][1][0] - coordinates[j][0][0]
- c1 = coordinates[j][0][0] * coordinates[j][1][1] - coordinates[j][1][0] * coordinates[j][0][1]
- y = (a*c1 - a1*c)/(a1*b - a*b1)
- x = (-b1*y-c1) / a1
- coord = (coordinates[j][1][0]-coordinates[j][0][0], coordinates[j][1][0]-coordinates[j][0][0])
- if a*x + b*y + c == 0:
- if x >= 0 and max(coordinates[i][0], coordinates[i][1])[0] > x:
- if (x,y) not in coordinates_new:
- coordinates_new.append((x,y))
- coordinates_new.append((min(coordinates[i][0], coordinates[i][1])))
- elif x < 0 and min(coordinates[i][0], coordinates[i][1])[0] < x:
- if (x,y) not in coordinates_new:
- coordinates_new.append((x,y))
- coordinates_new.append((max(coordinates[i][0], coordinates[i][1])))
- else:
- coordinates_new.append(coordinates[i][0])
- coordinates_new.append(coordinates[i][1])
- else:
- coordinates_new.append(coordinates[i][0])
- coordinates_new.append(coordinates[i][1])
- # coordinates_new.append((-50,50))
- # coordinates_new.append((-50,-50))
- center = (sum(i for i, _ in coordinates_new)/len(coordinates_new), sum(j for _, j in coordinates_new)/len(coordinates_new))
- bubble_sort(coordinates_new)
- print(PolygonArea(coordinates_new))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement