C++使用easyx畫實時走動的鐘表_第1頁
C++使用easyx畫實時走動的鐘表_第2頁
C++使用easyx畫實時走動的鐘表_第3頁
C++使用easyx畫實時走動的鐘表_第4頁
C++使用easyx畫實時走動的鐘表_第5頁
已閱讀5頁,還剩4頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

第C++使用easyx畫實時走動的鐘表這次的任務(wù)是用c++畫出實時走動的鐘表,并且與當前系統(tǒng)的時間一致。

由于我們使用的是c++語言,我們更需要用這個例子來提高我們對面向?qū)ο蟪绦蛟O(shè)計的理解。

我們首先需要分析出需求,畫一個能夠?qū)崟r走動的鐘表,根據(jù)需求我們可以好處兩個對象,鐘表對象與畫圖對象,所以我們大致先建立兩個類,Clock類與Paint類。

Clock類中的成員變量都有:表的中心坐標x與y、表的時間(時、分、秒)、表的大小r(即半徑)、表盤的顏色color。

Clock類中無其他函數(shù),只有用于初始化的構(gòu)造函數(shù)。

Paint類中無任何成員變量,只有三個函數(shù):畫表盤函數(shù)drawClock_bk、畫表盤刻度函數(shù)drawClock_scale、畫表針函數(shù)drawClock_sharp

其中畫表盤是非常簡單的,最最困難的就是畫刻度函數(shù)與畫表針函數(shù)。

要想要畫出刻度與表針,就必須知道如何得畫刻度的兩個坐標。

下面先來了解下如何求得坐標(純數(shù)學(xué)知識)

如圖:

如果要求圓上一點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連線上的一點。假設(shè)我們需要畫的刻度長度是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點坐標和圓心坐標就可以畫出對應(yīng)的指針了。

最重要的坐標求法就是這樣了,剩下具體的細節(jié)請看下面代碼:

#includeiostream

#includecstdio

#includeiomanip

#includegraphics.h

#includeconio.h

#includetime.h

#includecstdlib

#includecmath

#definePI3.1415

usingnamespacestd;

classClock

public:

int_x;

int_y;

int_hour;

int_minute;

int_second;

int_r;

COLORREF_bk_col;

public:

Clock(intx,inty,inth,intm,ints,intr,COLORREFbk_color)

{

this-_x=x;

this-_y=y;

this-_hour=h;

this-_minute=m;

this-_second=s;

this-_r=r;

this-_bk_col=bk_color;

}

classPaint

public:

voiddrawclock_bk(Clockc);

voiddrawclock_scale(Clockc);

voiddrawclock_sharp(Clockc);

voidPaint::drawclock_bk(Clockc)

setcolor(RGB(0,0,0));

setfillcolor(RGB(0,0,0));

fillcircle(c._x,c._y,c._r);

voidPaint::drawclock_scale(Clockc)

intx1,y1;

intx2,y2;

setlinecolor(RGB(255,255,255));

for(inta=1;a=60;a++)

{

if(a=15)

{

x1=static_castint(c._x+(cos(a*(PI/30)))*c._r);

y1=static_castint(c._y-(sin(a*(PI/30)))*c._r);

if(a%5==0)

{

x2=static_castint(c._x+(cos(a*(PI/30)))*(c._r-15));

y2=static_castint(c._y-(sin(a*(PI/30)))*(c._r-15));

}

else

{

x2=static_castint(c._x+(cos(a*(PI/30)))*(c._r-5));

y2=static_castint(c._y-(sin(a*(PI/30)))*(c._r-5));

}

}

elseif(a15a=30)

{

x1=static_castint(c._x+(cos(a*(PI/30)))*c._r);

y1=static_castint(c._y-(sin(a*(PI/30)))*c._r);

if(a%5==0)

{

x2=static_castint(c._x+(cos(a*(PI/30)))*(c._r-15));

y2=static_castint(c._y-(sin(a*(PI/30)))*(c._r-15));

}

else

{

x2=static_castint(c._x+(cos(a*(PI/30)))*(c._r-5));

y2=static_castint(c._y-(sin(a*(PI/30)))*(c._r-5));

}

}

elseif(a30a=45)

{

x1=static_castint(c._x+(cos(a*(PI/30)))*c._r);

y1=static_castint(c._y-(sin(a*(PI/30)))*c._r);

if(a%5==0)

{

x2=static_castint(c._x+(cos(a*(PI/30)))*(c._r-15));

y2=static_castint(c._y-(sin(a*(PI/30)))*(c._r-15));

}

else

{

x2=static_castint(c._x+(cos(a*(PI/30)))*(c._r-5));

y2=static_castint(c._y-(sin(a*(PI/30)))*(c._r-5));

}

}

elseif(a45a=60)

{

x1=static_castint(c._x+(cos(a*(PI/30)))*c._r);

y1=static_castint(c._y-(sin(a*(PI/30)))*c._r);

if(a%5==0)

{

x2=static_castint(c._x+(cos(a*(PI/30)))*(c._r-15));

y2=static_castint(c._y-(sin(a*(PI/30)))*(c._r-15));

}

else

{

x2=static_castint(c._x+(cos(a*(PI/30)))*(c._r-5));

y2=static_castint(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);

voidPaint::drawclock_sharp(Clockc)

{

intx1,y1;

intx2,y2;

intx3,y3;

setlinecolor(RGB(255,255,255));

x3=static_castint(c._x+(cos(static_castdouble(15-c._second)*(PI/30)))*static_castdouble(0.8*c._r));

x2=static_castint(c._x+(cos(static_castdouble(15-c._minute-static_castdouble(c._second)/60)*(PI/30)))*static_castdouble(0.6*c._r));

x1=static_castint(c._x+(cos(static_castdouble(3-c._hour-static_castdouble(c._minute)/60)*(PI/6)))*static_castdouble(0.4*c._r));

y3=static_castint(c._y-(sin(static_castdouble(15-c._second)*(PI/30)))*static_castdouble(0.8*c._r));

y2=static_castint(c._y-(sin(static_castdouble(15-c._minute-static_castdouble(c._second)/60)*(PI/30)))*static_castdouble(0.6*c._r));

y1=static_castint(c._y-(sin(static_castdouble(3-c._hour-static_castdouble(c._minute)/60)*(PI/6)))*static_castdouble(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);

intmain()

initgraph(1024,576);

setbkcolor(RGB(255,255,255));

cleardevice();

time_tnowtime;

struct

tm*ptime;

if(time(nowtime))

{

ptime=localtime(nowtime);

}

Clockc(512,288,ptime-tm_hour,ptime-tm_min,ptime-tm_sec,120,RGB(255,255,255));

Paintp1;

p1.drawclock_bk(c);

p1.drawclock_scale(c);

p1.draw

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論