0

我有一个以图片为背景的图片框(实际上是一张地图),并且在其上生成矩形。这些矩形应该移动给定的点。矩形以指定的速度移动,在到达(或接近)其中一个点后,它开始移动到下一个点。然而我的问题是,矩形并没有直接移动到给定点,它只是继续接近其中一个坐标,所以在某些情况下,矩形的 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();
    }

我不知道为什么会这样,有人可以帮忙吗?PS 运动绝对不需要平滑,矩形的位置将使用 Timer 每两秒更新一次。现在它暂时设置为一个按钮。

编辑:这是 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;
4

2 回答 2

1

你检查过你的坐标系吗?(https://web.archive.org/web/20140710074441/http://bobpowell.net/coordinatesystems.aspx

抱歉,您的问题的链接错误。尝试使用 Control.PointToClient 进行调试,以确保所有坐标都在客户端空间中表示。(https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.control.pointtoclient

你可以试试:

    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;上下文很重要。)

于 2013-03-09T21:26:28.937 回答
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)));
于 2013-03-10T18:59:05.103 回答