扫描线算法求若干条线段的交点SegmentsIntersection

123 篇文章 1 订阅

ref:https://github.com/johnhany/SegmentsIntersection/

http://johnhany.net/2013/11/sweep-algorithm-for-segments-intersection/

license: contact johnhany@163.com


/*
 *	Author: John Hany
 *	Website: http://johnhany.net/
 *	Source code updates: https://github.com/johnhany/SegmentsIntersection
 *	If you have any advice, you could contact me at: johnhany@163.com
 *
 *	------------Sweep Algorithm For Segments Intersection-------------
 *
 *	The program updates two structures during the whole algorithm:
 *	-Link list "POrder{}" saves all the points and intersection points (ordered list)
 *	-Binary tree "tree{}" saves the event points being scanned at present (scan tree)
 *
 *	The ordered list is arranged by Y axis decreasingly.
 *	The program scans from start of the list "POrder" to the end.
 *	When detecting a new intersection point, it will be inserted into the ordered list.
 *	For the lines that are being scanned, their upper points, lower points
 *	parameters(k and b) and intersection points are saved in the "POrder" node.
 *
 *	The scan tree is arranged by X axis increasingly.
 *	For each node in scan tree, (scan->left->id->x) < (scan->id->x) < (scan->right->id->x).
 *	The "id" value in "tree" node is a pointer towards certain node in ordered list.
 *
 *	-------------------------------------------------------------------
 *
 *	When scanning the "POrder" list, the program does such three things:
 *
 *	(1).For an upper point, add a new "scan" node in the scan tree, pointing to the point.
 *	Get its left and right neighbors in the scan tree. 
 *	Then check intersection between it and its two neighbors.
 *	If there is any intersection point, add it into the ordered list,
 *	and they will be processed in the future.
 *
 *	(2).For a lower point, search for its upper point in the scan tree and
 *	get left and right neighbors of the upper point.
 *	Delete the upper point from the tree, then check intersection between the two neighbors.
 *	If there is any intersection point, add it into the ordered list to be processed in the future.
 *
 *	(3).For an intersection point, get the left neighbor of the left line and
 *	right neighbor of the right line.
 *	Switch the order of two lines of the same intersection point in the scan tree.
 *	Check intersection between its left line(the new right line) and its right neighbor
 *	Check intersection between its right line(the new left line) and its left neighbor.
 *	
 */

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值