using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Threading; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Diagnostics; using System.Windows.Media.Media3D; namespace Orbital { public delegate void PosDelegate(double posX, double posY); public class Astro { public double G = 0.00000000006674d; //%gravitational constant% public double SunMass = 2000000000000000000000000000000d; //Sun mass public double PosX; public double PosY; public double VelX; public double VelY; public double AccX; public double AccY; public Astro(double PosX0, double PosY0, double VelX0, double VelY0) { this.PosX = PosX0 * 1000000000d; this.PosY = PosY0 * 1000000000d; this.VelX = VelX0 * 1000d; //m/s this.VelY = VelY0 * 1000d; //m/s double r = Math.Sqrt(Math.Pow(PosX,2) + Math.Pow(PosY, 2)); double Gravity0 = G * SunMass; //m / s2 this.AccX = -PosX * Gravity0 / (r * r * r); //% m / s2 % this.AccY = -PosY * Gravity0 / (r * r * r); //% m / s2 % } } public partial class MainWindow : Window { private Thread ParallelThread; private bool endThread = false; public MainWindow() { InitializeComponent(); ParallelThread = new Thread(new ThreadStart(rutinaParalela)); ParallelThread.Start(); } private void rutinaParalela() { PosDelegate delegatePlot = new PosDelegate(Plot); Astro asteroid = new Astro(150, 0, 0 ,30); double G = asteroid.G; double SunMass = asteroid.SunMass; double PosX = asteroid.PosX; double initialX= PosX / 1000000000d; double PosY = asteroid.PosY; double initialY = PosY / 1000000000d; double VelX = asteroid.VelX; double VelY = asteroid.VelY; double AccX = asteroid.AccX; double AccY = asteroid.AccY; while (!endThread) { //update position PosX += VelX * 86400d + 0.5d * AccX * 86400d * 86400d; //% m double dX = (VelX * 86400d + 0.5d * AccX * 86400d * 86400d) / 1000000000d + initialX; PosY += VelY * 86400d + 0.5d * AccY * 86400d * 86400d; //% m double dY = (VelY * 86400d + 0.5d * AccY * 86400d * 86400d) / 1000000000d + initialY; //update distance to Sun double r = Math.Sqrt(Math.Pow(PosX, 2) + Math.Pow(PosY, 2)); //update Velocity VelX = VelX + AccX * 86400d; //% m / s % VelY = VelY + AccY * 86400d; //% m / s % //update acceleration double Gravity = G * SunMass; //m / s2 AccX = -PosX * Gravity / (r * r * r); //% m / s2 % AccY = -PosY * Gravity / (r * r * r); //% m / s2 % //correction for second loop initialX = 0; initialY = 0; wnd_Principal.Dispatcher.BeginInvoke(delegatePlot, dX, dY); Thread.Sleep(50); } } private void Plot(double dX, double dY) { TranslateTransform translateTransform = new TranslateTransform(); translateTransform.X = dX; translateTransform.Y = -dY; cvasteroid.RenderTransform = translateTransform; } } }