c中的重构函数
Refactoring function in c
我有一段代码如下所示:
void printdatamatrix(int xp, int yp, int h)
{
int wide;
int x = xp;
int y = yp;
if(h>=40)
{
wide = h /9;
}
else
{
wide = h / 4;
}
/*vertical - L-shape*/
drawsquare(x, y, wide);
x += wide;
drawsquare(x, y, wide);
x += wide;
drawsquare(x, y, wide);
x += wide;
drawsquare(x, y, wide);
x += wide;
drawsquare(x, y, wide);
x += wide;
drawsquare(x, y, wide);
x += wide;
drawsquare(x, y, wide);
/*horizontal - L-shape*/
x = xp;
y -= wide;
drawsquare(x, y, wide);
y -= wide;
drawsquare(x, y, wide);
y -= wide;
drawsquare(x, y, wide);
y -= wide;
drawsquare(x, y, wide);
y -= wide;
drawsquare(x, y, wide);
y -= wide;
drawsquare(x, y, wide);
y -= wide;
drawsquare(x, y, wide);
x = xp;
y = yp;
x += wide;
y -= wide;
drawsquare(x, y, wide);
y -= wide;
drawsquare(x, y, wide);
y -= wide;
drawsquare(x, y, wide);
y -= wide;
drawsquare(x, y, wide);
y -= wide;
drawsquare(x, y, wide);
y -= wide;
drawsquare(x, y, wide);
x = xp;
y = yp;
x += wide * 2;
y -= wide;
drawsquare(x, y, wide);
y -= wide * 2;
drawsquare(x, y, wide);
y -= wide;
drawsquare(x, y, wide);
y -= wide;
drawsquare(x, y, wide);
y -= wide;
drawsquare(x, y, wide);
y -= wide * 2;
drawsquare(x, y, wide);
x = xp;
y = yp;
x += wide * 3;
y -= wide * 3;
drawsquare(x, y, wide);
y -= wide;
drawsquare(x, y, wide);
y -= wide * 2;
drawsquare(x, y, wide);
y -= wide * 4;
drawsquare(x, y, wide);
.
.
.
etc
drawsquare(x,y,wide) 函数创建一个正方形以形成数据矩阵(条形码)。
我想知道我是否可以将 printdatamatrix(int xp, int yp, int h) 函数重构为
更小。我唯一想到的就是写 2 个 for 循环并包括 L 形(一个用于
垂直的,另一个用于水平的)。
感谢任何帮助。提前致谢!
我创建了一个嵌套循环来绘制存储在二维数组中的图案。是这样的:
void printdatamatrix(int xp, int yp, int h, int dirn)
{
int wide = _convertPixelsToMmForDataMatrix(h);
int x = xp;
int y = yp;
unsigned int i, j;
static bool my_data[ROWS][COLS] = {
{ 1,1,1,1,1,1,1,1,1,1,1,1 },
{ 1,1,1,1,1,1,1,0,1,0,1,0 },
{ 1,1,0,1,1,1,1,0,1,1,0,1 },
{ 1,0,0,1,1,0,1,0,0,0,1,0 },
{ 1,1,0,1,0,1,0,0,1,1,0,1 },
{ 1,1,0,1,1,0,0,1,0,1,0,0 },
{ 1,0,1,1,0,1,1,0,1,0,1,1 },
{ 1,1,1,0,0,0,1,0,0,0,1,0 },
{ 1,1,0,0,0,0,0,1,1,1,1,1 },
{ 1,1,0,0,1,0,0,0,0,0,0,0 },
{ 1,1,0,0,1,0,0,1,0,1,0,1 },
{ 1,0,1,0,1,0,1,0,1,0,1,0 }
};
for (i = 0; i < 12; ++i)
{
for (j = 0; j < 12; ++j)
{
if (my_data[i][j])
{
switch (dirn)
{
case 1: // 270
drawsquare(x + j * wide, y + i * wide, wide);
break;
case 2: // 0
drawsquare(x + i * wide, y - j * wide, wide);
break;
case 3: // 90
drawsquare(x - j * wide, y - i * wide, wide);
break;
case 4: // 180
drawsquare(x - i * wide, y + j * wide, wide);
break;
}
}
}
}
}
我有一段代码如下所示:
void printdatamatrix(int xp, int yp, int h)
{
int wide;
int x = xp;
int y = yp;
if(h>=40)
{
wide = h /9;
}
else
{
wide = h / 4;
}
/*vertical - L-shape*/
drawsquare(x, y, wide);
x += wide;
drawsquare(x, y, wide);
x += wide;
drawsquare(x, y, wide);
x += wide;
drawsquare(x, y, wide);
x += wide;
drawsquare(x, y, wide);
x += wide;
drawsquare(x, y, wide);
x += wide;
drawsquare(x, y, wide);
/*horizontal - L-shape*/
x = xp;
y -= wide;
drawsquare(x, y, wide);
y -= wide;
drawsquare(x, y, wide);
y -= wide;
drawsquare(x, y, wide);
y -= wide;
drawsquare(x, y, wide);
y -= wide;
drawsquare(x, y, wide);
y -= wide;
drawsquare(x, y, wide);
y -= wide;
drawsquare(x, y, wide);
x = xp;
y = yp;
x += wide;
y -= wide;
drawsquare(x, y, wide);
y -= wide;
drawsquare(x, y, wide);
y -= wide;
drawsquare(x, y, wide);
y -= wide;
drawsquare(x, y, wide);
y -= wide;
drawsquare(x, y, wide);
y -= wide;
drawsquare(x, y, wide);
x = xp;
y = yp;
x += wide * 2;
y -= wide;
drawsquare(x, y, wide);
y -= wide * 2;
drawsquare(x, y, wide);
y -= wide;
drawsquare(x, y, wide);
y -= wide;
drawsquare(x, y, wide);
y -= wide;
drawsquare(x, y, wide);
y -= wide * 2;
drawsquare(x, y, wide);
x = xp;
y = yp;
x += wide * 3;
y -= wide * 3;
drawsquare(x, y, wide);
y -= wide;
drawsquare(x, y, wide);
y -= wide * 2;
drawsquare(x, y, wide);
y -= wide * 4;
drawsquare(x, y, wide);
.
.
.
etc
drawsquare(x,y,wide) 函数创建一个正方形以形成数据矩阵(条形码)。 我想知道我是否可以将 printdatamatrix(int xp, int yp, int h) 函数重构为 更小。我唯一想到的就是写 2 个 for 循环并包括 L 形(一个用于 垂直的,另一个用于水平的)。
感谢任何帮助。提前致谢!
我创建了一个嵌套循环来绘制存储在二维数组中的图案。是这样的:
void printdatamatrix(int xp, int yp, int h, int dirn)
{
int wide = _convertPixelsToMmForDataMatrix(h);
int x = xp;
int y = yp;
unsigned int i, j;
static bool my_data[ROWS][COLS] = {
{ 1,1,1,1,1,1,1,1,1,1,1,1 },
{ 1,1,1,1,1,1,1,0,1,0,1,0 },
{ 1,1,0,1,1,1,1,0,1,1,0,1 },
{ 1,0,0,1,1,0,1,0,0,0,1,0 },
{ 1,1,0,1,0,1,0,0,1,1,0,1 },
{ 1,1,0,1,1,0,0,1,0,1,0,0 },
{ 1,0,1,1,0,1,1,0,1,0,1,1 },
{ 1,1,1,0,0,0,1,0,0,0,1,0 },
{ 1,1,0,0,0,0,0,1,1,1,1,1 },
{ 1,1,0,0,1,0,0,0,0,0,0,0 },
{ 1,1,0,0,1,0,0,1,0,1,0,1 },
{ 1,0,1,0,1,0,1,0,1,0,1,0 }
};
for (i = 0; i < 12; ++i)
{
for (j = 0; j < 12; ++j)
{
if (my_data[i][j])
{
switch (dirn)
{
case 1: // 270
drawsquare(x + j * wide, y + i * wide, wide);
break;
case 2: // 0
drawsquare(x + i * wide, y - j * wide, wide);
break;
case 3: // 90
drawsquare(x - j * wide, y - i * wide, wide);
break;
case 4: // 180
drawsquare(x - i * wide, y + j * wide, wide);
break;
}
}
}
}
}