我需要编写和算法填充的关闭多行的有水平等距线。
我已经做了类似的事情的矩形和圆的,这里是一个code段为后者的:
//圆的参数:中心(点(0).X,点(0)·Y),半径
INT offsetX =点(0).X +半径;
INT offsetY =点(0).Y +半径;
对于(INT I = -radius; I<半径; I + =间距){
INT RY =我;
INT RX =开方(双(半径*半径 - RY * RY));
//参数是一对水平行的坐标的
fl_line(offsetX - RX,offsetY + I,
offsetX + RX,offsetY + I);
}
在的的封闭折线的情况下的难度的附加度数(我)是水平行的坐标将不被从一个单一等式(圆萃取,矩形的高度,等),而是从线与方程相同的y的的坐标,这将不连续匹配
这是扫描线算法的一个特例(设计用于填充面):的 http://www.tutorialspoint.com/computer_graphics/polygon_filling_algorithm.htm
这是YMIN(您多边形的顶部),以YMAX迭代y随所需的步骤(间隔)。
对于每个Y,找到交叉点与多边形线段,责令他们的x坐标,用线连接每隔一
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.
解决方案
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