在两个向量之间移动图像

时间:2016-07-16 13:04:03

标签: c# graphics draw win2d

所以我希望在附带的屏幕截图中有一个沿着白线移动的图像。但我不确定如何实现这一目标。这是我的例子 坐标:

141,78和509,223

我绘制一条线来显示路径,最终我希望在一段时间内将图像从开始移动到结束。比方说5分钟。

过去18个小时我一直在网上搜索,但我仍然卡住了。有人可以帮忙吗?

enter image description here

1 个答案:

答案 0 :(得分:1)

你需要一个游戏循环,并且需要以固定的间隔执行代码。如果这是您想要做的唯一动画,那么更简单的计时器组件可以触发更新位置的代码,比如每秒50次(Interval=20 ms)。

以下是在空白PictureBox顶部绘制的圆圈中的图像的骨架代码。

public partial class Form1 : Form
{
    float t = 0;

    public Form1()
    {
        InitializeComponent();
    }

    private void pictureBox1_Resize(object sender, EventArgs e)
    {
        pictureBox1.Refresh();
    }

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        Image img = Resources.Image1;
        float dx = img.Width, dy = img.Height;
        float r = 100;
        e.Graphics.TranslateTransform(pictureBox1.Width / 2, pictureBox1.Height / 2);
        e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
        // draw a gray circle to indicate the path visually
        e.Graphics.DrawEllipse(Pens.Gray, -r, -r, 2 * r, 2 * r);

        // you must set the x,y coordinate of the center of the image
        // according to your path.
        // If it is a line use linear interpolation
        // x = x_start + t*(x_end-x_start);
        // y = y_start + t*(y_end-y_start);
        float x = (float)(r * Math.Cos(2 * Math.PI * t));
        float y = -(float)(r * Math.Sin(2 * Math.PI * t));

        e.Graphics.DrawImageUnscaled(img, (int)(x - dx / 2), (int)(y - dy / 2));
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        if (t >= 1)
        {
            t -= 1;
        }
        t += 0.02f;
        pictureBox1.Refresh();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        timer1.Interval = 20;
        timer1.Start();
    }
}

scr

相关问题