-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathDead-mens-shot.js
More file actions
23 lines (17 loc) 路 831 Bytes
/
Copy pathDead-mens-shot.js
File metadata and controls
23 lines (17 loc) 路 831 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// https://www.codingame.com/training/easy/dead-mens-shot
const isPointInsidePolygon = ([x, y], polyArray) => {
let inside = false;
for (let i = 0, j = polyArray.length - 1; i < polyArray.length; j = i++) {
const xi = polyArray[i][0]; const yi = polyArray[i][1];
const xj = polyArray[j][0]; const yj = polyArray[j][1];
const intersect = ((yi > y) != (yj > y))
&& (x < (xj - xi) * (y - yi) / (yj - yi) + xi);
if (intersect) inside = !inside;
}
return inside;
};
const readlineToPoint = () => readline().split(' ').map(Number);
const polygon = [...Array(+readline())].map(readlineToPoint);
const shots = [...Array(+readline())].map(readlineToPoint);
shots.map((p) => isPointInsidePolygon(p, polygon) === true? 'hit' : 'miss')
.forEach((res) => console.log(res));