CAD Geometry: Point-in-Polygon Ray Casting Algorithm
Problem Statement
In computer-aided mechanical design (CAD/CAM), determine whether a 2D query point `(px, py)` lies strictly inside a non-self-intersecting closed polygon using the **Ray Casting Algorithm (Crossing Number Rule)**.
Cast a horizontal ray extending from the point toward $+\infty$. Count how many polygon edges intersect the ray:
- If the intersection count is odd, the point is inside (`true`).
- If even, the point is outside (`false`).
### Input Format
- First line contains two floats: `px py` (query point coordinates).
- Second line contains an integer `n` (number of polygon vertices).
- The next `n` lines each contain two floats: `x y` (vertex coordinates in counter-clockwise order).
### Output Format
- Print `true` or `false`.
### Constraints
- `3 <= n <= 1000`
- `-1000.0 <= x, y, px, py <= 1000.0`
Sample Cases
Example 1Sample Case
Input: 2.0 2.0
4
0 0
4 0
4 4
0 4
Output: true
Example 2Sample Case
Input: 5.0 5.0
4
0 0
4 0
4 4
0 4
Output: false
Example 3Sample Case
Input: 0 4
4 4
4 0
0 0
4
2.0 2.0
Output: false
Example 4Sample Case
Input: 0 4
4 4
4 0
0 0
4
5.0 5.0
Output: true
Constraints
Runtime Limit: 2000 ms per test batch
Memory Limit: 256 MB
Standard I/O stream reading
Official submissions are evaluated against hidden test cases on the server to verify time & memory complexity.