日期:2014-05-18  浏览次数:21061 次

GDI+(C#)绘图,实现地球绕太阳旋转,月亮绕地球旋转
C# code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SunEarthMoon
{
    public partial class Form1 : Form
    {
        private Graphics g;
        private const int sunr = 100;  //太阳的半径
        private const int earthr = 60; //地球的半径
        private const int moonr = 40; // 月亮的半径
        private const int dissunearth = 260;  // 太阳到地球的距离
        private const int disearthmoon = 150; //地球到月球的距离
        double earthradian =360d/365d*Math.PI/180;  // 地球一天旋转角度
        double  moonradian = 360d / 30d * Math.PI / 180;  // 月球一天旋转角度
        Point sunp = new Point(0, 0);  //设置太阳的位置在原点
        Point earthp, moonp;  //地球,月球的位置
        int earthCount = 1;  //地球旋转的天数
        int moonCount = 1;  //月球旋转的天数
 

        public Form1()
        {
            InitializeComponent();
        }

        private void InitGraphics()
        {
            g = this.CreateGraphics();
            g.Clear(Color.White);
            drawSence();
        }
        
        /// <summary>
        /// 点的平移变换
        /// </summary>
        /// <param name="s">要平移的点</param>
        /// <param name="deltax">要平移的X值</param>
        /// <param name="deltay">要平移的Y值</param>
        /// <returns>平移后的点</returns>
        private  Point translatePoint(Point s, int deltax, int deltay)
        {
            s.X += deltax;
            s.Y += deltay;
            return s;
        }

        private  Point rotatePoint(Point s,double  radian)
        {
            s.X =(int)( s.X * Math.Cos(radian) - s.Y * Math.Sin(radian));
            s.Y = (int)(s.X * Math.Sin(radian) + s.Y * Math.Cos(radian));
            return s;
        }

        private void  drawCircel(Point s,int r)
        {
            g.DrawEllipse(new Pen(Color.Blue), s.X, s.Y, r, r);
        
        }

        
        private void drawSence()
        {
            
            int x = this.Width;
            int y = this.Height;

            //画坐标的箭头
            AdjustableArrowCap arrow = new AdjustableArrowCap(4, 4, true);
            Pen myPen = new Pen(Color.Black, 1);
            myPen.CustomEndCap = arrow;
            //确定坐标原点
            g.TranslateTransform(x / 2, y / 2);
            // 画X坐标轴
            g.DrawLine(myPen, -x / 2, 0, x / 2, 0);
            //画Y坐标轴
            g.DrawLine(myPen, 0, y / 2, 0, -y / 2);

            earthp = translatePoint(sunp, dissunearth, 0);
            earthp = rotatePoint(earthp, earthradian * earthCount);
            if (earthCount == 365)
            {
                earthCount = 1;
            }
            else
            {
                earthCount++;
            }
            moonp = rotatePoint(new Point(disearthmoon, 0), moonradian * moonCount);
            moonp = translatePoint(moonp, earthp.X, earthp.Y);
            if (moonCount == 30)
            {
                moonCount = 1;
            }
            else
            {
                moonCount++;
            }
            drawCircel(sunp, sunr);
            drawCircel(earthp, earthr);
            drawCircel(moonp, moonr);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Refresh();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            InitGraphics();
        }
    }
}



运行后地球和月亮的运行轨迹都是倒立的“8”,并不是地球绕太阳,月球绕地球旋转。
估计应该是求旋转后的点的位置出错,但是不知道怎么改,望指教!!

------解决方案--------------------
圆的公式怎么这个样子的?
几个地方不对,改了下,正常了
C# code

public partial class Form3 : Form
    {
        private Graphics g;
        privat