-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathPython.py
52 lines (40 loc) · 1.4 KB
/
Python.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#****************************************#
#* *#
#* CodinGame.com Solutions by pathosDev *#
#* *#
#* Puzzle: Dead men's shot *#
#* Difficulty: Easy *#
#* Date solved: 19.11.2018 *#
#* *#
#****************************************#
def divisionEx(a, b):
if b != 0:
return a / b
else:
return float('Inf')
#Checks if a point lays inside a polygon or not.
def pointInPolygon(polygon, px, py):
collision = False
for current in range(len(polygon)):
vc = polygon[current]
vn = polygon[(current + 1) % len(polygon)]
btv = (vc[1] > py) != (vn[1] > py) #Check if between vc.y and vn.y.
jct = px < divisionEx((vn[0] - vc[0]) * (py - vc[1]), (vn[1] - vc[1])) + vc[0] #Jordan Curve Theorem.
if btv and jct:
#Swap collision state.
collision = not collision
return collision
#Read inputs.
N = int(input())
corners = []
for i in range(N):
cornerX, cornerY = map(int, input().split())
corners.append((cornerX, cornerY))
M = int(input())
for i in range(M):
shotX, shotY = map(int, input().split())
#Check if current shot lays in the polygon or not.
if pointInPolygon(corners, shotX, shotY):
print('hit')
else:
print('miss')