我有多边形填充算法的实现:
//point in polygon algorithm for filling polyons
void fill_polygon(Array *vx,Array *vy){
int nodes, pixelX, pixelY, i, j, swap,polyCorners ;
double top,bot,right,left,nodeX[1000];
polyCorners = vx->used;
if(polyCorners<=2){
return;
}
/*printf("Soltado VX: Used(%d) \n",vx->used);
for(i=0;i<vx->used;i++){
printf("|%d|", vx->array[i]);
}
printf("\n VY: Used(%d) \n",vy->used);
for(i=0;i<vy->used;i++){
printf("|%d|", vy->array[i]);
}
printf("\n");
wait(35); */
bot = maximum(vy);
top = minimum(vy);
right = maximum(vx);
left = minimum(vx);
/*printf("Top %d - Bot %d - Left %d - Right %d \n",top,bot,left,right);
wait(40);*/
for (pixelY=top; pixelY<bot; pixelY++) {
// Construyendo lista de Nodos de la figura
nodes=0; j=polyCorners-1;
for (i=0; i<polyCorners; i++) {
if (vy->array[i]< (double)pixelY && vy->array[j]>=(double)pixelY
|| vy->array[j]<(double) pixelY && vy->array[i]>=(double)pixelY) {
nodeX[nodes++]=(vx->array[i]+(pixelY-vy->array[i])/(vy->array[j]-vy->array[i])*(vx->array[j]-vx->array[i]));
}
j=i;
}
// Ordenamos nodos con bubble sort
i=0;
while (i<nodes-1) {
if (nodeX[i]>nodeX[i+1]) {
swap=nodeX[i]; nodeX[i]=nodeX[i+1]; nodeX[i+1]=swap; if (i) i--;
}
else {
i++;
}
}
// Rellenamos pixeles entre los nodos
for (i=0; i<nodes; i+=2) {
if (nodeX[i]>=right) break;
if (nodeX[i+1]> left ) {
if (nodeX[i ]< left ) nodeX[i ]=left ;
if (nodeX[i+1]> right) nodeX[i+1]=right;
for (pixelX= (int)nodeX[i]; pixelX<nodeX[i+1]; pixelX++){
plot_pixel(pixelX,pixelY,current_color);
}
}
}
}
}
它对普通多边形工作正常,但是当我有一个如下所示的交叉多边形时,它不会填充多边形。 (右边是未填充的多边形,左边是我算法的结果。
当结果应该是这样的时候: