问题
If I get a line segment which was long enough to cross a given polygon, which could be concave or convex polygon. How did I find the all the intersected light segments which was contained in the polygon?
If the target region is not polygon, but a implicit curve function or spline curve, how to do it?
Thanks!
回答1:
There really isn't a simple solution to your problem, especially with curves (beziers and splines). On top of the complexities of polygon clipping, there's the considerable challenge of reconstructing the clipped curves (assuming you want the clipping result to remain as beziers and splines and not just 'flattened' line approximations).
I have recently released a beta update* to my polygon clipping library 'Clipper' that does do line-polygon and line-line clipping (where lines can be curves too). However, while the main library is written in Delphi, C++ & C#, the new beta code is so far only in Delphi which may not help you. Nevertheless if you look at the code you'll see why I state there's no 'simple' solution.
- Edit 15 Jul 2011: This 'update' never got beyond this beta release and is now simply 'proof-of-concept'. It is now based on an old version of my Clipper library and would need a major rewrite to be maintainable and extensible. (At some stage I may revisit it but I'm currently intent on further improving the core library.) Nevertheless, this 'proof-of-concept' Delphi code can be downloaded here
回答2:
- Step one: find all intersection points, in any order. For polygon, you need to find intersection of red line and line of each segment. Just solve system of two linear equations. If solution is constrained to polygon segment limits, you have an intersection.
- Step two: sort found points by position on red line. You know that first and last point are outer ones. "Outerness" flips with each point - outer-inner-outer and so on. Between two adjacent outer points you have inner line segment (green). Edit: not true... Start with point #0, segment #0 - #1 is inner, next is outer, next is inner again and so on.
If region is not polygon, but is given by some implicit function, you need to find where that function is equal to red line (approach depends on function, of course).
来源:https://stackoverflow.com/questions/3940694/line-clipping-to-arbitary-2d-polygon