根据行数列数左上角右上角右下角算出矩阵所有坐标及间距,要求C Shap语言实现并带注释
时间: 2023-05-30 19:06:39 浏览: 34
以下是C#代码实现:
```csharp
using System;
class Program
{
static void Main(string[] args)
{
int row = 4; // 矩阵行数
int col = 5; // 矩阵列数
int leftTop = 1; // 左上角坐标值
int rightTop = 5; // 右上角坐标值
int rightBottom = 8; // 右下角坐标值
int x1 = 0, y1 = 0; // 左上角坐标
int x2 = 0, y2 = 0; // 右上角坐标
int x3 = 0, y3 = 0; // 右下角坐标
int dx = 0, dy = 0; // 间距
// 计算左上角坐标
for (int i = 1; i <= row; i++)
{
for (int j = 1; j <= col; j++)
{
if ((i - 1) * col + j == leftTop)
{
x1 = j;
y1 = i;
break;
}
}
}
// 计算右上角坐标
for (int i = 1; i <= row; i++)
{
for (int j = 1; j <= col; j++)
{
if ((i - 1) * col + j == rightTop)
{
x2 = j;
y2 = i;
break;
}
}
}
// 计算右下角坐标
for (int i = 1; i <= row; i++)
{
for (int j = 1; j <= col; j++)
{
if ((i - 1) * col + j == rightBottom)
{
x3 = j;
y3 = i;
break;
}
}
}
// 计算间距
dx = (x3 - x2) / (y3 - y2);
dy = (y2 - y1) / (x2 - x1);
// 输出矩阵所有坐标及间距
Console.WriteLine("矩阵所有坐标及间距:");
for (int i = 1; i <= row; i++)
{
for (int j = 1; j <= col; j++)
{
int x = x1 + (j - 1) * dx;
int y = y1 + (i - 1) * dy;
Console.Write("(" + x + "," + y + ") ");
}
Console.WriteLine();
}
Console.WriteLine("间距:dx = " + dx + ", dy = " + dy);
Console.ReadKey();
}
}
```
注:以上代码假定左上角坐标为(1,1),如果左上角坐标不是(1,1),需要对代码进行相应修改。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![dll](https://img-home.csdnimg.cn/images/20210720083646.png)
![md](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)