日期:2014-05-18 浏览次数:21324 次
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();
}
}
}
public partial class Form3 : Form
{
private Graphics g;
privat