C++使用easyx畫實時走動的鐘表
這次的任務是用c++畫出實時走動的鐘表,并且與當前系統(tǒng)的時間一致。
由于我們使用的是c++語言,我們更需要用這個例子來提高我們對面向對象程序設計的理解。
我們首先需要分析出需求,“畫一個能夠實時走動的鐘表”,根據(jù)需求我們可以好處兩個對象,鐘表對象與畫圖對象,所以我們大致先建立兩個類,Clock類與Paint類。
Clock類中的成員變量都有:表的中心坐標x與y、表的時間(時、分、秒)、表的大小r(即半徑)、表盤的顏色color。
Clock類中無其他函數(shù),只有用于初始化的構造函數(shù)。
Paint類中無任何成員變量,只有三個函數(shù):畫表盤函數(shù)drawClock_bk、畫表盤刻度函數(shù)drawClock_scale、畫表針函數(shù)drawClock_sharp
其中畫表盤是非常簡單的,最最困難的就是畫刻度函數(shù)與畫表針函數(shù)。
要想要畫出刻度與表針,就必須知道如何得畫刻度的兩個坐標。
下面先來了解下如何求得坐標(純數(shù)學知識)
如圖:
如果要求圓上一點a的坐標(x,y),利用三角函數(shù),若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,所以根據(jù)三角函數(shù)也可以求得點b的坐標為x:x0+cos(c)*(r-s),y為:y0-sin(c)*(r-s)。
這下有a、b這兩點的坐標就可以畫出一個刻度了,然后根據(jù)表盤的實際分布可以將所有的刻度畫出來了(即每個刻度為5度)。
表針的畫法與刻度類似:需要找這個b這種點(圓心與圓上的點連線上的點),然后根據(jù)你指定的針長和夾角,就可以求出b點的坐標。然后用b點坐標和圓心坐標就可以畫出對應的指針了。
最重要的坐標求法就是這樣了,剩下具體的細節(jié)請看下面代碼:
#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語言使用單鏈表實現(xiàn)學生信息管理系統(tǒng)
這篇文章主要為大家詳細介紹了C語言使用單鏈表實現(xiàn)學生信息管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-11-11C++數(shù)據(jù)結構之二叉搜索樹的實現(xiàn)詳解
二叉搜索樹作為一個經(jīng)典的數(shù)據(jù)結構,具有鏈表的快速插入與刪除的特點,同時查詢效率也很優(yōu)秀,所以應用十分廣泛。本文將詳細講講二叉搜索樹的C++實現(xiàn),需要的可以參考一下2022-08-08C/C++實現(xiàn)動態(tài)庫動態(tài)加載
在很多項目中,我們多少會用到第三方動態(tài)庫,這些動態(tài)庫一般都是相對固定,使用也很簡單,下面我們就來看看c/c++中如何實現(xiàn)動態(tài)庫動態(tài)加載吧2024-01-01