己知矩形两对角点坐标,计算矩形区域内所有坐标
时不时要用到求坐标,原来写过又忘了,重写一次备忘
private string getAllPoint(Point p1, Point p2)
{
string info = "";
int minNumX = 0;
int minNumY = 0;
for (int i = 0; i < Math.Abs(p2.X - p1.X) + 1; i++)
{
minNumX = (int)(p1.X < p2.X ? p1.X : p2.X) + i;
for (int j = 0; j < Math.Abs(p2.Y - p1.Y) + 1; j++)
{
minNumY = (int)(p1.Y < p2.Y ? p1.Y : p2.Y) + j;
info += minNumX.ToString() + "," + minNumY.ToString() + "|";
}
info += "\r\n";
}
return info;
}
//从左上角到右下角调用
private void Button_Click(object sender, RoutedEventArgs e)
{
Point p1 = new Point(2, 2);
Point p2 = new Point(5, 4);
MessageBox.Show(getAllPoint(p1, p2));
}
运行如图一:
//从左下角到右上角调用
private void Button_Click(object sender, RoutedEventArgs e)
{
Point p1 = new Point(3, 2);
Point p2 = new Point(1, 4);
MessageBox.Show(getAllPoint(p1, p2));
}
运行如图二: