按照书上例子写了个简易的时钟,但有一串代码看不懂,如果角度是(0,90),那岂不是
secPoint.y=centerPoint.Y - 一个正数,不对啊。但是程序运行正常。求助!
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private const int s_pinlen = 150, m_penlen = 100, h_penlen = 50;
private void timer1_Tick(object sender, EventArgs e)
{
int h = DateTime.Now.Hour;
int m = DateTime.Now.Minute;
int s = DateTime.Now.Second;
MyDrawClock(h, m, s);
toolStripStatusLabel1.Text = string.Format("{0}:{1}:{2}", h, m, s);
}
private void MyDrawClock(int h, int m, int s)
{
Graphics g = this.CreateGraphics();
g.Clear(Color.White);
Pen myPen = new Pen(Color.Black, 1);
g.DrawEllipse(myPen, this.ClientRectangle);
Point centerPoint = new Point(this.ClientRectangle.Width / 2, this.ClientRectangle.Height / 2);
Point secPoint = new Point((int)(centerPoint.X + (Math.Sin(s * Math.PI / 30)) * s_pinlen), (int)(centerPoint.Y - (Math.Cos( s * Math.PI / 30)) * s_pinlen));
Point minPoint = new Point((int)(centerPoint.X + (Math.Sin(m * Math.PI / 30)) * m_penlen), (int)(centerPoint.Y - (Math.Cos( m * Math.PI / 30)) * m_penlen));
Point hourPoint = new Point((int)(centerPoint.X + (Math.Sin(h * Math.PI / 6)) * h_penlen), (int)(centerPoint.Y - (Math.Cos(h * Math.PI / 6)) * h_penlen));
g.DrawLine(myPen, centerPoint, secPoint);
g.DrawLine(myPen, centerPoint, minPoint);
g.DrawLine(myPen, centerPoint, hourPoint);
}
}
[此贴子已经被作者于2007-1-1 2:00:46编辑过]