How to fill a closed poly-line with equidistant horizontal lines?

橙三吉。 提交于 2019-12-13 06:02:40

问题


I need to write and algorithm that fills closed poly-line with horizontal equidistant lines.

I've done similar things with rectangles and circles, here is a code snippet for the latter:

// circle parameters: center(point(0).x, point(0).y), radius
int offsetX = point(0).x + radius;
int offsetY = point(0).y + radius;
for(int i = -radius; i < radius; i += spacing){
    int ry = i;
    int rx = sqrt(double(radius*radius - ry*ry));
    // the parameters are pair of coordinates of the horizontal line
    fl_line(offsetX - rx, offsetY + i, 
            offsetX + rx, offsetY + i);
}

In the case of closed poly-line the additional degree of difficulty (for me) is that the coordinates of the horizontal lines would not be extracted from a single equation (circle, height of rectangle, etc), but rather from the equations of the lines with the same "y" coordinates, which will not match continuously.

Question:

  1. Could you provide me with some insight on how to proceed with creating an algorithm that fills closed poly-lines with horizontal lines?

回答1:


This is just a special case of the scan line algorithm (designed for filling polygons): http://www.tutorialspoint.com/computer_graphics/polygon_filling_algorithm.htm

Iterate y from yMin (top of your polygon) to yMax with the desired step (spacing).

For each y, find intersections with the polygon line segments, order them by their x-coordinate, connect every other pair with a line




回答2:


Create a list of all edges with their lowest endpoint first. Sort the list by increasing ordinate (of the lowest endpoint).

Create an "active list" that will contain all edges that are intersected by the current horizontal.

Initialize the current horizontal position just below the lowest edge and make sure the active list is empty.

Move the horizontal upward in desired increments until the active list empties again.

Upon a move, discard from the active list the edges that will no more intersect it. Also add to it the edges that will start intersecting it (as the edges are sorted, you will search no more than needed).

Beware that an edge can be completely skipped (it can enter the active list and immediately leave it).

When the active list is up-to-date, compute all intersections and join them by an horizontal segment, left-to-right.


Note that it is possible to avoid horizontal sorting before joining the intersections in pairs, by carefully inserting the new edges where necessary. Given that the active list is usually very short, I prefer to systematically apply insertion sort.

Assuming that all active list operations take linear time in the size of the list, the total time is like O(Ne.Lg(Ne) + Ny.L), where Ne is the number of edges, Ny the number of horizontals and L the average number of intersections per horizontal (usually between 2 and 4). This is to be compared to O(Ne.Ny) for the naïve algorithm.



来源:https://stackoverflow.com/questions/32839667/how-to-fill-a-closed-poly-line-with-equidistant-horizontal-lines

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!