Chủ đề: Vẽ đồng hồ
treo tường bằng C#
I.
Ý tưởng.
- Dùng hàm DrawEclipe để vẽ mặt đồng hồ.
- Dùng hàm FillPolygon để vẽ kim đồng hồ.
II.
Vẽ đồng hồ.
- Vẽ kim dây.
- Chọn điểm O là trung tâm của đồng hồ. Cần xác định thêm 3 điểm là A, P1, P2 để vẽ kim đồng hồ.
- Chọn điểm O là trung tâm của đồng hồ. Cần xác định thêm 3 điểm là A, P1, P2 để vẽ kim đồng hồ.
- Xác định điểm A.
- Nhận xét: Kim dây cứ mỗi giây sẽ nhích 1 cái. Khi kim dây nhích đủ 60 cái thì sẽ quay được 1 vòng và kim dây sẽ trở lại vị trí ban đầu. 60s kim nhích được 360°, sẽ có 1s kim sẽ nhích được 6°. Góc lệch của A so với trục Oy tại thời điểm t là:Ĉ = (số giây *6) *2Π/360°. Tọa độ điểm A là xA = xO + bán kính * sinĈ; yA = yO + bán kính * cosĈ.
- Xác định điểm P1, P2 , OP1, OP2, sẽ có 1 góc lệch so với OA là 1°. Hàm tính P1, P2 tương tự như A.
- Xác định B. Góc của OB sẽ lệch so với OA 1 góc 180°. Hàm tính B tương tự như A.
- Vẽ kim phút, kim giờ.
Tương tự vẽ kim dây.
III.
Souch Code.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace drawclock
{
public partial class Form1 : Form
{
Timer t = new Timer();
int Width = 300, Height =
300, slength = 140, mlength = 110, hlength = 80, aniseclength = 100,
anisecbehindlength = 40;
int cx, cy;
Bitmap bmp;
Graphics g;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object
sender, EventArgs e)
{
bmp = new Bitmap(Width++,
Height++);
cx = Width / 2;
cy = Height / 2;
this.BackColor = Color.White;
t.Interval = 1000;
t.Tick += new EventHandler(this.t_Tick);
t.Start();
}
private void t_Tick(object
sender, EventArgs e)
{
g = Graphics.FromImage(bmp);
int ss = DateTime.Now.Second;
int mm = DateTime.Now.Minute;
int hh = DateTime.Now.Hour;
int[] handCoord = new int[2];
int[] aniCoord1 = new int[2];
int[] aniCoord2 = new int[2];
int[] aniCoord3 = new int[2];
g.Clear(Color.White);
// Vẽ đồng hồ
g.DrawEllipse(new Pen(Color.Black),
0, 0, Width, Height);
g.FillEllipse(new SolidBrush(Color.Black),
145, 145, 10, 10);
g.DrawString("12",
new Font("Area", 12), Brushes.Black, new PointF(140, 0));
g.DrawString("1", new Font("Area",
12), Brushes.Black, new PointF(215, 22));
g.DrawString("2",
new Font("Area", 12), Brushes.Black, new PointF(270, 75));
g.DrawString("3",
new Font("Area", 12), Brushes.Black, new PointF(286, 140));
g.DrawString("4",
new Font("Area", 12), Brushes.Black, new PointF(260, 227));
g.DrawString("5",
new Font("Area", 12), Brushes.Black, new PointF(205, 270));
g.DrawString("6",
new Font("Area", 12), Brushes.Black, new PointF(142, 282));
g.DrawString("7",
new Font("Area", 12), Brushes.Black, new PointF(75, 267));
g.DrawString("8",
new Font("Area", 12), Brushes.Black, new PointF(25, 227));
g.DrawString("9",
new Font("Area", 12), Brushes.Black, new PointF(0, 140));
g.DrawString("10",
new Font("Area", 12), Brushes.Black, new PointF(13, 75));
g.DrawString("11",
new Font("Area", 12), Brushes.Black, new PointF(65, 22));
Point O = new Point(cx,
cy);
// ve kim day
handCoord =
msCoord(ss, slength);
Point A = new Point(handCoord[0],
handCoord[1]);
aniCoord1 =
anisecCoord1(ss, aniseclength);
Point X1 = new Point(aniCoord1[0],
aniCoord1[1]);
aniCoord2 =
anisecCoord2(ss, aniseclength);
Point X2 = new Point(aniCoord2[0],
aniCoord2[1]);
Pen p = new Pen(Color.Black,
1f);
Point[] SS = { O, X1,
A, X2 };
g.FillPolygon(new SolidBrush(Color.Green),
SS);
aniCoord3 =
anisecCoord3(ss, anisecbehindlength);
Point C = new Point(aniCoord3[0],
aniCoord3[1]);
g.DrawLine(new Pen(Color.Green,
3f), O, C);
//ve kim phut
handCoord =
msCoord(mm, mlength);
Point B = new Point(handCoord[0],
handCoord[1]);
aniCoord1 =
anisecCoord11(mm, aniseclength);
Point Y1 = new Point(aniCoord1[0],
aniCoord1[1]);
aniCoord2 = anisecCoord22(mm,
aniseclength);
Point Y2 = new Point(aniCoord2[0],
aniCoord2[1]);
Pen q = new Pen(Color.Black,
1f);
Point[] MM = { O, Y1, B, Y2 };
aniCoord3
= anisecCoord33(mm, 20);
Point D = new Point(aniCoord3[0],
aniCoord3[1]);
g.DrawLine(new Pen(Color.Black,
3f), O, D);
g.FillPolygon(new SolidBrush(Color.Black),
MM);
//ve kim gio
handCoord = hrCoord(hh
% 12 , mm, 80);
Point E = new Point(handCoord[0],
handCoord[1]);
g.DrawLine(new Pen(Color.Red,
5f), O, E);
handCoord =
hrCoord1(hh % 12 , mm, 20);
Point F = new Point(handCoord[0],
handCoord[1]);
g.DrawLine(new Pen(Color.Red,
3f), O, F);
pictureBox1.Image =
bmp;
this.Text = "Bay
gio la - " + hh + " : " + mm + "
: " + ss + "";
g.Dispose();
}
//ham tinh kim day
private int[] msCoord(int
sg, int cdkd) //sg la so giay, cdkd la chieu dai kim day
{
int[] coord = new int[2];
sg *= 6;
if (sg >= 0
&& sg <= 180)
{
coord[0] = cx + (int)(cdkd
* Math.Sin(Math.PI * sg / 180));
coord[1] = cy - (int)(cdkd
* Math.Cos(Math.PI * sg / 180));
}
else
{
coord[0] = cx - (int)(cdkd
* -Math.Sin(Math.PI * sg / 180));
coord[1] = cy - (int)(cdkd
* Math.Cos(Math.PI * sg / 180));
}
return coord;
}
private int[] msCoord1(int
sg, int cdkd) //sg la so giay, cdkd la chieu dai kim day
{
int[] coord = new int[2];
sg *= 1/6;
if (sg >= 0
&& sg <= 180)
{
coord[0] = cx + (int)(cdkd
* Math.Sin(Math.PI * sg / 180));
coord[1] = cy - (int)(cdkd
* Math.Cos(Math.PI * sg / 180));
}
else
{
coord[0] = cx - (int)(cdkd
* -Math.Sin(Math.PI * sg / 180));
coord[1] = cy - (int)(cdkd
* Math.Cos(Math.PI * sg / 180));
}
return coord;
}
private int[]
anisecCoord1(int val, int hlen)
{
int[] coord = new int[2];
val = val * 6 + 1;
coord[0] = cx + (int)(hlen
* Math.Sin(Math.PI * val / 180));
coord[1] = cy - (int)(hlen
* Math.Cos(Math.PI * val / 180));
return coord;
}
private int[]
anisecCoord2(int val, int hlen)
{
int[] coord = new int[2];
val = val * 6 - 1;
coord[0] = cx + (int)(hlen
* Math.Sin(Math.PI * val / 180));
coord[1] = cy - (int)(hlen
* Math.Cos(Math.PI * val / 180));
return coord;
}
private int[]
anisecCoord3(int val, int hlen)
{
int[] coord = new int[2];
val = val * 6 + 180;
coord[0] = cx + (int)(hlen
* Math.Sin(Math.PI * val / 180));
coord[1] = cy - (int)(hlen
* Math.Cos(Math.PI * val / 180));
return coord;
}
private int[]
anisecCoord11(int val, int hlen)
{
int[] coord = new int[2];
val = val * 6 + 2;
coord[0] = cx + (int)(hlen
* Math.Sin(Math.PI * val / 180));
coord[1] = cy - (int)(hlen
* Math.Cos(Math.PI * val / 180));
return coord;
}
private int[]
anisecCoord22(int val, int hlen)
{
int[] coord = new int[2];
val = val * 6 - 2;
coord[0] = cx + (int)(hlen
* Math.Sin(Math.PI * val / 180));
coord[1] = cy - (int)(hlen
* Math.Cos(Math.PI * val / 180));
return coord;
}
private int[]
anisecCoord33(int val, int hlen)
{
int[] coord = new int[2];
val = val * 6 + 180;
coord[0] = cx + (int)(hlen
* Math.Sin(Math.PI * val / 180));
coord[1] = cy - (int)(hlen
* Math.Cos(Math.PI * val / 180));
return coord;
}
private int[] hrCoord(int
hval, int mval, int hlen)
{
int[] coord = new int[2];
int val = (int)((hval
* 30) + (mval * 0.5));
if (val >= 0
&& val <= 180)
{
coord[0] = cx + (int)(hlen
* Math.Sin(Math.PI * val / 180));
coord[1] = cy - (int)(hlen
* Math.Cos(Math.PI * val / 180));
}
else
{
coord[0] = cx - (int)(hlen
* -Math.Sin(Math.PI * val / 180));
coord[1] = cy - (int)(hlen
* Math.Cos(Math.PI * val / 180));
}
return coord;
}
private int[] hrCoord1(int
hval, int mval, int hlen)
{
int[] coord = new int[2];
int val = (int)((hval
* 30) + (mval * 0.5));
if (val >= 0
&& val <= 180)
{
coord[0] = cx - (int)(hlen
* Math.Sin(Math.PI * val / 180));
coord[1] = cy + (int)(hlen
* Math.Cos(Math.PI * val / 180));
}
else
{
coord[0] = cx + (int)(hlen
* -Math.Sin(Math.PI * val / 180));
coord[1] = cy + (int)(hlen
* Math.Cos(Math.PI * val / 180));
}
return coord;
}
private int[]
anisecCoord111(int sg, int sp, int cdkg)
{
int[] coord = new int[2];
int val = (int)((sg *
30) + (sp * 0.5));
val = val * 6 + 5;
coord[0] = cx + (int)(sp
* Math.Sin(Math.PI * val / 180));
coord[1] = cy - (int)(sp
* Math.Cos(Math.PI * val / 180));
return coord;
}
private int[]
anisecCoord222(int sg, int sp, int cdkg)
{
int[] coord = new int[2];
int val = (int)((sg * 30) + (sp * 0.5));
val = val * 6 - 5;
coord[0] = cx + (int)(sp
* Math.Sin(Math.PI * val / 180));
coord[1] = cy - (int)(sp
* Math.Cos(Math.PI * val / 180));
return coord;
}
private int[]
anisecCoord333(int sg, int sp, int cdkg)
{
int[] coord = new int[2];
int val = (int)((sg *
30) + (sp * 0.5));
val = val * 6 + 180;
coord[0] = cx + (int)(sp
* Math.Sin(Math.PI * val / 180));
coord[1] = cy - (int)(sp
* Math.Cos(Math.PI * val / 180));
return coord;
}
}
}
IV.
Hình ảnh minh họa.
Cảm ơn bạn đã theo dõi. Nếu có bất kỳ ý kiến gì xin hãy để lại bình luận bên dưới.
Không có nhận xét nào:
Đăng nhận xét