C++使用easyx画实时走动的钟表_第1页
C++使用easyx画实时走动的钟表_第2页
C++使用easyx画实时走动的钟表_第3页
C++使用easyx画实时走动的钟表_第4页
C++使用easyx画实时走动的钟表_第5页
已阅读5页,还剩4页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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

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

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

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

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

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

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

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

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

如图:

如果要求圆上一点a的坐标(x,y),利用三角函数,若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,所以根据三角函数也可以求得点b的坐标为x:x0+cos(c)*(r-s),y为:y0-sin(c)*(r-s)。

这下有a、b这两点的坐标就可以画出一个刻度了,然后根据表盘的实际分布可以将所有的刻度画出来了(即每个刻度为5度)。

表针的画法与刻度类似:需要找这个b这种点(圆心与圆上的点连线上的点),然后根据你指定的针长和夹角,就可以求出b点的坐标。然后用b点坐标和圆心坐标就可以画出对应的指针了。

最重要的坐标求法就是这样了,剩下具体的细节请看下面代码:

#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. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论