我需要使用角度来移动一个矩形。实际上,当我的移动矩形到达我在if语句中给定的位置时,我想要改变它的方向!
我只需要知道如何移动我的矩形到60、30、60、120、150、270度的位置!
假设如果
circle.Y>=this.Height-80
请看这里:
![在此输入图片描述](https://istack.dev59.com/foLW1.webp)
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
* dy/dy
没有任何作用。请更改代码以反映您实际想要实现的内容。或者用语言解释一下,我们可以告诉您应该进行什么计算。 - mbeckish