使用角度移动矩形

7

我需要使用角度来移动一个矩形。实际上,当我的移动矩形到达我在if语句中给定的位置时,我想要改变它的方向!

我只需要知道如何移动我的矩形到60、30、60、120、150、270度的位置!

假设如果

          circle.Y>=this.Height-80

请看这里:在此输入图片描述 我需要通过角度来改变矩形的方向!当矩形运动到特定位置时,根据我选择的角度更改矩形的方向!例如:
if(circle.Y>=this.Height-80)
    move in the direction of 90 degrees

if(circle.X>=this.Width-80)
    move in the direction of 60 degree

正如您在屏幕截图中看到的那样!

我一直在尝试的是:

public partial class Form1 : Form
{
    Rectangle circle;
    double dx = 2;
    double dy = 2;

    public Form1()
    {
        InitializeComponent();
        circle = new Rectangle(10, 10, 40, 40);
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        this.Refresh();
    }

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        Graphics g = e.Graphics;
        g.SmoothingMode = SmoothingMode.AntiAlias;
        g.FillEllipse(new SolidBrush(Color.Red), circle);
    }

    private void timer_Tick(object sender, EventArgs e)
    {
        circle.X += (int)dx;
        circle.Y += (int)dy;
        if (circle.Y>=this.Height-80)
        {
            dy = -Math.Acos(0) * dy/dy; //here i want to change the direction of circle at 90 degrees so that it should go up vertically straight with same speed
        }
        this.Refresh();
    }
}

问题是我一直试图改变我的条件为:
dy = -Math.Asin(1) * dy;
dx = Math.Acos(0) * dx ;

但在这两种情况下,没有任何事情发生,方向仍然相同!当圆圈到达时,我只希望将其以90度的反向上移方向移动。
circle.Y>=this.Height-80

7
即使这是一个任务,我们仍然会提供帮助。对我们来说,重要的是你在编写代码时付出的努力。 - But I'm Not A Wrapper Class
答案仍然一样亲,看到 dx=dy=2; 虽然 dx/dy=dy/dx=1; - Java Nerd
1
@user3411946 - 不要添加无用的代码,这只会让我们感到困惑。显然 * dy/dy 没有任何作用。请更改代码以反映您实际想要实现的内容。或者用语言解释一下,我们可以告诉您应该进行什么计算。 - mbeckish
是的,那是因为我的矩形改变了方向。 - Java Nerd
3个回答

3
你需要将矩形再次绘制到某个图像中以使其显示出来。我已经为在 pictureBox1 上移动和绘制矩形创建了此代码,使用你已定义的circle矩形: 移动矩形:
public void MoveRectangle(ref Rectangle rectangle, double angle, double distance)
{
   double angleRadians = (Math.PI * (angle) / 180.0);
   rectangle.X = (int)((double)rectangle.X - (Math.Cos(angleRadians) * distance));
   rectangle.Y = (int)((double)rectangle.Y - (Math.Sin(angleRadians) * distance));
}

绘制矩形并在 PictureBox 中显示:
public void DrawRectangle(Rectangle rectangle)
{
   Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
   using (Graphics g = Graphics.FromImage(bmp))
   {
      g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
      g.FillEllipse(new SolidBrush(Color.Red), rectangle);
   }
   pictureBox1.Image = bmp;
}

点击按钮进行演示:

private void Button1_Click(object sender, EventArgs e)
{
   MoveRectangle(ref circle, 90, 5);
   DrawRectangle(circle);
}

很好,我不需要整个代码,但是从你的回答中得到了很棒的想法。谢谢你的奖励! - Java Nerd

1

Math.Asin(1) * dy是一个常数值。因此,你应该使用例如每个计时器Tick中递增的实例变量。

*dy/dy是无关紧要的。

public partial class Form1 : Form
{
    Rectangle circle;
    double dx = 2;
    double dy = 2;
    acum=0; //the new variable

...

private void timer_Tick(object sender, EventArgs e)
    {
        circle.X += (int)dx;
        circle.Y += (int)dy;
        if (circle.Y>=this.Height-300)
        {
            dy = -Math.Acos(acum); 
            acum+=1; //your accumulator
        }
        this.Refresh();
    }

1

acos和asin是sin和cos的反函数,因此这两个函数的输出结果是一个角度(通常以弧度表示)。这使得代码不正确。

我强烈建议您阅读向量和矩阵数学知识,因为使用欧拉角可能会变得非常混乱。

因此,您将有一个位置向量P和一个移动向量M,当前位置为:

 P' = P + M.t

其中t表示时间,P表示原始位置,P'表示当前位置。

然后,当您想要改变方向时,您创建一个旋转矩阵,并将运动向量M乘以此旋转矩阵。

这里的优点是,您可以通过向您的向量添加Z分量和增加矩阵大小来从2D系统转换为3D系统。


实际上,在你的例子中,旋转矩阵是不需要的,新的运动向量将是 (0,1),假设向上为 +y。 - Skizz
那么我该如何真正实现这个? - Java Nerd

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接