Движение по синусоиде - C#
Формулировка задачи:
Есть анимация солнечной системы. Земля вращается вокруг Солнца, а Луна вокруг Земли. Нужно сделать так, чтобы Земля перемещалась по синусоиде.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Animation
{
public partial class Galaxy : Form
{ // декларация переменных, описывающих движение Земли и Луны
float UgolWrascheniaZemli = 0;
float UgolWrascheniaLuny = 0;
// прирост изменения положения Земли и Луны
float PrirostUglaWrascheniaZemli = 0.05f;
float PrirostUglaWrascheniaLuny = 0.1f;
public Galaxy()
{
InitializeComponent();
this.Left = 10;
this.Top = 10;
this.Width = (int)(Screen.PrimaryScreen.Bounds.Width * 0.7f);
this.Height = (int)(Screen.PrimaryScreen.Bounds.Height * 0.85f);
this.BackColor = Color.DarkBlue;
}
private void timer1_Tick(object sender, EventArgs e)
{
UgolWrascheniaZemli = UgolWrascheniaZemli + PrirostUglaWrascheniaZemli;
if (UgolWrascheniaZemli >= 360)
UgolWrascheniaZemli = 0;
UgolWrascheniaLuny = UgolWrascheniaLuny + PrzyrostKД…taObrotuKsiД™Ејyca;
if (UgolWrascheniaLuny >= 360)
UgolWrascheniaLuny = 0;
this.Refresh();
}
private void Galaxy_Paint(object sender, PaintEventArgs e)
{
// рисовка Солнца
e.Graphics.FillEllipse(Brushes.Yellow, this.Width / 2 - 80, this.Height / 2 - 80, 160, 160);
// вычисления положения Земли
float Zx = (float)(0.01) + (float)(this.Width / 2 + this.Width / 4 * Math.Sin(UgolWrascheniaZemli));
float Zy = (float)(this.Height / 2 - this.Width / 4 * Math.Cos(UgolWrascheniaZemli));
// рисовка Земли
e.Graphics.FillEllipse(Brushes.LimeGreen, Zx - 25, Zy - 25, 50, 50);
// вычисления положения Луны
float Kx = (float)(Zx + this.Width / 12 * Math.Sin(UgolWrascheniaLuny));
float Ky = (float)(Zy - this.Width / 12 * Math.Cos(UgolWrascheniaLuny));
// рисовка Луны
e.Graphics.FillEllipse(Brushes.LightYellow, Kx - 5, Ky - 5, 10, 10);
}
}
}Решение задачи: «Движение по синусоиде»
textual
Листинг программы
float Zy = (float)(this.Height / 2 - this.Width / 4 * Math.Cos(UgolWrascheniaZemli) * 1.3D);