rows = int(input("请输入行数:"))cols = int(input("请输入列数:"))left_top = input("请输入左上角坐标,格式如'2,3':").split(",")right_top = input("请输入右上角坐标,格式如'2,3':").split(",")right_bottom = input("请输入右下角坐标,格式如'2,3':").split(",")lt_row, lt_col = int(left_top[0]), int(left_top[1])rt_row, rt_col = int(right_top[0]), int(right_top[1])rb_row, rb_col = int(right_bottom[0]), int(right_bottom[1])matrix = []for i in range(rows): row = [] for j in range(cols): if i == lt_row and j == lt_col: row.append("LT") elif i == rt_row and j == rt_col: row.append("RT") elif i == rb_row and j == rb_col: row.append("RB") else: row.append("") matrix.append(row)coords = []for i in range(rows): for j in range(cols): coords.append((i, j))print("矩阵:")for row in matrix: print(row)print("所有元素坐标:")print(coords)
时间: 2023-05-27 19:02:19 浏览: 153
对于给定的行数和列数,以及左上角、右上角和右下角坐标,程序会先生成一个空矩阵,并在左上角、右上角和右下角位置上分别填入字符串“LT”、“RT”和“RB”,其他位置填入空字符串。然后程序会生成一个包含所有元素坐标的列表,并输出矩阵和该列表。
相关问题
优化一下这段代码float star = 0, end = 0; if (directionFlag == DIRECTION_FLAG1::RIGHT) { star = p0.x; end = p1.x; } else if (directionFlag == DIRECTION_FLAG1::LEFT) { star = p1.x; end = p0.x; } for (int var = star; var < end; var++) { float x = var; if (var < 0) { x = 0; } else if (var >= cols) { x = cols - 1; } float y =0; if(k!=0) { y = k * x + b; if (y >= rows) { y = rows - 1; } } else if(k==0) { y = p0.y; } if (y < 0) { y = 0; } else if (y >= rows) { y = rows - 1; } int data = inputImg.ptr<uchar>(int (y))[int(x)]; PointX.push_back(x); PointY.push_back(y); Gray.push_back(data); }
这段代码可以进行如下优化:
float star = 0, end = 0;
if (directionFlag == DIRECTION_FLAG1::RIGHT)
{
star = p0.x;
end = p1.x;
}
else if (directionFlag == DIRECTION_FLAG1::LEFT)
{
star = p1.x;
end = p0.x;
}
for (int var = star; var < end; var++) //修正了循环条件
{
float x = var < 0 ? 0 : (var >= cols ? cols - 1 : var); //使用三目运算符代替if-else语句
float y = k * x + b;
if (y < 0) y = 0;
else if (y >= rows) y = rows - 1;
int data = inputImg.ptr<uchar>(int(y))[int(x)];
PointX.push_back(x);
PointY.push_back(y);
Gray.push_back(data);
}
这样可以减少if-else语句的使用,使代码更加简洁和高效。
优化一下这段代码else if(directionFlag == DIRECTION_FLAG1::RIGHT) { for (int var = p0.x; var < p1.x; var++) { float x = var; float y =0; if(var<0) { x = 0; } else if(var>inputImg.rows) { x = inputImg.rows; } if(k!=0) { y = k * x + b; } else if(k==0) { y = p0.y; } int data = inputImg.ptr<uchar>(int (y))[int(x)]; PointX.push_back(x); PointY.push_back(y); Gray.push_back(data); } } else if(directionFlag == DIRECTION_FLAG1::LEFT) { for (int var = p1.x; var < p0.x; var++) { float x = var; float y =0; if(var<0) { x = 0; } else if(var>inputImg.rows) { x = inputImg.rows; } if(k!=0) { y = k * x + b; } else if(k==0) { y = p0.y; } int data = inputImg.ptr<uchar>(int (y))[int(x)]; PointX.push_back(x); PointY.push_back(y); Gray.push_back(data); } }
这段代码可以进行如下优化:
1. 在判断 x 是否越界时,应该使用 inputImg.cols 而不是 inputImg.rows。
2. 在循环中,var 的增量应该为 1,而不是空着不写。
3. 在计算 y 坐标时,可以将 k * x 和 b 分别计算出来,然后再相加,这样可以减少重复计算。
4. 可以将两个 if(k!=0) 和 if(k==0) 合并成一个 if-else 语句。
优化后的代码如下:
else if(directionFlag == DIRECTION_FLAG1::RIGHT) { for (int var = p0.x; var < p1.x; var++) { float x = var; float y = 0; if(x < 0) { x = 0; } else if(x > inputImg.cols) { x = inputImg.cols; } y = k * x + b; int data = inputImg.ptr<uchar>(int (y))[int(x)]; PointX.push_back(x); PointY.push_back(y); Gray.push_back(data); } } else if(directionFlag == DIRECTION_FLAG1::LEFT) { for (int var = p1.x; var < p0.x; var++) { float x = var; float y = 0; if(x < 0) { x = 0; } else if(x > inputImg.cols) { x = inputImg.cols; } y = k * x + b; int data = inputImg.ptr<uchar>(int (y))[int(x)]; PointX.push_back(x); PointY.push_back(y); Gray.push_back(data); } }
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)