基於C#的2D太陽、地球、月亮運動軌跡模擬實現
阿新 • • 發佈:2019-01-24
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Drawing; using System.Drawing.Drawing2D; using System.Threading; namespace SunEarthMoon { class Space { private Graphics graphics; private Start sun; private Start earth; private Start moon; private Point screenCenter; private bool isMoving = false; private double d_angle = 2 * 3.14 * 1 / 360; private double angle ; public Space(Graphics graphics, Point screenCenter) { this.graphics = graphics; this.screenCenter = screenCenter; this.sun = new Sun(screenCenter,screenCenter,50,50,graphics,Color.Yellow); this.earth = new Earth(new Point(screenCenter.X + 200, screenCenter.Y), screenCenter, 25, 200, graphics, Color.Blue); this.moon = new Moon(new Point(earth.center.X+50,earth.center.Y),earth.center,15,50,graphics,Color.White); this.angle = d_angle; } public void draw(bool isMoving) { this.IsMoving = isMoving; ThreadStart threadStart = new ThreadStart(threadDraw); Thread thread = new Thread(threadStart); thread.Start(); } public void drawBg() { graphics.Clear(Color.Black); } private void threadDraw() { int dx_e = 200; int dx_m = 50; while (true) { sun.draw(); earth.draw(); moon.draw(); earth.center.X = screenCenter.X + (int)(dx_e * Math.Cos(angle)); earth.center.Y = screenCenter.Y + (int)(dx_e * Math.Sin(angle)); moon.center.X = earth.center.X + (int)(dx_m * Math.Cos(-angle * 12)); moon.center.Y = earth.center.Y + (int)(dx_m * Math.Sin(-angle * 12)); moon.movingCenter = earth.center; angle += d_angle; Thread.Sleep(400); if (!IsMoving) break; drawBg(); } } public bool IsMoving { get { return isMoving; } set { isMoving = value; } } public double D_angle { get { return d_angle; } set { d_angle = value; } } } }