前几年在本站发布过博文介绍人体生物钟程序的制作方法。后来发现上传后显示的博文有错漏,计算符号脱漏。这会误导读者。今修订整理重新发布,展示一下漂亮的界面设计。
人体生物钟也就是人体生物节律。人体生物节律是自然进化赋予生命的基本特征之一,人类和一切生物都要受到生物节律的控制与影响。人体生物节律是指体力节律、情绪节律和智力节律。每个人从他出生那天起一直到生命终结,都存在着体力23天、情绪28天、智力33天的周期性波动规律,称为人体生物节律。每一个周期中又存在着高潮期、低潮期和临界期。由于它具有准确的时间性,因此也称之为人体生物钟。在我们日常生活中,有人会觉得自己的体力、情绪或智力有时很好,有时很坏。人从他诞生之日起,直至生命终结,其自身的体力、情绪和智力都存在着由强至弱、由弱至强的周期性起伏变化。人们把这种现象称作生物节律,或生物节奏、生命节律等。之所以产生这种现象的原因是生物体内存在着生物钟,它自动地调节和控制着人体的行为和活动。
人体生物节律有什么作用,网上有许多介绍,对于想编程的朋友都是了解的,这里就不赘言什么了。本文就谈如何制作。就二个要点:一是输入当前日期和出生日期,此程序使用myspringc编译器自带的安卓式样日期输入控件。其他语言日期输入都很简单。二是根据这二个日期计算总天数,依数值绘出曲线。难点是曲线的绘制。本程序给出了曲线算法和绘图方法,以及绘制漂亮的界面。程序使用最基本的绘图函数DrawLine,DrawRect,DrawCircle,设置颜色SetColor(a,r,g,b)。程序有详细注释介绍绘制方法。
本程序是用简单的C语言编译调试的。主要展示该语言超强的绘图功能。如何绘制图形曲线,绘制漂亮的界面。本人喜欢用编程语言的绘图功能绘制图形图案和漂亮的界面,能发挥你的想象力和创造力,很有趣,希望大家也喜欢。
下面的代码是用简单的C语言写的,很简洁 ,适用于初学者。
// myspringc v2.7 可编译安卓手机本机 app 应用
// 此样例可复制黏贴到 myspringc 编译
// 亦可用vb6, VC , Java 等改写
//*******人体生物钟***************
//== 人体生物节律曲线 == Biorhythm
//制作人:张纯叔micelu@126.com
//**** myspringc v2.7编译 *********
Canvas cs; //画布
float pi=3.1415926535;
float src[4]; //ClearDraw (cls) clear screen
int s1,s2,s3;
int s1n,s2n,s3n;
int i,k,bx;//bx 画标尺
int alln0,alln1; //计算日期
int alln;//set today-birthdate all days
int ns; //draw
float a;
float dx,dy, x0,y0;
float x1,y1;
string s,ss,ss1,ss2,ss3,ss4,ss5,ss6; //输出字串
string gs1,gs2,gs3,gs4,gs5,gs6;
int by,bm,bd; //birthdate yy,mm,dd
int ty,tm,td; //today
double bms,tms;
int d[3]; //input date
int count;
int context;//canvasProc触屏按钮
int obj;
int id;
int event;
main(){
setDisplay(1);
cs.Active();
cs.SetBackground(255,255,255);
setTitle(“人体生物钟 ( Biorhythm )”);
cs.SetProc (context, mycanvasProc);
drawtable ();
while (){}
}//main()
mycanvasProc (int context,int obj,int id,int event,float x,float y){
cs.SetFillMode(0);//0不填色,1填色
//** 触控按钮 select button : input date ****
if (x>400&&x810&&y<880) {
input_date ();} //输入日期
if (x>300&&x980&&y<1050){
input_date ();} //输入日期
if (x>440&&x980&&y<1050) {
input_date ();} //重新开始
if (x>580&&x980&&y<1050){
clearOutput();
cs.ClearDraw (0,src);
setDisplay (0);
exit (0);} //Exit 退出程序
}//mycanvasProc ()
drawbutton (){//绘制按钮
cs.SetColor (255,230,230,230);
cs.DrawRect (4,940,715,1078);
cs.SetColor (255,255,255,255);
cs.DrawRect (11,960,707,963);
cs.DrawRect (11,1065,707,1068);
for (i=0;i<3;i++){//draw 3-5 cmd_button
cs.SetColor (255,150,150,150);
cs.DrawRect (i*140+299,980,i*140+423,1052);
cs.SetColor (255,250,250,250);
cs.DrawRect (i*140+297,978,i*140+420,1050);
cs.SetColor (255,200,240,250);
cs.DrawRect (i*140+300,980,i*140+420,1050);
}
cs.SetTextSize (24);
cs.SetTextStyle(1);
cs.SetColor(255,50,150,220);
cs.DrawText (“输入日期”,313,1020);
cs.DrawText (“重新开始”,453,1020);
cs.DrawText (“退出程序”,595,1020);
s=”BIORHYTHM”; //立体字
cs.SetTextSize (40);
cs.SetTextStyle(1);
cs.SetColor (255,100,100,100);
cs.DrawText (s,20,1030);
cs.SetColor (255,255,255,255);
cs.DrawText (s,23,1033);
//draw Input button ****
cs.SetColor (255,250,0,150);
cs.DrawRect (399,819,561,881);
cs.SetColor (255,120,150,250);
cs.DrawRect (400,820,560,880);
cs.SetTextSize (24);
cs.SetTextStyle(0);
cs.SetColor(255,250,250,0);
cs.DrawText (“输入日期”,435,855);
cs.SetColor (255,250,0,150);
s=”请输入当前日期和出生日期 >> “;
cs.DrawText (s,80,860);
cs.Update ();
}//drawbutton ()
input_date (){ //输入日期
//here get birth date & current date then show out
inputdd:
getDate(d);
pickDate(“输入>当前日期”,d);
ty=d[0];
tm=d[1];
td=d[2];
//print “currentday = “,ty ,”-“,tm,”-“,td;
pickDate(“输入>出生日期”,d);
by=d[0];
bm=d[1];
bd=d[2];
//print “birthdate = “,by ,”-“,bm,”-“,bd;
clearOutput (); //clear output text
cs.ClearDraw (0,src); //clear screen
// calculate (alln)= all days from birthdate to today
gs1=intToString (by);
gs2=intToString (bm);
gs3=intToString (bd);
gs4=intToString (ty);
gs5=intToString ™;
gs6=intToString (td);
if (by>ty){
ss=”输入错误 ! 重新输入当前日期,出生日期。”;
msgBox (“人体生物节律 ” ,ss);
goto inputdd;}
if (bm<10)gs2="0"+gs2;
if (bd<10)gs3="0"+gs3;
if (tm<10)gs5="0"+gs5;
if (td<10)gs6="0"+gs6;
print ” Input birthdate = “,gs1,” – “,gs2,” – “,gs3;
print ” Input this Date = “,gs4,” – “,gs5,” – “,gs6;
//getDate and calculate past days alln ****
string yds;
string ydds;
double tms;
yds=”000031059090120151181212243273304334″;
//计算生日 alln0
ydds=subString(yds,(bm-1)*3,3);
bms=stringToDouble(ydds);
alln0=(by-1900)*365+((by-1900-1)/4)
+(int)(bms)+bd;
if ((by-by/4*4)==0&&bm>2)alln0=alln0+1; //加闰
// print “birth = “,alln0; //test
//计算当前日 alln1
ydds=subString(yds,(tm-1)*3,3);
tms=stringToDouble(ydds);
alln1=(ty-1900)*365+((ty-1900-1)/4)
+(int)(tms)+td+1;//*****
if ((ty-ty/4*4)==0&&tm>2)alln1=alln1+1; //加闰
// print “current = “,alln1;//test
alln=alln1-alln0;
// print “all days = “,alln;//test
if (alln<1){//容错
ss=”输入错误 ! 重新输入当前日期,出生日期。”;
msgBox (“人体生物钟 ” ,ss);
goto inputdd;}
drawtable ();//show…绘出曲线
}//input_date ()
绘制曲线:初始图表样例
drawtable(){//draw table and show
cs.ClearDraw(0,src);//清屏重绘
//get birthdate yymmdd and current yymm ***
//calculate s1,s2,s3 and draw curve **********
//drawout and show biorhythm **********
cs.SetFillMode(1);//0不填色,1填色
cs.SetColor(255,240,240,240);
cs.DrawRect(10,10,710,600);
// draw table ===============
cs.SetStrokeWidth(1);//画笔线宽
cs.SetFillMode(1);//0不填色,1填色
cs.SetTextSize(60);
cs.SetTextStyle(1); //粗体
s=”人体生物节律曲线”;
cs.SetColor(255,120,60,0);
cs.DrawText(s,53,104);
cs.SetColor(255,0,250,0);
cs.DrawText(s,50,100);
cs.SetFillMode(0);//0不填色,1填色
cs.SetColor(255,250,150,0);
cs.DrawText(s,50,100);
cs.SetTextStyle(0);
cs.SetStrokeWidth (1);
cs.SetTextSize (28);
cs.SetColor(250,250,0,0);
ss=”高潮区”;
cs.DrawText (ss,560,210);
ss=”临界区 “;
cs.DrawText (ss,560,270);
ss=”低潮区 “;
cs.DrawText (ss,560,410);
cs.SetFillMode(1);//0不填色,1填色
cs.SetColor(250,250,0,0);
ss=”体力 >”;
cs.DrawText (ss,200,510);
cs.SetColor(180,0,90,0);
ss=”情绪 >”;
cs.DrawText (ss,200,540);
cs.SetColor(180,0,0,180);
ss=”智力 >”;
cs.DrawText (ss,200,570);
//draw table========
cs.SetFillMode (1);
cs.SetStrokeWidth(2);
cs.SetColor(100,250,250,0); //high low 区块
cs.DrawRect(50,150,670,450);
cs.SetColor (100,255,120,255);
cs.DrawRect (50,150,670,235);
cs.SetColor(100,130,255,255);
cs.DrawRect(50,365,670,450);
cs.SetColor (255,0,120,128);//X Line标尺
cs.DrawRect(20,296,710,304);
cs.SetColor(180,250,0,0);
cs.DrawRect (50,500,180,506); //s1
cs.SetColor(180,0,200,0);
cs.DrawRect (50,530,180,536); //s2
cs.SetColor(180,0,0,192);
cs.DrawRect (50,560,180,566); //s3
cs.SetColor (255,0,0,0);
cs.SetTextSize (20);
cs.DrawText(“0”,44,340);
cs.DrawText(“5”,143,340);
cs.DrawText(“10”,238,340);
cs.DrawText(“15”,338,340);
cs.DrawText(“20”,438,340);
cs.DrawText(“25”,538,340);
cs.DrawText(“30”,638,340);
cs.SetFillMode (0);
cs.SetColor (255,250,250,0);
cs.DrawRect(23,300,715,300);//X Line 标尺
cs.SetColor (255,0,0,128);
for (i=0;i<34;i++){//画标尺点位
bx=50+i*20;
cs.DrawLine (bx,280,bx,320); }
cs.SetColor (255,255,0,0);
for (i=0;i<7;i++){//画标尺 5 天标线
bx=50+i*100;
cs.DrawCircle (bx,300,2);
cs.DrawLine (bx,150,bx,320);
cs.DrawLine (bx,350,bx,450); }
//绘制人体生物节律曲线
// Physical Sensitive Intellectual
//draw curve : x1 +50 标尺x轴 0 位, s1*20 步长
//x0 曲线幅宽, + -130 y轴峰谷弧高, y+300 x轴y定位
//x0 : > 230 = 23d, 280 = 28d, 330 = 33d
//Draw out graph here ===========
//get birthdate & calculate thisday-birthdate
cs.SetStrokeWidth(2);
ns=td-alln; //这算法很奇特,研究一下
//s1=23d, draw graph 体力
s1=ns-ns/23*23;//mod (ns,23)
cs.SetColor (255,255,0,0);
for (i=-140;i<280;i++){
a=pi/360*i*6;//360 曲线点密度
x0=230;
x1=(float)((x0/pi*a+50)+s1*20);
y1=(float)(-130*sin(a)+300);
cs.DrawCircle(x1,y1,3);}
print ” s1= “,s1;
//s2=28d, draw graph 情绪
s2=ns-ns/28*28; //mod(ns,28)
cs.SetColor (255,0,220,0);
for (i=-140;i<280;i++){
a=pi/390*i*6;//390 曲线点密度
x0=280;
x1=(float)((x0/pi*a+50)+s2*20);
y1=(float)(-130*sin(a)+300);
cs.DrawCircle(x1,y1,3);}
print ” s2= “,s2;
//s3=33d, draw graph 智力
s3=ns-ns/33*33; //mod (ns,33)
cs.SetColor (205,0,0,192);
for (i=-140;i<280;i++){
a=pi/420*i*6; //420 曲线点密度
x0=330;
x1=(float)((x0/pi*a+50)+s3*20);
y1=(float)(-130*sin(a)+300);
cs.DrawCircle(x1,y1,3);}
Print ” s3= “,s3;
cs.SetStrokeWidth(0);//绘出当日标尺
cs.SetColor (255,60,60,60);
x1=(float)(td*20+47); //draw current day当日标
cs.DrawRect (x1,146,x1+5,454);
cs.SetFillMode (1);//曲线两边遮罩
cs.SetStrokeWidth(2);
cs.SetColor (255,255,255,255);
cs.DrawRect (0,150,11,450);
cs.DrawRect (709,150,720,450);
cs.SetColor (255,164,164,164);
cs.DrawRect (710,15,717,606);
cs.DrawRect (15,600,716,606);
cs.SetFillMode (0);
cs.SetColor(255,50,0,128);
cs.DrawRect(10,10,710,600);
//== draw data ======
cs.SetTextSize (32);
cs.SetFillMode (1);
cs.SetStrokeWidth(2);
cs.SetColor (255,0,0,200);
if (len(gs2)==1)gs2=”0″+gs2;
if (len(gs3)==1)gs3=”0″+gs3;
if (len(gs5)==1)gs5=”0″+gs5;
if (len(gs6)==1)gs6=”0″+gs6;
ss1=”出生日期 : “+gs1+” 年 “+gs2+” 月 “+gs3+” 日 “;
cs.DrawText(ss1,30,680);
ss2=”当前日期 : “+gs4+” 年 “+gs5+” 月 “+gs6+” 日 “;
cs.DrawText (ss2,30,730);
s=intToString (alln);
//print ” all days = “,alln; //test
ss3=”你已在这美好的世界上生活了”+s+”天。”;
if (alln<1)ss3="Welcome to using our program . ";
cs.DrawText(ss3,30,780);
ss=”制作人: 张纯叔 ( micelu@126.com ) “;
cs.SetColor (255,150,180,250);
cs.DrawRect (5,1078,715,1080);
cs.DrawText (ss,20,1110);
//draw status 状态 ***********
cs.SetFillMode(1);//0不填色,1填色
//**** s1n s 23
s1n=alln-alln/23*23 ;
cs.SetColor (255,250,220,0) ; //临界
cs.DrawRect (300,490,400,510);
if (s1n>2&&s1n<10){//高3 – 9
cs.SetColor(255,250,120,180);
cs.DrawRect (300,490,400,510);}
if (s1n>13&&s1n<22){ //低 14~21
cs.SetColor (255,130,250,150) ;
cs.DrawRect (300,490,400,510);}
//**** s2n s 28
s2n=alln-alln/28*28 ;
cs.SetColor (255,250,220,0) ; //临界
cs.DrawRect (300,520,400,540);
if (s2n>2&&s2n<12){//高3 – 11
cs.SetColor(255,250,120,180);
cs.DrawRect (300,520,400,540);}
if (s2n>16&&s2n<26){ //低 17 – 25
cs.SetColor (255,130,250,150) ;
cs.DrawRect (300,520,400,540);}
//**** s3n s 33
s3n=alln-alln/33*33 ;
cs.SetColor (255,250,220,0) ; //临界
cs.DrawRect (300,550,400,570);
if (s3n>2&&s3n<14){//高3 – 13
cs.SetColor(255,250,120,180);
cs.DrawRect (300,550,400,570);}
if (s3n>19&&s3n<31){ //低 20 – 30
cs.SetColor (255,130,250,150) ;
cs.DrawRect (300,550,400,570);}
drawbutton (); //绘制按钮
cs.Update();//show out table
}//drawtable ()
//**** End ******