请教前辈一个关于算法的问题、。。!
int rowNumber = 10, colNumber = 10;int[,]a=new int [rowNumber,colNumber];
double cellsize=1;
int lineID = 1,col,j,row;
double x1 = 1, y1 = 2;
double x2 = 8, y2 = 6;
double k = (y2-y1)/(x2-x1),b = y1-x1*k;
int col1=(int)(x1/cellsize),col2=(int)(x2/cellsize);
int row1=(int)(y1/cellsize),row2=(int)(y2/cellsize);
if (k > 1 || k < -1)
{
for (col= col1; col < col2;col++ )
{
double y0 =col*k+b;
double x = col+1;
double y = x*k+b;
double p = Math.Ceiling(y-y0);
for(j=0;j<p;j++)
{
int yz = (int)(y0);
a[yz, col] = lineID;
Console.WriteLine("a[{0},{1}]={2}", yz,col, a[yz, col]);
y0++;
}
}
}
else
{
for (row = row1; row < row2;row++ )
{
double x0 =(row-b)/k;
double y = row+1;
double x = (y-b)/k;
double p = Math.Ceiling(x - x0);
for(j=0;j<p;j++)
{
int xz = (int)(x0);
a[row, xz] = lineID;
Console.WriteLine("a[{0},{1}]={2}", row,xz, a[row,xz]);
;
x0++;
}
}
}
程序定义了一个10乘10的数组。。想要看看从点(1,2)到点(8,6)形成的线段穿过了哪些“格子”。。然后给穿过的这些格子赋值lineID。。。。每次运行出来结果都不对。好像差了几个格子。请各位前辈指教一下。。。很着急很着急!!!非常感谢!!!