C#绘制窗口边缘的图形

时间:2016-09-15 21:10:25

标签: c# winforms graphics

我正在开发一个模拟交通模式的应用程序,并将每个车辆表示为彩色矩形。

当程序运行时,每个车辆的位置会根据速度递增,直到它太靠近前面的车辆。

private void SlowDown(int carBehind, int carAhead, List<Vehicle> vehicles)
{
        vehicles[carBehind].SetActualVelocity(vehicles[carAhead].GetVelocity());
}

然后我增加每个车辆的位置:

private void AdvancePosition(List<Vehicle> vehicles)
    {
        Vehicle tempVehicle;
        double velocity = 0;
        int position = 0;
        int newPosition = 0;
        for(int i = vehicles.Count - 1; i >= 0; i--) //starting with forward vehicles first
        {
            tempVehicle = vehicles[i];
            velocity = tempVehicle.GetVelocity() * 1.466667 / 33; //convert mph to fps then allow for 30 frames per second
            position = tempVehicle.GetPosition();
            newPosition = (int)(velocity + position);
            tempVehicle.SetPosition(newPosition);
        }
    }

这种方法很有效,直到车辆从屏幕外显示到屏幕上(或位置为负位置,位置为正)。它们开始相互爬行并最终重叠,即使我将它们的速度设置为相等。

以下是我绘制图形的代码

private void paintTraffic(Road road, List<Vehicle> vehicles)
    {

        using (Graphics g = CreateGraphics())
        {
            Vehicle tempVehicle;
            int x = 0;
            int y = 0;
            int index = 0;
            int vehicleSize = 0;

            Refresh();
            for (int i = 0; i < vehicles.Count; i++)
            {
                /*
                 * I tried using this condition but I'm still seeing the problem
                 *if(x < vehicleSize)
                 *{
                 *   vehicleSize = x;
                 *}
                 */
                tempVehicle = vehicles[i];
                x = tempVehicle.GetPosition();
                y = tempVehicle.GetLane() * 100;
                vehicleSize = tempVehicle.GetSize();
                g.FillRectangle(new SolidBrush(color), x, y, vehicleSize, 20);
            }

        }
    }

每辆车被涂成一个小矩形,车辆的位置是车辆的前部。

我的问题是:这似乎不是程序的计算错误,因为车辆的速度设置为前面车辆的速度。

这听起来像是图形错误还是奇怪的计算错误?

0 个答案:

没有答案