c - 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 个并包括 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;
}
}
}
}
}
推荐阅读
- android - Android studio error - react native using firebase
- greenplum - 表greenplum数据库中缺少分布键
- excel - 如何从 maatwebsite/Excel 向集合添加额外数据
- git - 如何获取 github 文件的查询 url?
- python - 在尝试粘贴时,使用 tkinter 从 Windows 10 中的 Python 3.4 复制到剪贴板会导致崩溃
- android - simd 是否包含在 Android NDK 中
- python - python从套接字流接收图像
- javascript - Nodejs发布未访问文件所在的目录
- javascript - 单击添加按钮后元素立即消失
- javascript - Angular - 方法不等待从 Firebase 获取值