我有一个带有图片作为背景的图片框(实际上是地图),在它上面我产生了矩形。这些矩形应该按给定点移动。矩形以指定的速度移动,在到达(或接近)其中一个点后,它开始移动到下一个点。我的问题是,矩形不直接移动到给定点,它只是接近只有一个坐标,所以有些情况下矩形的Y坐标和点的Y坐标是相同,但矩形像60像素,不会移动。 下面我将添加一张图片作为运动的一个例子。蓝色是预期路线,红色是实际路线。我已经检查了一百次坐标,它们是正确的,矩形只是移动到其他地方。注意:这只发生在某些点上。
这是我用来计算矩形相对于X轴和Y轴的移动的代码。
public void Move_calculate(Graphics g)
{
if (points[passed].X == 0 || points[passed].Y == 0) // this happens when the rectangle reaches it final point - it stays where it is (working fine)
{
Redraw(g);
return;
}
speed = randomNumbers.Next(7, 13);
if (points[passed].X > x_coordinate && points[passed].Y > y_coordinate)
{
Bx = points[passed].X;
By = points[passed].Y;
distanceForAlfaX = Bx - x_coordinate; // x_coordinate is the x coordinate of the rectangle
distanceForAlfaY = By - y_coordinate;
if (distanceForAlfaX <= 20 || distanceForAlfaY <= 20) speed = 5; // slowing down when approaching the point
if (distanceForAlfaX + distanceForAlfaY <= 15) passed += 1;
alpha = (distanceForAlfaY / distanceForAlfaX); // tangent alpha
x_change = (int)(speed * (Math.Cos(alpha))); // get distance moved relative to axis X
y_change = (int)Math.Sqrt(((speed * speed) + (x_change * x_change))); // again distance for axis Y, using Pythagoras theorem
x_coordinate += x_change;
y_coordinate += y_change;
}
else if (points[passed].X > x_coordinate && points[passed].Y < y_coordinate)
{
Bx = points[passed].X;
By = points[passed].Y;
distanceForAlfaX = Bx - x_coordinate;
distanceForAlfaY = y_coordinate - By;
if (distanceForAlfaX <= 20 || distanceForAlfaY <= 20) speed = 5;
if (distanceForAlfaX + distanceForAlfaY <= 15) passed += 1;
alpha = (distanceForAlfaY / distanceForAlfaX);
x_change = (int)(speed * (Math.Cos(alpha)));
y_change = (int)Math.Sqrt(((speed * speed) + (x_change * x_change)));
x_coordinate += x_change;
y_coordinate -= y_change;
}
else if (points[passed].X < x_coordinate && points[passed].Y > y_coordinate)
{
Bx = points[passed].X;
By = points[passed].Y;
distanceForAlfaX = x_coordinate - Bx;
distanceForAlfaY = By - y_coordinate;
if (distanceForAlfaX <= 20 || distanceForAlfaY <= 20) speed = 5;
if (distanceForAlfaX+distanceForAlfaY <= 15) passed += 1;
alpha = (distanceForAlfaY / distanceForAlfaX);
x_change = (int)(speed * (Math.Cos(alpha)));
y_change = (int)Math.Sqrt(((speed * speed) + (x_change * x_change)));
x_coordinate -= x_change;
y_coordinate += y_change;
}
else if (points[passed].X < x_coordinate && points[passed].Y < y_coordinate)
{
Bx = points[passed].X;
By = points[passed].Y;
distanceForAlfaX = x_coordinate - Bx;
distanceForAlfaY = y_coordinate - By;
if (distanceForAlfaX <= 20 || distanceForAlfaY <= 20) speed = 5;
if (distanceForAlfaX + distanceForAlfaY <= 15) passed += 1;
alpha = (distanceForAlfaY / distanceForAlfaX);
x_change = (int)(speed * (Math.Cos(alpha)));
y_change = (int)Math.Sqrt(((speed * speed) + (x_change * x_change)));
x_coordinate -= x_change;
y_coordinate -= y_change;
}
else
{
MessageBox.Show("Something went wrong"); // just notify me that it isnt working again..
}
Pen p = new Pen(Color.Turquoise, 2);
r = new Rectangle(x_coordinate, y_coordinate, 5, 5); // redraw the rectangle
g.DrawRectangle(p, r);
p.Dispose();
}
我不知道为什么会这样,有人可以帮忙吗? 附: 绝对不需要移动平滑,矩形的位置将使用定时器每两秒更新一次。现在它暂时设置为一个按钮。
编辑: 这是foreach代码。标签只是PictureBox旁边显示的坐标
foreach (aircraft acft in aircrafts) // aircraft is an array aircrafts[]
{
label2.Text = "xp" + acft.points[acft.passed].X;
label3.Text = "yp" + acft.points[acft.passed].Y;
label4.Text = acft.passed.ToString();
label5.Text = "y" + acft.y_coordinate.ToString();
//MessageBox.Show(acft.points[0].X.ToString());
acft.Move_calculate(e.Graphics);
spawn = string.Empty;
}
EDIT2:aircraft
类
public string callsign;
public int speed;
public double heading;
public bool moving = false;
public Point[] points;
public double alpha;
public int x_change;
public int y_change;
public int x_coordinate;
public int y_coordinate;
public int Bx;
public int By;
public double distanceForAlfaX;
public double distanceForAlfaY;
public int passed = 0;
public Rectangle r;
答案 0 :(得分:1)
你检查过你的坐标系吗? (http://www.bobpowell.net/coordinatesystems.htm)
抱歉,您的问题链接错误。尝试使用Control.PointToClient进行调试,以确保所有坐标都在客户端空间中表示。 (http://msdn.microsoft.com/en-us/library/system.windows.forms.control.pointtoclient.aspx)
你可以尝试:
Point cPoint = this.PointToClient(new Point(x_coordinate, y_coordinate));
Size cSize = new Size(5,5);
r = new Rectangle(cPoint, cSize); // redraw the rectangle
g.DrawRectangle(p, r);
您是否可以发布变量的数据类型以澄清您的代码?你可能会在某处丢失精度,特别是如果你使用整数除法。
(关于DJ KRAZE,a - = b;在C#中也可以表示a = a - b;上下文很重要。)
答案 1 :(得分:1)
我猜,
中存在数学错误y_change = (int)Math.Sqrt(((speed * speed) + (x_change * x_change)));
...而且
// again distance for axis Y, using Pythagoras theorem
让Mr.Pythagoras成为,我宁愿使用与X轴相同的
y_change = (int)(speed * (Math.Sin(alpha)));