Visibility of polygons from an edge
Given is a 2D are with the polygons. I need to find out the polygons visible in a perpendicular line of sight from the a given line segme开发者_StackOverflow中文版nt lying within that area. e.g.
Further,
What can be the optimizations when the polygons have only vertical and horizontal edges.
I'd suggest the following ...
- Rotate the problem so your 'line of sight' segment is aligned to the x axis.
- Find the (axis aligned) bounding rectangle (BR) of each polygon.
- Sort the polygons using the Y coordinate of the bottom edge of each BR
- Create a clipping 'range buffer' to mark the portions of the viewing segment that will be no longer visible.
- For each polygon C (current) in the sorted list do ...
- Use C's left and right bounds as its initial clipping range.
- Trim C's clipping range with the range already marked as clipped in the 'range buffer'.
- Now for each subsequent polygon S of a similar depth (ie where S's BR bottom edge starts below C's BR top edge) ...
- loop to next S if it doesn't overlap horizontally with C
- determine if S is overlapping from the left or right (eg by comparing the BR horizontal midpoints of S and C). If S overlaps from the right and S's left-most vertex is below C's right-most vertex, then truncate C's clipping range accordingly. (Likewise if S overlaps from the left.)
- If the residual clipping range isn't empty, then at least part of C is visible from your viewing segment. Now add C's residual clipping range to the clipping 'range buffer'.
精彩评论