-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathClock.cpp
244 lines (188 loc) · 6.39 KB
/
Clock.cpp
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include "StdAfx.h"
#include "Clock.h"
#include "Bitmap.h"
#include "Globals.h"
#include "ColorFunctions.h"
COLORREF clrHourNumber,clrMinuteTicks,clrWeek,clrDate,clrSecondHand,clrMinuteHand,clrHourHand,clrCalendarBackGnd,clrCalendarDate;
POINT GetCoordinateForAngle(int dAngle,int iRadius,bool ZeroDegAtTwelve)
{
POINT pCoOrdinates;double radAngle, dRadius;
dRadius=iRadius*1.0;
//Correct input angle within 0-360
if(dAngle<0) dAngle=360+dAngle;
if(dAngle>360) dAngle=dAngle%360;
//adjust angle for zero degree at 12'o Clock
//by default it will be at 3'o Clock which is geometrical x axis
if(ZeroDegAtTwelve)
dAngle=(dAngle+270)%360;
//Radian=Angle*PI/180
radAngle = dAngle*3.14159265358979323846 /180;
//Polor Distance
//X=Cos(x)*r, Y=Sin(x)*r
pCoOrdinates.x = long(cos(radAngle)*dRadius);
pCoOrdinates.y = long(sin(radAngle)*dRadius);
//Make these points relative to centre of the circle
pCoOrdinates.x = pCoOrdinates.x+CENTRE.x;
pCoOrdinates.y = pCoOrdinates.y+CENTRE.y;
return pCoOrdinates;
}
void DrawClockFace(HDC hDC)
{
POINT Pixel;
int iNumber,iAngle;
//SetPixel(hDC,CENTRE.x,CENTRE.y,RGB(200,0,0));
Ellipse(hDC,CENTRE.x-2,CENTRE.y-2,CENTRE.x+2,CENTRE.y+2);
//Draw the No
for (iAngle=0,iNumber=0; iAngle<360; iAngle+=6)
{
if (iAngle%30==0)
{
Pixel=GetCoordinateForAngle(iAngle,int(RADIUS*0.92),true);
DrawText(hDC,sHourNumbers[iNumber],Pixel,clrHourNumber);
iNumber=iNumber++;
}
else
{
Pixel=GetCoordinateForAngle(iAngle,int(RADIUS*0.95),true);
SetPixel(hDC,Pixel.x,Pixel.y,clrMinuteTicks);
}
}
}
void DrawClockHands(HDC hDC,bool bShowSeconds,bool bSoftMove)
{
SYSTEMTIME SysTime;
POINT CoOrd;
int iSecond, iMinute, iHour;
char sDay[10];
//Draw the Clock Circle
POINT pPixel;
pPixel.x =13;
pPixel.y =13;
pPixel=CENTRE;
GetLocalTime (&SysTime);
//Draw Day Info
CoOrd= GetCoordinateForAngle(0,int(RADIUS*0.5));
wsprintf(sDay,"%d/%d",SysTime.wMonth, SysTime.wDay);
DrawText(hDC,sDay,CoOrd,clrDate,0.85);
CoOrd= GetCoordinateForAngle(180,int(RADIUS*0.5));
DrawText(hDC,sWeekNames[SysTime.wDayOfWeek],CoOrd,clrWeek,0.9);
//Draw second hand
//each second moves 360/60=6 degrees
//if soft move is enabled then seconds hand will move by one degree for each 167 milli sec
if (!bSoftMove)
iSecond=SysTime.wSecond*6;
else
iSecond=SysTime.wSecond*6+int(SysTime.wMilliseconds/167);
//Draw Minute hand
//each minute moves 360/60=6 degree + second/60 to show proper proration
iMinute=SysTime.wMinute*6+int(SysTime.wSecond/10.0);
//Draw Hour hand
//each minute moves 360/60=6 degree + Minute/60 to show proper proration
if (SysTime.wHour>=12)
{
//Change the color of 12 to indicate PM
CoOrd=GetCoordinateForAngle(0,int(RADIUS*0.92),true);
DrawText(hDC,sHourNumbers[0],CoOrd,ColorAdjustLuma(clrHourNumber,666));
iHour=(SysTime.wHour-12)*30+int(SysTime.wMinute/2.0);
}
else
iHour=(SysTime.wHour)*30+int(SysTime.wMinute/2.0);
DrawHand(hDC,iHour,int(RADIUS*0.60),3,clrHourHand);
DrawHand(hDC,iMinute,int(RADIUS*0.80),2,clrMinuteHand);
if (bShowSeconds)
DrawHand(hDC,iSecond,int(RADIUS*0.97),3,clrSecondHand);
}
void DrawHand(HDC hDC, int iAngle,int iRadius,int iThickNess,COLORREF clrLineColor)
{
POINT CoOrd;
CoOrd= GetCoordinateForAngle(iAngle,iRadius,true);
DrawAntialiasedLine(hDC,CENTRE,CoOrd,clrLineColor);
return;
}
void DrawAntialiasedLine(HDC hDC,POINT pFrom,POINT pTo,COLORREF clrLineColor)
{
int iDirX,iXd,iYd;
int iX,iY;
WORD iTemp,iSlope, iPixelWeight,iPixelSlope;
COLORREF clrBackGround;
iPixelSlope = 0;
//Make sure the line runs top to bottom
if(pFrom.y > pTo.y )
{
LONG lTemp;
lTemp = pFrom.y; pFrom.y = pTo.y; pTo.y = lTemp;
lTemp = pFrom.x; pFrom.x = pTo.x; pTo.x = lTemp;
}
//Width and Height of the line
iXd = pTo.x-pFrom.x;
iYd = pTo.y-pFrom.y;
//X Direction
if(iXd<0)
{
iDirX=-1;
iXd=-iXd;
}
else
iDirX=1;
//Horizontal, vertical, and diagonal lines do not require Wu antialiasing because
//they pass through the center of every pixel they meet
if (iYd == 0||iXd == 0||iXd == iYd)
{
if (iYd==0)
for(iX=0;--iXd>0;iX+=iDirX)
SetPixel(hDC,pFrom.x+iX,pFrom.y,clrLineColor);
else if (iXd == 0)
for(iY=0;--iYd>0;iY++)
SetPixel(hDC,pFrom.x,pFrom.y+iY,clrLineColor);
else if (iXd == iYd)
for(iX=0,iY=0;--iYd>0;iY++,iX+=iDirX)
SetPixel(hDC,pFrom.x+iX,pFrom.y+iY,clrLineColor);
return;
}
if (iYd > iXd)
{
//It's an Y-major line;
SetPixel(hDC,pFrom.x,pFrom.y, clrLineColor);
iPixelSlope = 0;
iSlope =WORD ((DWORD(iXd) << 16) / DWORD(iYd));
for(iX=0,iY=0;iYd-->0;iY++)
{
iTemp = iPixelSlope;
iPixelSlope += iSlope;
if (iPixelSlope <= iTemp)
iX += iDirX;
iPixelWeight =BYTE(iPixelSlope >> 8);
//clrBackGround = GetWeightedColor(clrLineColor,GetPixel(hDC,pFrom.x+iX,pFrom.y+iY),iPixelWeight);
clrBackGround = GetWeightedColor(clrLineColor,ColorAdjustLuma(clrLineColor,-250),iPixelWeight);
SetPixel(hDC,pFrom.x+iX,pFrom.y+iY,clrBackGround);
//clrBackGround = GetWeightedColor(clrLineColor,GetPixel(hDC, pFrom.x+(iX+iDirX),pFrom.y+iY),iPixelWeight^65535);
clrBackGround = GetWeightedColor(clrLineColor,ColorAdjustLuma(clrLineColor,-250),iPixelWeight^65535);
SetPixel(hDC, pFrom.x+(iX+iDirX),pFrom.y+iY,clrBackGround);
}
SetPixel(hDC, pTo.x ,pTo.y, clrLineColor );
return;
}
else
{
//It's an X-major line;
SetPixel(hDC, pFrom.x ,pFrom.y, clrLineColor );
iPixelSlope = 0;
iSlope =WORD((DWORD(iYd) << 16) / DWORD(iXd));
for(iX=0,iY=0;iXd-->0;iX+=iDirX)
{
iTemp = iPixelSlope;
iPixelSlope += iSlope;
if (iPixelSlope <= iTemp)
iY++;
iPixelWeight = iPixelSlope>>8;
//clrBackGround =GetWeightedColor(clrLineColor,GetPixel(hDC,pFrom.x+iX,pFrom.y+iY),iPixelWeight) ;
clrBackGround =GetWeightedColor(clrLineColor,ColorAdjustLuma(clrLineColor,-250),iPixelWeight) ;
SetPixel(hDC,pFrom.x+iX,pFrom.y+iY,clrBackGround);
//clrBackGround = GetWeightedColor(clrLineColor,GetPixel(hDC, pFrom.x+iX,pFrom.y+iY+1),iPixelWeight^65535);
clrBackGround = GetWeightedColor(clrLineColor,ColorAdjustLuma(clrLineColor,-250),iPixelWeight^65535);
SetPixel(hDC, pFrom.x+iX,pFrom.y+iY+1,clrBackGround);
}
SetPixel(hDC, pTo.x ,pTo.y, clrLineColor );
return;
}
}