C++使用easyx畫(huà)實(shí)時(shí)走動(dòng)的鐘表
這次的任務(wù)是用c++畫(huà)出實(shí)時(shí)走動(dòng)的鐘表,并且與當(dāng)前系統(tǒng)的時(shí)間一致。
由于我們使用的是c++語(yǔ)言,我們更需要用這個(gè)例子來(lái)提高我們對(duì)面向?qū)ο蟪绦蛟O(shè)計(jì)的理解。
我們首先需要分析出需求,“畫(huà)一個(gè)能夠?qū)崟r(shí)走動(dòng)的鐘表”,根據(jù)需求我們可以好處兩個(gè)對(duì)象,鐘表對(duì)象與畫(huà)圖對(duì)象,所以我們大致先建立兩個(gè)類(lèi),Clock類(lèi)與Paint類(lèi)。
Clock類(lèi)中的成員變量都有:表的中心坐標(biāo)x與y、表的時(shí)間(時(shí)、分、秒)、表的大小r(即半徑)、表盤(pán)的顏色color。
Clock類(lèi)中無(wú)其他函數(shù),只有用于初始化的構(gòu)造函數(shù)。
Paint類(lèi)中無(wú)任何成員變量,只有三個(gè)函數(shù):畫(huà)表盤(pán)函數(shù)drawClock_bk、畫(huà)表盤(pán)刻度函數(shù)drawClock_scale、畫(huà)表針函數(shù)drawClock_sharp
其中畫(huà)表盤(pán)是非常簡(jiǎn)單的,最最困難的就是畫(huà)刻度函數(shù)與畫(huà)表針函數(shù)。
要想要畫(huà)出刻度與表針,就必須知道如何得畫(huà)刻度的兩個(gè)坐標(biāo)。
下面先來(lái)了解下如何求得坐標(biāo)(純數(shù)學(xué)知識(shí))
如圖:

如果要求圓上一點(diǎn)a的坐標(biāo)(x,y),利用三角函數(shù),若a點(diǎn)與圓心o(x0,y0)連線(xiàn)與x軸的夾角大小為c,r為半徑,則a的橫坐標(biāo)x值為x0+cos(c)*r,a的縱坐標(biāo)y為y0-sin(c)*r,這樣就可求得圓上任意一點(diǎn)的坐標(biāo)。然后我們需要畫(huà)出刻度,即我們還需要圓心o與圓上一點(diǎn)a的連線(xiàn)上的另一個(gè)坐標(biāo),這樣才可以畫(huà)出刻度。如圖:

如圖點(diǎn)b是點(diǎn)a與圓心o連線(xiàn)上的一點(diǎn)。假設(shè)我們需要畫(huà)的刻度長(zhǎng)度是s,所以a與b連線(xiàn)的距離為s,b與圓心連線(xiàn)的距離為r-s,所以根據(jù)三角函數(shù)也可以求得點(diǎn)b的坐標(biāo)為x:x0+cos(c)*(r-s),y為:y0-sin(c)*(r-s)。
這下有a、b這兩點(diǎn)的坐標(biāo)就可以畫(huà)出一個(gè)刻度了,然后根據(jù)表盤(pán)的實(shí)際分布可以將所有的刻度畫(huà)出來(lái)了(即每個(gè)刻度為5度)。
表針的畫(huà)法與刻度類(lèi)似:需要找這個(gè)b這種點(diǎn)(圓心與圓上的點(diǎn)連線(xiàn)上的點(diǎn)),然后根據(jù)你指定的針長(zhǎng)和夾角,就可以求出b點(diǎn)的坐標(biāo)。然后用b點(diǎn)坐標(biāo)和圓心坐標(biāo)就可以畫(huà)出對(duì)應(yīng)的指針了。
最重要的坐標(biāo)求法就是這樣了,剩下具體的細(xì)節(jié)請(qǐng)看下面代碼:
#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運(yùn)行效果如圖:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C語(yǔ)言實(shí)現(xiàn)JSON解析器的方法步驟
JSON是一種非常流行的數(shù)據(jù)格式,本文主要介紹了C語(yǔ)言實(shí)現(xiàn)JSON解析器的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-08-08
C語(yǔ)言用棧和隊(duì)列實(shí)現(xiàn)的回文檢測(cè)功能示例
這篇文章主要介紹了C語(yǔ)言用棧和隊(duì)列實(shí)現(xiàn)的回文檢測(cè)功能,結(jié)合具體實(shí)例形式分析了C語(yǔ)言棧和隊(duì)列的定義及使用棧和隊(duì)列進(jìn)行回文檢測(cè)的操作技巧,需要的朋友可以參考下2017-06-06
C++中Copy-Swap實(shí)現(xiàn)拷貝交換
本文主要介紹了C++中Copy-Swap實(shí)現(xiàn)拷貝交換,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
C++動(dòng)態(tài)內(nèi)存分配(new/new[]和delete/delete[])詳解
這篇文章主要介紹了C++動(dòng)態(tài)內(nèi)存分配(new/new[]和delete/delete[])詳解的相關(guān)資料,需要的朋友可以參考下2017-05-05
C++優(yōu)先級(jí)隊(duì)列的使用指南與模擬實(shí)現(xiàn)
優(yōu)先級(jí)隊(duì)列是一種特殊的隊(duì)列,其中每個(gè)元素都有一個(gè)與之關(guān)聯(lián)的優(yōu)先級(jí),優(yōu)先級(jí)較高的元素會(huì)在隊(duì)列中較早地被處理,而優(yōu)先級(jí)較低的元素會(huì)在后續(xù)處理,本文給大家介紹C++優(yōu)先級(jí)隊(duì)列的使用指南與模擬實(shí)現(xiàn),需要的朋友可以參考下2023-09-09
C語(yǔ)言 map函數(shù)的基礎(chǔ)用法詳解
這篇文章主要為大家介紹了C語(yǔ)言 map函數(shù)的基礎(chǔ)用法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-01-01
C++強(qiáng)制類(lèi)型轉(zhuǎn)換的四種方式
本文主要介紹了C++強(qiáng)制類(lèi)型轉(zhuǎn)換的四種方式,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05

