我需要找到2个最接近图片框内点的边缘。我正在使用以下方法
void findclosestedges(Point p)
{
//Xedge=1 -- Left Edge is closer to Point 2--Right Edge
int Xedge,Yedge;
//Finding closest Left/Right Edge
if (p.X < (Width - p.X))
{
Xedge = 1;
LaunchOrigin2.closestedge.Text = " ";
LaunchOrigin2.closestedge.Text = LaunchOrigin2.closestedge.Text + " left";
}
else
{
Xedge = 2;
LaunchOrigin2.closestedge.Text = " ";
LaunchOrigin2.closestedge.Text = LaunchOrigin2.closestedge.Text + " right";
}
//Finding closest Top/Bottom Edge
if (p.Y < (Height - p.Y))
{
Yedge = 1;
LaunchOrigin2.closestedge.Text = LaunchOrigin2.closestedge.Text + " top";
}
else
{
Yedge = 2;
LaunchOrigin2.closestedge.Text = LaunchOrigin2.closestedge.Text + " bottom";
}
}
我的方法是否正确?这种方法有什么警告吗?
请建议。
答案 0 :(得分:1)
正如评论中所提到的,这可能不是正确的地方,因为你的代码工作正常,至少据我所知?
但是一些评论可能是我不确定为什么你将文本设置为值然后在下一行添加它?
LaunchOrigin2.closestedge.Text = " ";
LaunchOrigin2.closestedge.Text = LaunchOrigin2.closestedge.Text + " left";
为什么不直接这样做:
LaunchOrigin2.closestedge.Text = " " + " left";
当然也是正确的方法:
LaunchOrigin2.closestedge.Text = " left";
我还会缩短代码,如下所示:
void findclosestedges(Point p) {
int Xedge = p.X < (Width - p.X) ? 1 : 2;
int Yedge = p.Y < (Height - p.Y) ? 1 : 2;
LaunchOrigin2.closestedge.Text = (Xedge == 1 ? " left" : " right") + (Yedge == 1 ? " top" : " bottom");
}
但那只是我的两分钱......