亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C++使用easyx畫實時走動的鐘表

 更新時間:2022年05月16日 12:27:13   作者:kkkgoing  
這篇文章主要為大家詳細介紹了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)

    這篇文章主要為大家詳細介紹了C語言使用單鏈表實現(xiàn)學生信息管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • C++學習之虛函數(shù)表與多態(tài)詳解

    C++學習之虛函數(shù)表與多態(tài)詳解

    這篇文章主要為大家詳細介紹了C++中虛函數(shù)表與多態(tài)的相關知識,文中的示例代碼講解詳細,對我們學習C++有一定的幫助,感興趣的小伙伴可以了解一下
    2023-03-03
  • C++使用遞歸方法求n階勒讓德多項式完整實例

    C++使用遞歸方法求n階勒讓德多項式完整實例

    這篇文章主要介紹了C++使用遞歸方法求n階勒讓德多項式,涉及C++遞歸算法與浮點數(shù)運算的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2016-05-05
  • C++數(shù)據(jù)結構之二叉搜索樹的實現(xiàn)詳解

    C++數(shù)據(jù)結構之二叉搜索樹的實現(xiàn)詳解

    二叉搜索樹作為一個經(jīng)典的數(shù)據(jù)結構,具有鏈表的快速插入與刪除的特點,同時查詢效率也很優(yōu)秀,所以應用十分廣泛。本文將詳細講講二叉搜索樹的C++實現(xiàn),需要的可以參考一下
    2022-08-08
  • C/C++實現(xiàn)動態(tài)庫動態(tài)加載

    C/C++實現(xiàn)動態(tài)庫動態(tài)加載

    在很多項目中,我們多少會用到第三方動態(tài)庫,這些動態(tài)庫一般都是相對固定,使用也很簡單,下面我們就來看看c/c++中如何實現(xiàn)動態(tài)庫動態(tài)加載吧
    2024-01-01
  • C語言棧與隊列相互實現(xiàn)詳解

    C語言棧與隊列相互實現(xiàn)詳解

    棧和隊列,嚴格意義上來說,也屬于線性表,因為它們也都用于存儲邏輯關系為 "一對一" 的數(shù)據(jù),但由于它們比較特殊,本章講解分別用隊列實現(xiàn)棧與用棧實現(xiàn)隊列
    2022-04-04
  • C語言實現(xiàn)父進程主動終止子進程的方法總結

    C語言實現(xiàn)父進程主動終止子進程的方法總結

    一般的情況,子進程自己運行完后,執(zhí)行exit 或者return 后,父進程wait.  waitpid收回子進程,但子進程是一個循環(huán)等待狀態(tài)不主動退出,父進程可以采用文中介紹的幾種方法,需要的朋友可以參考下
    2023-10-10
  • string居然也可以用<<和>>

    string居然也可以用<<和>>

    今天小編就為大家分享一篇關于string居然也可以用<<和>>,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-04-04
  • 基于C語言實現(xiàn)關機小游戲的示例代碼

    基于C語言實現(xiàn)關機小游戲的示例代碼

    關機會寫吧!猜數(shù)字會寫吧!本文將結合這兩個功能,用C語言編寫一個關機惡搞小游戲(最好的朋友轉瞬即逝),只要猜對了,1分鐘后執(zhí)行關機,除非輸入“我是豬”,但是輸完后,1分鐘后還是會執(zhí)行關機,該保存保存,感興趣的可以嘗試一下
    2022-07-07
  • C/C++中多態(tài)性詳解及其作用介紹

    C/C++中多態(tài)性詳解及其作用介紹

    這篇文章主要介紹了C/C++中多態(tài)性(polymorphism)詳解及其作用介紹,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-09-09

最新評論