pseudocode - Simple algorithm (pseudo-code) for line segment intersection -


i trying solve question, stuck on how make work. post question, , explain in it.

given set of horizontal line segments , vertical lines of total size n, want compute number of horizontal segments intersected each vertical line. algorithm should of complexity o(n*logn), , should achieved sorting, following linear scan. horizontal segment specified 2 x-coordinates , y-coordinate, while vertical line specified single x-coordinate. output array of numbers count[l], 1 each vertical line l.

for sorting, think sort entire set line finishes earliest (i.e. smallest second x-coordinate, or in case of vertical line, 1 x-coordinate) have linear progression through of lines. confused how linear scan following sorting should played out. if has hints, tips, or guidelines, appreciate it!

ps: practise midterm, while it's not homework, still mark such.

the question can written otherwise:

foreach horizontal segment (x1,x2), find vertical lines intersect it. can sorting vertical lines getting set of position x.

now, run binary search , position x1 in set of x's, let's call position p1. same x2, p2. number of intersection given segment equals p2-p1.

do horizontal segements.

sorting vertical segments take o(klog(k)). every search done in o(log(k)) , done twice each segment: o(mlog(k)). k number of vertical lines , m number of horizontal segments. n = m+k > m,k therefor, overall complexity o(nlogn).


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -