我在使该功能正常工作时遇到问题。我必须创建一个充当出租车映射器的Winform应用程序。在加载时,出租车根据文本文件放置在相同的位置。当用户点击表格时,最近的出租车应该移动到“用户”或位置,然后停车。根据winform中的鼠标单击找到最短距离的对象
除了最近的出租车并不总是去的位置,一切工作正常。更远的出租车将转到该位置。它似乎在某些时候有效,但并非全部。我不知道如果我的逻辑是在Form1_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!
}
}