在2d网格上填充颜色矩形

时间:2015-08-12 22:53:15

标签: c# algorithm unity3d geometry rasterizing

按照这篇文章Efficient Polygon Fill Algorithm With C我已经在2d网格上实现了rectagle填充颜色,但它给我一些错误的结果,请看下面的截图:

Screenshots

源代码:

    GroundGridCell[] obbVerticesCells = new GroundGridCell[componentRef.OBBVertices.Length];
    for(int ii = 0; ii < componentRef.OBBVertices.Length; ii++){
        obbVerticesCells[ii] = componentRef.Grid.GetCellAtPos(componentRef.OBBVertices[ii]);
    }

    GroundGridCell[] sortedobbVerticesCellsY = obbVerticesCells.OrderByDescending((c) => { return c.YIndex; }).ToArray();
    GroundGridCell[] sortedobbVerticesCellsX = obbVerticesCells.OrderByDescending((c) => { return c.XIndex; }).ToArray();

    int nodes, cellX, cellY, maxY, minY, minX, maxX, i, j, swap;
    int[] nodeX = new int[obbVerticesCells.Length];
    maxY = sortedobbVerticesCellsY[0].YIndex;
    minY = sortedobbVerticesCellsY[sortedobbVerticesCellsY.Length - 1].YIndex;
    maxX = sortedobbVerticesCellsX[0].XIndex;
    minX = sortedobbVerticesCellsX[sortedobbVerticesCellsX.Length - 1].XIndex;

    //Loop through the rows of the image.
    for (cellY = minY; cellY < maxY; cellY++){
        //Build a list of nodes.
        nodes = 0; j = obbVerticesCells.Length - 1;
        for (i = 0; i < obbVerticesCells.Length; i++){
            if (cellY > obbVerticesCells[i].YIndex && cellY <= obbVerticesCells[j].YIndex ||  
                cellY > obbVerticesCells[j].YIndex && cellY <= obbVerticesCells[i].YIndex){

                //solving linear equation
                //     x1 * y2   x2 * y1   y * (x2 - x1)
                // x = ------- - ------- + -------------
                //     y2 - y1   y2 - y1      y2 - y1
                int y2y1 = obbVerticesCells[j].YIndex - obbVerticesCells[i].YIndex;
                int f1 = (obbVerticesCells[i].XIndex * obbVerticesCells[j].YIndex) / y2y1;
                int f2 = (obbVerticesCells[j].XIndex * obbVerticesCells[i].YIndex) / y2y1;
                int f3 = (cellY * (obbVerticesCells[j].XIndex - obbVerticesCells[i].XIndex)) / y2y1;
                nodeX[nodes++] = f1 - f2 + f3;
            }

            j = i; 
        }

        //Sort the nodes
        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 == 1) i--;
            }
            else {
                i++;
            }
        }

        // fill each pair
        for (i = 0; i < nodes; i += 2) {
            if (nodeX[i] >= maxX) 
                break;

            if (nodeX[i + 1] > minX ){
                if (nodeX[i] < minX ) 
                    nodeX[i] = minX;

                if (nodeX[i + 1] > maxX) 
                    nodeX[i + 1] = maxX;

                for (cellX = nodeX[i]; cellX < nodeX[i + 1]; cellX++) {
                    GroundGridCell resultingCell = componentRef.Grid.GetCellAtIndex(cellX, cellY);
                    if(resultingCell != null){
                        resultingCell.ChangeCellColorInMesh(componentRef.ColorKey);
                    }

                }
            }
        }
    }

0 个答案:

没有答案