C# 在二维网格上用颜色填充矩形
在本文之后,我在2d网格上实现了用颜色填充Rectage,但它给出了一些错误的结果,请参见下面的屏幕截图: 源代码:C# 在二维网格上用颜色填充矩形,c#,algorithm,unity3d,geometry,rasterizing,C#,Algorithm,Unity3d,Geometry,Rasterizing,在本文之后,我在2d网格上实现了用颜色填充Rectage,但它给出了一些错误的结果,请参见下面的屏幕截图: 源代码: GroundGridCell[] obbVerticesCells = new GroundGridCell[componentRef.OBBVertices.Length]; for(int ii = 0; ii < componentRef.OBBVertices.Length; ii++){ obbVerticesCells[ii]
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);
}
}
}
}
}
GroundGridCell[]obbVerticesCells=新的GroundGridCell[componentRef.OBBVertices.Length];
对于(int ii=0;ii{return c.YIndex;}).ToArray();
GroundGridCell[]SortedObjVerticeCellsX=ObjVerticeCells.OrderByDescending((c)=>{return c.XIndex;}).ToArray();
int节点,cellX,cellY,maxY,minY,minX,maxX,i,j,swap;
int[]nodeX=新的int[obbVerticesCells.Length];
maxY=SortedobVerticeCcellsy[0].YIndex;
minY=Sortedobverticescellsy[Sortedobverticescellsy.Length-1].YIndex;
maxX=SortedObjerticesCellsX[0].XIndex;
minX=sortedobverticescellsx[sortedobverticescellsx.Length-1].XIndex;
//循环浏览图像的行。
用于(cellY=minY;cellYobbVerticesCells[i].YIndex&&cellY obbVerticesCells[j].YIndex&&cellY nodeX[i+1]){
swap=nodeX[i];
nodeX[i]=nodeX[i+1];
nodeX[i+1]=交换;
如果(i==1)i--;
}
否则{
i++;
}
}
//把每一对都填满
对于(i=0;i<节点;i+=2){
if(nodeX[i]>=maxX)
打破
if(nodeX[i+1]>minX){
if(nodeX[i]maxX)
nodeX[i+1]=maxX;
对于(cellX=nodeX[i];cellX
忘了添加Y坐标从底部向顶部增长,(0,0)在第一眼看到左下方,我认为这是因为使用int值进行计算。