c++ - 如何用等距水平线填充闭合折线?

标签 c++ algorithm computational-geometry

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” 坐标的直线方程,它们不会连续匹配。

问题:

  1. 您能否提供一些关于如何继续创建用水平线填充闭合折线的算法的见解?

最佳答案

这只是扫描线算法的一个特例(专为填充多边形而设计):http://www.tutorialspoint.com/computer_graphics/polygon_filling_algorithm.htm

使用所需的步长(间距)将 y 从 yMin(多边形顶部)迭代到 yMax。

对于每个 y,找到与多边形线段的交点,按它们的 x 坐标对它们进行排序,用一条线连接每对其他线段

关于c++ - 如何用等距水平线填充闭合折线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32839667/

相关文章:

c++ - 获取 lambda 参数类型

c++ - 基于 int 的短掩码在 C++ 库 Bullet 中如何工作?

c++ - 是否有可能知道哪个库使用 ldd 引入了另一个库?

c++ - Qt Signal/Slots发送完整结构

c - 如何向左旋转位并向右添加旋转位

c++ - CGAL:在浏览平面 map 时使用 `Mark`类型

algorithm - voronoi 细胞中的质心

algorithm - 如何计算可能的不同序列的数量?

php - 兔龟计算

Python 如何使用 osgeo.ogr.Geometry 对象计算多边形周长