find the intersection of two convex polygon among set of m polygons in a plane having all total n vertices in O(nlogm) time
-
Take 'em two-at-a-time, and do this: http://stackoverflow.com/questions/753140/how-do-i-determine-if-two-convex-polygons-intersect. – Jonathan M Dec 15 '14 at 17:55
-
1And google this stuff before you ask. I found this by googling "how to tell if polygons intersect" – Jonathan M Dec 15 '14 at 17:55
-
1I could not understand how to make it O(nlogm) – user3621835 Dec 15 '14 at 17:58
-
No. The question says O(nlogm). m set of convex polygons(p1,p2..pm) and total n vertices. ni denote the number of vertices on Pi and n = n1+n2+..+nm – user3621835 Dec 15 '14 at 18:54
-
1At most 2m things will be in your binary search tree at any given time. You can take it from there. – tmyklebu Dec 15 '14 at 19:05
-
Please can you provide the answer.I am not understanding it – user3621835 Dec 15 '14 at 19:11
-
Why did you edit, removing all the details??? Now answer looks strange a bit. – MBo Dec 16 '14 at 07:40
1 Answers
Here is an O(n * log m)
solution:
Let's check if any two polygons really intersect(not when one is
contained within another). In this case, we can use standard algorithm
to check if there is pair of intersecting segments in an arbitrary set
of segments. There is a well-knownO(n * log n)
sweep line based solution
for this problem(I will later show how to make itO(n * log m)
for this
particular problem). The set of segments we are interested in is just a set
of all edges of given polygons. If an intersection is found, we are done.Otherwise, we have to check if one polygon is contained within another. We
will use sweep line again. The events should be sorted by their x-coordinate.
There are 2 types of events: an edge of a polygon has started and an edge has
ended. Let's iterate over all events and maintain a sorted set
(using a balanced binary search tree) (edges should be compared by their
y-coordinate, their relative order never changes because none of them intersect)
of all started but not ended edges(the first type event adds one edge to
the set, the second type removes one). I claim that one polygon is contained
within another if and only if at some point there is a following sequence in
the set: A B B A, where A are edges of one polygon and B are edges of
the other. To check if this situation ever happens, it is sufficient to look at adjacent to the newly inserted/newly deleted element of the set after each insertion/deletion.
So far, it looks likeO(n * log n)
.Now let's actually achieve
O(n * log m)
time complexity. Iterating over events in both 1. and 2. and maintaining the set is alreadyO(n * log m)
becuase there are at mostO(m)
elements in the set at a time(no more then two edges from each polygon). So the only remaining part is sorting all edges by x-coordinate. The idea here is pretty simple: for a convex polygon, it is possible to generate a sorted list of its edges inO(k)
time, wherek
is the number of vertices(we can traverse it from the left to right in 2 directions and obtain two sorted lists that can be merged in linear time). So now we havem
sorted lists with no more thann
elements in total. Merging them into one sorted list inO(n * log m)
time using a heap is a standard problem, too. That's it.

- 18,368
- 4
- 33
- 45
-
Thanks user2040251. but why there are no more than 2 edges from each polygon? – user3621835 Dec 15 '14 at 19:36
-
-
The polygon is convex. Thus, at most two edges can intersect a vertical line(actually, there can be 4 if a line goes through exactly two vertices, but I assume that the events are processed in order first delete-then add). If the events are processed in first add-then delete order, the number of edges in the set is at most 4, but it does not affect the complexity anyway. – kraskevich Dec 15 '14 at 19:50