根据行数列数左上角右上角右下角算出矩阵所有坐标及间距,要求C Shap语言封装
时间: 2023-05-31 09:03:56 浏览: 86
以下是一个C#语言的矩阵坐标计算类的封装示例:
```csharp
public class MatrixCoordinates
{
private int rows;
private int columns;
private int startX;
private int startY;
private int endX;
private int endY;
private int width;
private int height;
public MatrixCoordinates(int rows, int columns, int startX, int startY, int endX, int endY)
{
this.rows = rows;
this.columns = columns;
this.startX = startX;
this.startY = startY;
this.endX = endX;
this.endY = endY;
this.width = (endX - startX) / (columns - 1);
this.height = (endY - startY) / (rows - 1);
}
public List<Point> GetAllCoordinates()
{
List<Point> coordinates = new List<Point>();
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
int x = startX + j * width;
int y = startY + i * height;
coordinates.Add(new Point(x, y));
}
}
return coordinates;
}
public int GetWidth()
{
return width;
}
public int GetHeight()
{
return height;
}
}
```
使用示例:
```csharp
MatrixCoordinates mc = new MatrixCoordinates(3, 4, 0, 0, 100, 100);
List<Point> coordinates = mc.GetAllCoordinates();
foreach (Point p in coordinates)
{
Console.WriteLine("X: " + p.X + ", Y: " + p.Y);
}
Console.WriteLine("Width: " + mc.GetWidth() + ", Height: " + mc.GetHeight());
```
该示例中创建了一个3行4列的矩阵,左上角坐标为(0,0),右下角坐标为(100,100),然后通过调用GetAllCoordinates方法获取了所有坐标,最后通过GetWidth和GetHeight方法获取了矩阵中每个元素之间的宽度和高度。
阅读全文