服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - C++使用easyx画实时走动的钟表

C++使用easyx画实时走动的钟表

2022-12-02 15:46kkkgoing C/C++

这篇文章主要为大家详细介绍了C++使用easyx画实时走动的钟表,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

这次的任务是用c++画出实时走动的钟表,并且与当前系统的时间一致。

由于我们使用的是c++语言,我们更需要用这个例子来提高我们对面向对象程序设计的理解。

我们首先需要分析出需求,“画一个能够实时走动的钟表”,根据需求我们可以好处两个对象,钟表对象与画图对象,所以我们大致先建立两个类,Clock类与Paint类。

Clock类中的成员变量都有:表的中心坐标x与y、表的时间(时、分、秒)、表的大小r(即半径)、表盘的颜色color。

Clock类中无其他函数,只有用于初始化的构造函数。

Paint类中无任何成员变量,只有三个函数:画表盘函数drawClock_bk、画表盘刻度函数drawClock_scale、画表针函数drawClock_sharp

其中画表盘是非常简单的,最最困难的就是画刻度函数与画表针函数。

要想要画出刻度与表针,就必须知道如何得画刻度的两个坐标。

下面先来了解下如何求得坐标(纯数学知识)

如图:

C++使用easyx画实时走动的钟表

如果要求圆上一点a的坐标(x,y),利用三角函数,若a点与圆心o(x0,y0)连线与x轴的夹角大小为c,r为半径,则a的横坐标x值为x0+cos(c)*r,a的纵坐标y为y0-sin(c)*r,这样就可求得圆上任意一点的坐标。然后我们需要画出刻度,即我们还需要圆心o与圆上一点a的连线上的另一个坐标,这样才可以画出刻度。如图:

C++使用easyx画实时走动的钟表

如图点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运行效果如图:

C++使用easyx画实时走动的钟表

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/weixin_43977523/article/details/88849635

延伸 · 阅读

精彩推荐
  • C/C++C语言 详解字符串基础

    C语言 详解字符串基础

    在 C 语言中,字符串实际上是使用空字符 \0 结尾的一维字符数组。因此,\0 是用于标记字符串的结束。空字符(Null character)又称结束符,缩写 NUL,是一个...

    清风自在 流水潺潺3662022-11-10
  • C/C++C语言实现电话簿项目

    C语言实现电话簿项目

    这篇文章主要为大家详细介绍了C语言实现电话簿项目,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    MisakiFx8922021-08-09
  • C/C++Qt编写地图实现闪烁点图的示例代码

    Qt编写地图实现闪烁点图的示例代码

    闪烁点图的核心有三个要素,城市的名称、城市的经纬度、对应值的大小,当值越大闪烁点也就越大,本文就来实现一下地图闪烁点图,具有一定的参考价...

    feiyangqingyun10242022-07-20
  • C/C++Qt编写地图综合应用之绘制覆盖物折线

    Qt编写地图综合应用之绘制覆盖物折线

    折线图目前应用最广的也是用来绘制各种轨迹,折线图其实就是后面动态轨迹图、飞机航线图的前身,公用的一个方法addPolyline。本文将教大家如何通过Q...

    feiyangqingyun11732022-07-22
  • C/C++使用Objective-C获取IPHONE手机IMSI序列号

    使用Objective-C获取IPHONE手机IMSI序列号

    这篇文章主要介绍了使用Objective-C获取IPHONE手机IMSI序列号的方法以及通过IMSI序列号获取运营商、手机号的方法,非常的实用,有需要的小伙伴可以参考下。...

    C语言教程网5112021-02-24
  • C/C++详解C语言中strpbrk()函数的用法

    详解C语言中strpbrk()函数的用法

    这篇文章主要介绍了详解C语言中strpbrk()函数的用法,是C语言入门学习中的基础知识,需要的朋友可以参考下...

    C语言教程网11572021-03-08
  • C/C++详解C++编程中的嵌套类的声明与其中的函数使用

    详解C++编程中的嵌套类的声明与其中的函数使用

    这篇文章主要介绍了C++编程中的嵌套类的声明与其中的函数使用,嵌套类即在一个类的范围内声明和编写另一个类,需要的朋友可以参考下...

    C++教程网9522021-03-23
  • C/C++C++ string替换指定字符实例代码

    C++ string替换指定字符实例代码

    这篇文章主要给大家介绍了关于C++ string替换指定字符的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用C++具有一定的参考学习价值,需...

    哀汐9392021-08-05