这次的任务是用c++画出实时走动的钟表,并且与当前系统的时间一致。
由于我们使用的是c++语言,我们更需要用这个例子来提高我们对面向对象程序设计的理解。
我们首先需要分析出需求,“画一个能够实时走动的钟表”,根据需求我们可以好处两个对象,钟表对象与画图对象,所以我们大致先建立两个类,Clock类与Paint类。
Clock类中的成员变量都有:表的中心坐标x与y、表的时间(时、分、秒)、表的大小r(即半径)、表盘的颜色color。
Clock类中无其他函数,只有用于初始化的构造函数。
Paint类中无任何成员变量,只有三个函数:画表盘函数drawClock_bk、画表盘刻度函数drawClock_scale、画表针函数drawClock_sharp
其中画表盘是非常简单的,最最困难的就是画刻度函数与画表针函数。
要想要画出刻度与表针,就必须知道如何得画刻度的两个坐标。
下面先来了解下如何求得坐标(纯数学知识)
如图:
如果要求圆上一点a的坐标(x,y),利用三角函数,若a点与圆心o(x0,y0)连线与x轴的夹角大小为c,r为半径,则a的横坐标x值为x0+cos(c)*r,a的纵坐标y为y0-sin(c)*r,这样就可求得圆上任意一点的坐标。然后我们需要画出刻度,即我们还需要圆心o与圆上一点a的连线上的另一个坐标,这样才可以画出刻度。如图:
如图点b是点a与圆心o连线上的一点。假设我们需要画的刻度长度是s,所以a与b连线的距离为s,b与圆心连线的距离为r-s,所以根据三角函数也可以求得点b的坐标为x:x0+cos(c)*(r-s),y为:y0-sin(c)*(r-s)。
这下有a、b这两点的坐标就可以画出一个刻度了,然后根据表盘的实际分布可以将所有的刻度画出来了(即每个刻度为5度)。
表针的画法与刻度类似:需要找这个b这种点(圆心与圆上的点连线上的点),然后根据你指定的针长和夹角,就可以求出b点的坐标。然后用b点坐标和圆心坐标就可以画出对应的指针了。
最重要的坐标求法就是这样了,剩下具体的细节请看下面代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
#include <iostream> #include <cstdio> #include <iomanip> #include <graphics.h> #include <conio.h> #include <time.h> #include <cstdlib> #include <cmath> #define PI 3.1415 using namespace std; class Clock { public : int _x; int _y; int _hour; int _minute; int _second; int _r; COLORREF _bk_col; public : Clock( int x, int y, int h, int m, int s, int r, COLORREF bk_color) { this ->_x = x; this ->_y = y; this ->_hour = h; this ->_minute = m; this ->_second = s; this ->_r = r; this ->_bk_col = bk_color; } }; class Paint { public : void drawclock_bk(Clock c); void drawclock_scale(Clock c); void drawclock_sharp(Clock c); }; void Paint::drawclock_bk(Clock c) { setcolor(RGB(0,0,0)); setfillcolor(RGB(0,0,0)); fillcircle(c._x,c._y,c._r); } void Paint::drawclock_scale(Clock c) { int x1,y1; int x2, y2; setlinecolor(RGB(255, 255, 255)); for ( int a = 1; a <= 60;a++) { if (a <= 15) { x1 = static_cast < int >(c._x + ( cos (a*(PI / 30)))*c._r); y1= static_cast < int >(c._y - ( sin (a*(PI / 30)))*c._r); if (a % 5 == 0) { x2 = static_cast < int >(c._x + ( cos (a*(PI / 30)))*(c._r - 15)); y2 = static_cast < int >(c._y - ( sin (a*(PI / 30)))*(c._r - 15)); } else { x2 = static_cast < int >(c._x + ( cos (a*(PI / 30)))*(c._r - 5)); y2 = static_cast < int >(c._y - ( sin (a*(PI / 30)))*(c._r - 5)); } } else if (a > 15 && a <= 30) { x1 = static_cast < int >(c._x + ( cos (a*(PI / 30)))*c._r); y1 = static_cast < int >(c._y - ( sin (a*(PI / 30)))*c._r); if (a % 5 == 0) { x2 = static_cast < int >(c._x + ( cos (a*(PI / 30)))*(c._r - 15)); y2 = static_cast < int >(c._y - ( sin (a*(PI / 30)))*(c._r - 15)); } else { x2 = static_cast < int >(c._x + ( cos (a*(PI / 30)))*(c._r - 5)); y2 = static_cast < int >(c._y - ( sin (a*(PI / 30)))*(c._r - 5)); } } else if (a > 30 && a <= 45) { x1 = static_cast < int >(c._x + ( cos (a*(PI / 30)))*c._r); y1 = static_cast < int >(c._y - ( sin (a*(PI / 30)))*c._r); if (a % 5 == 0) { x2 = static_cast < int >(c._x + ( cos (a*(PI / 30)))*(c._r - 15)); y2 = static_cast < int >(c._y - ( sin (a*(PI / 30)))*(c._r - 15)); } else { x2 = static_cast < int >(c._x + ( cos (a*(PI / 30)))*(c._r - 5)); y2 = static_cast < int >(c._y - ( sin (a*(PI / 30)))*(c._r - 5)); } } else if (a > 45 && a <= 60) { x1 = static_cast < int >(c._x + ( cos (a*(PI / 30)))*c._r); y1 = static_cast < int >(c._y - ( sin (a*(PI / 30)))*c._r); if (a % 5 == 0) { x2 = static_cast < int >(c._x + ( cos (a*(PI / 30)))*(c._r - 15)); y2 = static_cast < int >(c._y - ( sin (a*(PI / 30)))*(c._r - 15)); } else { x2 = static_cast < int >(c._x + ( cos (a*(PI / 30)))*(c._r - 5)); y2 = static_cast < int >(c._y - ( sin (a*(PI / 30)))*(c._r - 5)); } } line(x1, y1,x2, y2); } setfillcolor(RGB(255,255,255)); fillcircle(c._x,c._y,5); } void Paint::drawclock_sharp(Clock c) { int x1, y1; int x2, y2; int x3, y3; setlinecolor(RGB(255,255,255)); x3 = static_cast < int >(c._x + ( cos ( static_cast < double >(15 - c._second)*(PI / 30)))* static_cast < double >(0.8*c._r)); x2 = static_cast < int >(c._x + ( cos ( static_cast < double >(15 - c._minute - static_cast < double >(c._second) / 60)*(PI / 30)))* static_cast < double >(0.6*c._r)); x1 = static_cast < int >(c._x + ( cos ( static_cast < double >(3 - c._hour - static_cast < double >(c._minute) / 60)*(PI / 6)))* static_cast < double >(0.4*c._r)); y3 = static_cast < int >(c._y - ( sin ( static_cast < double >(15 - c._second)*(PI / 30)))* static_cast < double >(0.8*c._r)); y2 = static_cast < int >(c._y - ( sin ( static_cast < double >(15 - c._minute - static_cast < double >(c._second) / 60)*(PI / 30)))* static_cast < double >(0.6*c._r)); y1 = static_cast < int >(c._y - ( sin ( static_cast < double >(3 - c._hour - static_cast < double >(c._minute) / 60)*(PI / 6)))* static_cast < double >(0.4*c._r)); line(c._x, c._y, x1, y1); line(c._x, c._y, x2, y2); line(c._x, c._y, x3, y3); } int main() { initgraph(1024,576); setbkcolor(RGB(255, 255, 255)); cleardevice(); time_t nowtime; struct tm * ptime; if ( time (&nowtime)) { ptime = localtime (&nowtime); } Clock c(512, 288,ptime->tm_hour, ptime->tm_min, ptime->tm_sec, 120, RGB(255, 255, 255)); Paint p1; p1.drawclock_bk(c); p1.drawclock_scale(c); p1.drawclock_sharp(c); int flag=0; while ( true ) { Sleep(1000); ++c._second; c._second%=60; if (c._second== 0) { c._minute++; } c._minute %= 60; if (c._minute==1) { flag=0; if (c._minute == 0&&flag==0) { c._hour++; flag=1; } c._hour %= 24; p1.drawclock_bk(c); p1.drawclock_scale(c); p1.drawclock_sharp(c); } _getch(); closegraph(); return 0; } |
vs2013运行效果如图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/weixin_43977523/article/details/88849635