I need to write and algorithm that fills closed poly-line with horizontal equidistant lines.
我用矩形和圆做过类似的事情,这是后者的代码片段:
// 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);
}
在闭合折线的情况下,额外的难度(对我而言)是水平线的坐标不会从单个方程(圆,矩形的高度,等),而是来自具有相同 “y” 坐标的直线方程,它们不会连续匹配。
问题:
- 您能否提供一些关于如何继续创建用水平线填充闭合折线的算法的见解?
最佳答案
这只是扫描线算法的一个特例(专为填充多边形而设计):http://www.tutorialspoint.com/computer_graphics/polygon_filling_algorithm.htm
使用所需的步长(间距)将 y 从 yMin(多边形顶部)迭代到 yMax。
对于每个 y,找到与多边形线段的交点,按它们的 x 坐标对它们进行排序,用一条线连接每对其他线段
关于c++ - 如何用等距水平线填充闭合折线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32839667/