C# winforms中基于鼠标点击的物体最短距离搜索
我在使此函数正常工作时遇到问题。我必须创建一个winform应用程序作为出租车地图绘制程序。装载时,出租车根据文本文件放置在同一位置。当用户点击表单时,最近的出租车应该移动到“用户”或位置,然后停止 一切正常,只是最近的出租车并不总是去那个地方。再远一点的出租车将前往该地点。它似乎在某些时候起作用,但不是一直起作用。我不确定C# winforms中基于鼠标点击的物体最短距离搜索,c#,.net,winforms,C#,.net,Winforms,我在使此函数正常工作时遇到问题。我必须创建一个winform应用程序作为出租车地图绘制程序。装载时,出租车根据文本文件放置在同一位置。当用户点击表单时,最近的出租车应该移动到“用户”或位置,然后停止 一切正常,只是最近的出租车并不总是去那个地方。再远一点的出租车将前往该地点。它似乎在某些时候起作用,但不是一直起作用。我不确定Form1\u MouseDown函数中的逻辑是否正确 private void Form1_MouseDown(object sender, MouseEventArgs
Form1\u MouseDown
函数中的逻辑是否正确
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (pickUp) //Are we picking up a passenger?
{
//Convert mouse pointer location to local window locations
int mLocalX = this.PointToClient(Cursor.Position).X;
int mLocalY = this.PointToClient(Cursor.Position).Y;
//set the minimum value (for range finding)
int min = int.MaxValue;
//Temporary object to get the handle for the taxi object we want to manipulate
taxiCabTmp = new TaxiClass();
//Iterate through each object to determine who is the closest
foreach (TaxiClass taxiCab in taxi)
{
if (Math.Abs(Math.Abs(taxiCab.CabLocationX - mLocalX) + Math.Abs(taxiCab.CabLocationY - mLocalY)) <= min)
{
min = Math.Abs(Math.Abs(taxiCab.CabLocationX - mLocalX) + Math.Abs(taxiCab.CabLocationY - mLocalY));
//We found a minimum, grab a handle to the object's instance
taxiCab.GetReference(ref taxiCabTmp);
}
}
//Call the propogate method so it can spin off a thread to slowly change it's location for the timer to also change
taxiCabTmp.Propogate(mLocalX - 20, mLocalY - 20);
taxiCabTmp.occupied = true; //This taxi object is occupied
pickUp = false; //We are not picking up a passenger at the moment
}
else //We are dropping off a passenger
{
taxiCabTmp.Propogate(this.PointToClient(Cursor.Position).X, this.PointToClient(Cursor.Position).Y);
taxiCabTmp.occupied = false;
pickUp = true; //We can pick up a passenger again!
}
}
private void Form1\u MouseDown(对象发送方,MouseEventArgs e)
{
如果(接送)//我们在接送乘客吗?
{
//将鼠标指针位置转换为本地窗口位置
int mLocalX=this.PointToClient(Cursor.Position).X;
int mLocalY=this.PointToClient(Cursor.Position).Y;
//设置最小值(用于测距)
int min=int.MaxValue;
//获取要操纵的滑行对象句柄的临时对象
taxiCabTmp=新的滑行等级();
//遍历每个对象以确定谁是最接近的对象
foreach(出租车等级出租车中的出租车)
{
如果(Math.Abs(Math.Abs(taxiCab.CabLocationX-mLocalX)+Math.Abs(taxiCab.CabLocationY-mLocalY))正确,则用于确定距离的计算并不总是正确的。具有一定角度的对象计算出来的距离将比实际距离远
有关更多信息,请查看此链接:
下面是一个例子:
int mLocalX = 1;
int mLocalY = 1;
int taxiCab.CabLocationX = 2;
int taxiCab.CabLocationY = 2;
double distance = Math.Sqrt(Math.Pow((taxiCab.CabLocationX - mLocalX), 2) + Math.Pow((taxiCab.CabLocationY - mLocalY), 2));
作为旁注,您不应该将类附加到类中,即TaxiClass,它应该简单地称为Taxi。您正确地认识到,您用于确定距离的计算并不总是正确的。具有一定角度的对象将计算为比实际距离更远
有关更多信息,请查看此链接:
下面是一个例子:
int mLocalX = 1;
int mLocalY = 1;
int taxiCab.CabLocationX = 2;
int taxiCab.CabLocationY = 2;
double distance = Math.Sqrt(Math.Pow((taxiCab.CabLocationX - mLocalX), 2) + Math.Pow((taxiCab.CabLocationY - mLocalY), 2));
作为旁注,您不应该将类附加到Class,即TaxiClass,它应该简单地称为Taxi。使用此公式计算两个坐标之间的距离
var distance = Math.Sqrt(Math.Pow(taxiCab.CabLocationX - mLocalX, 2) + Math.Pow(taxiCab.CabLocationY - mLocalY, 2));
if (distance <= min) {
min = distance;
//We found a minimum, grab a handle to the object's instance
taxiCab.GetReference(ref taxiCabTmp);
}
var distance=Math.Sqrt(Math.Pow(taxiCab.CabLocationX-mLocalX,2)+Math.Pow(taxiCab.CabLocationY-mLocalY,2));
如果(距离使用此公式计算两个坐标之间的距离
var distance = Math.Sqrt(Math.Pow(taxiCab.CabLocationX - mLocalX, 2) + Math.Pow(taxiCab.CabLocationY - mLocalY, 2));
if (distance <= min) {
min = distance;
//We found a minimum, grab a handle to the object's instance
taxiCab.GetReference(ref taxiCabTmp);
}
var distance=Math.Sqrt(Math.Pow(taxiCab.CabLocationX-mLocalX,2)+Math.Pow(taxiCab.CabLocationY-mLocalY,2));
如果(距离)