1

I have been given a strictly convex polygon of S sides and Q queries to process.

All points of polygon and query points are given in (x,y) pairs.The points of the polygon are given in anti-clockwise order.

The aforementioned variables are limited such that 1<=S<=10^6 and 1<=Q<=10^5 and 1<=|x|,|y|<=10^9.

For each query I should output Yes if the given point lies inside the polygon; otherwise, No.

I tried using an O(S) inclusion test (ray-casting) and it timed out for the bigger test cases but also didn't pass all the preliminary ones.

Obviously, the implementation didn't cover all the edge cases and I got to know about a specific algorithm for this question which could answer each query in O(log S) using binary search but I can't figure out how to implement it from the pseudocode (first time doing computational geometry).

Could anyone provide me with the algorithm which covers all edge cases within the required time complexity (Q log S) or guide me to a page or paper that implements it?

jah
  • 31
  • 1
  • 1
  • 6
  • 1
    Putting adequate whitespace in your question is an important first step to writing a good question. Appropriate capitalization of the word "I" is also helpful. I've edited your question to make these changes. – Richard Feb 08 '18 at 09:14
  • Possible duplicate of [Show that, given a query point q, it can be tested in time O(log n) whether q lies inside P](https://stackoverflow.com/questions/36135978/show-that-given-a-query-point-q-it-can-be-tested-in-time-olog-n-whether-q-li) – meowgoesthedog Feb 08 '18 at 13:36
  • Thanks @Richard.Will keep these in mind for the future. – jah Feb 08 '18 at 18:52

3 Answers3

1

You can do a scan line algorithm. You need to sort the Q points by their x coordinate. Then find the S point with the lowest x and consider a line moving along the x axis. You need to track the two sides of the polygon.

Then move along the polygon and the Q set in ascending x coordinate. For every point you now just have to check if it's between the two lines you are tracking.

Complexity is O(Q logQ + S) if Q is not sorted and O(Q+S) if Q is already sorted.

Sorin
  • 11,863
  • 22
  • 26
  • Excellent approach. Anyway O(Q + S) does not always beat O(Q Log S). –  Feb 08 '18 at 15:50
  • I have never come across this algorithm during my research for the question.I will surely look into it now. – jah Feb 08 '18 at 18:56
  • @YvesDaoust you are right, but that only happens when Q is small. When Q ~= S, O(Q+S) is equivalent to O(Q) while O(Q logS) is equivalent to O(Q logQ). Obviously implementation details matter a lot and I suspect for S ~= 10^6 they matter more than the log added. On the other hand the O(Q+S) approach has mostly linear memory access so I think the compiler+processor can optimize it better. – Sorin Feb 09 '18 at 10:08
  • @Sorin: when it comes to asymptotic complexities, discussions about actual running times are futile as there is a hidden constant and the assumption is that the size parameters are unbounded. So O(Q+S) != O(Q Log S) [You can fix by switching to another algorithm when the respective values of Q and S demand it.] This said, the asymptotic complexity approach is far from being sufficient. –  Feb 09 '18 at 10:38
1

First, you can split your convex polygon into left and right parts both starting with the upper point and ending with the lower point. The points in both parts are already sorted by y-coordinate.

Assume that query point has coordinates (qx, qy). Now you can try to find (using a binary search) a segment from the left part and a segment from the right part that intersect with the line y = qy. If you could find both segments and qx is lying between x-coordinates of the segments' intersections with the line y = qy, it's inside the polygon.

enter image description here

The complexity of the query is O(log(S)).

DAle
  • 8,990
  • 2
  • 26
  • 45
1

There is no need to sort, a convex polygon is already sorted !

For a convex polygon, point location is quick and easy: split the polygon in two using a straight line between vertex 0 and vertex S/2. The signed area test will tell you on which side the test point lies and which half to keep (the half is also a convex polygon).

Continue recursively until S=3 and compare against the supporting line of the third side.

O(Log(S)) tests in total per query.

enter image description here

(The numbers show the order of the splits.)

  • Polygon is already sorted? In the question it was given points are in anti-clockwise order (I forgot to mention ) . Do both imply the same thing? – jah Feb 08 '18 at 19:00
  • @jaha: I am referring to a much deeper concept. When given as a *simple* polygon, a set of points has more structure than points in bulk (or forming a crossed polygon). An example is given by the convex hull problem: for independent points it requires an explicit sort, which justifies complexity O(N Log N), while for a simple polygon you can do without sorting, in O(N) operations. The case of convex polygons (which are obviously simple) is even easier: the vertices are ordered by angle around the center and also form two monotone vertical or horizontal chains (and in arbitrary directions). –  Feb 08 '18 at 20:40