O--------------------------------------------------->X
|
|
|
|
↓Y
在Windows Form中,坐标系的定义如上图所示,有时我们要在窗口中绘制矩形,能够方便得到的是起始点的坐标和终点的坐标,而如何计算四个点的坐标方式也是很多的。
起始点为start, 终点为end, pointLT, pointLB, pointRT, pointRB分别为左上角,左下角,右上角,右下角
方法一:(很复杂不推荐)
将end.X, end.Y 分别与start.X, start.Y作比较,一共四类情况,赋值的时候非常复杂,写代码麻烦
if (start.X < end.X && start.Y < end.Y)
{
/*
S.....................
* ....................
* ....................
* ....................E
*/
pointLT = start;
pointLB.X = start.X; pointLB.Y = end.Y;
pointRB = end;
pointRT.X = end.X; pointRT.Y = start.Y;
}
else if (start.X > end.X && start.Y > end.Y)
{
/*
E.....................
* ....................
* ....................
* ....................S
*/
……
方法二(代码书写方便)
左上角是start与end两者坐标的较小值,利用这个思想确定四点坐标
pointLT.X = (start.X <= end.X) ? start.X : end.X;
pointLT.Y = (start.Y <= end.Y) ? start.Y : end.Y;
pointRB.X = (start.X >= end.X) ? start.X : end.X;
pointRB.Y = (start.Y >= end.Y) ? start.Y : end.Y;
pointRT.X = pointRB.X;
pointRT.Y = pointLT.Y;
pointLB.X = pointLT.X;
pointLB.Y = pointRB.Y;
显然方法二的思路明确,代码清晰,书写方便~