角度を使用して長方形を移動する必要があります。実際には、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