views:

52

answers:

2

Hi, guys, 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?

alt text

If the target region is not polygon, but a implicit curve function or spline curve, how to do it?

Thanks!

+2  A: 
  • 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).

alxx
Maybe I need to give a complex polygon.
Buzz
How to sort them. It's hard to find the in/out relationship I think. The intersection point list depends on the polygon edges.
Buzz
How to sort? Find point with lowest coordinates and sort all other ones by the distance to that point.
alxx
Thanks. Is there some reference related to this issue?
Buzz
This is known problem, there should be many. Try to search for "line polygon intersection".
alxx
+1  A: 

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.

Clipper demo image

Angus Johnson
Thanks. Which method did you use for curves clipping?
Buzz
The approach I took was initially to flatten the curves (and labeling each flattened segment) since the clipping algorithm works on lines only. Once intersections are found the labeled segments are used to identify the curve sub-segments (de Casteljau's algorithm). Then it's a matter of reapplying the de Casteljau's algorithm to the original curve, but only to the portions of the curve that contain intersections. Does that make sense?
Angus Johnson