原创 李萨如图形生成模拟

2008-9-4 13:14 5706 3 3 分类: 软件与OS
功能介绍:
?
(1)分别输入参数 A1,A2,M1,M2,beta
(2)将屏幕分成四个区域,


? ? ? ? ? ? ? ? ? ?1 2
? ? ? ? ? ? ? ? ? ?3 4
1区显示两个输入函数 , ,以及相关功能键,ESC:退出;S:重新设置两个函数的参数; SPACE(空格键):暂停动画显示李萨如图形(按任意键恢复显示);
2区显示函数;?
3区显示函数:?
4区显示李萨如图形:

源程序:
#include <stdio.h>
#include <graphics.h>
#include <math.h>
#include <dos.h>
#include <bios.h>
#include <conio.h>
#define PI 3.1415926
#define VK_ESC 27
#define VK_SET 115
#define VK_SPACE 32

void initG();/*图形模式初始化*/
void closeG();/*关闭图形模式*/
void set();/*设置参数*/
void help();/*显示功能键及相关操作*/
void drawforce(void);/*屏幕分成四个区域*/
void wavedelay();/*延时*/
void drawtimeline(int,int,int); /*画水平波形*/
void drawwave(int,int,int); /*画垂直波形*/
void drawLisajous(int,int,int); /*画李萨如图*/
void drawall();/*画所有图形*/
void clrall();/*擦屏*/
void change_t2();

float t,t2;
float x,y;
float x1,y1,x2,y2;
float beta;
int x0=320,y0=240;
float M1,M2,A1,A2;/*输入函数的参数*/

void initG()/*initial graphics*/
{
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"d:\\TURBOC2");
cleardevice();
}

void closeG()
{
cleardevice();
settextstyle(1,0,3);
setcolor(BLUE);
outtextxy(80,120,"Thanks for using the Program");
outtextxy(80,190,"This Program Is Modified By Ming Lee");
outtextxy(80,260,"E-Mail:freeren_liming@126.com");
outtextxy(200,330,"Date:2008.9.2");
getch();
closegraph();
}

void set()
{
cleardevice();
clrscr();
textbackground(BLACK);
textcolor(BLACK);
setbkcolor(BLACK);
printf("input parameters:\n");
printf("f1(x)=A1*sin(M1*x)\n");
printf("f2(x)=A2*sin(M2*x+beta)\n");
printf(“A1=”);
scanf(“%f”,&A1);
printf("M1=");
scanf("%f",&M1);
printf(“%A2=”);
scanf(“%f”,&A2);
printf("M2=");
scanf("%f",&M2);
printf("beta=");
scanf("%f",&beta);
}
void help()
{
cleardevice();
textcolor(BLACK);
gotoxy(14,7);
printf("f1=%.2fsin(%.2f*x)\n",A1,M1);
gotoxy(14,8);
printf("f2=%.2fsin(%.2f*x+%.2f)\n",A2,M2,beta);
settextstyle(0,0,1);
setcolor(LIGHTRED);
outtextxy(105,140,"STOPRUN:");
outtextxy(105,150,"EXIT:");
outtextxy(105,160,"RESET:");
setcolor(YELLOW);
outtextxy(105,170,"YELLOW:");
setcolor(WHITE);
outtextxy(105,180,"WHITE");
setcolor(LIGHTGREEN);
outtextxy(105,190,"GREEN");
setcolor(LIGHTGREEN);
outtextxy(180,140,"Space");
outtextxy(180,150,"Esc");
outtextxy(180,160,"S/s");
outtextxy(180,170,"f1(x)");
outtextxy(180,180,"f2(x)");
outtextxy(180,190,"lisajous");
setcolor(LIGHTCYAN);
settextstyle(0,0,2);
outtextxy(110,30,"Lisajous");
}

void drawforce(void)
{
setcolor(LIGHTCYAN);
rectangle(95,15,305,230);
rectangle(335,255,545,465);
rectangle(335,15,545,230);
rectangle(95,255,305,465);
delay(1000);
}

void clrall()
{
setcolor(BLACK);
setfillstyle(SOLID_FILL,BLACK);
bar(1,1,640,480);
}

void drawtimeline(int timex,int timey,int r)
{
setcolor(YELLOW);
for(t=0;t<=360;t++)
{
y1=y0-timey+10*t/18;
x1=x0+timex+r*A1*sin(M1*t*PI/180+t2);
if((int)(M1*t)%90==0) /*选取几个点以显示其变化情况*/
{
setfillstyle(SOLID_FILL,WHITE);
bar(x1-1,y1-1,x1+1,y1+1);
}
else setcolor(YELLOW);
if(t==0) moveto(x1,y1);
lineto(x1,y1);
moveto(x1,y1);
}
}

void drawwave(int wavex,int wavey,int r)
{
setcolor(WHITE);
for(t=0;t<=360;t++)
{
x2=x0-wavex+10*t/18;
y2=y0+wavey-r*A2*sin(M2*t*PI/180+t2);
if(((int)(M2*t))%90==0)
{
setfillstyle(SOLID_FILL,YELLOW);
bar(x2-1,y2-1,x2+1,y2+1);
}
else setcolor(WHITE);
if(t==0) moveto(x2,y2);
lineto(x2,y2);
moveto(x2,y2);
}
}



void drawLisajous(int lsx,int lsy,int r)
{
setcolor(LIGHTGREEN);
for(t=0;t<=360;t++)
{
y=y0+lsy-r*sin(M1*t*PI/180+t2);
x=x0+lsx-r*sin(M2*t*PI/180+t2+beta);
if((((int)(M1*t))%90==0)||(((int)(M2*t))%90==0))
{
setfillstyle(SOLID_FILL,WHITE);
bar(x-1,y-1,x+1,y+1);
}
else setcolor(LIGHTGREEN);
if(t==0) moveto(x,y);
lineto(x,y);
moveto(x,y);
}
}

void wavedelay()
{
int time;
for(time=0;time<20;time++) delay(30*time);
}

void change_t2()
{
t2+=PI/96;
if(t2>=2*PI+beta) t2=beta;
}

void drawall()
{
clrscr();
help();
drawforce();
drawtimeline(120,220,10);
drawwave(220,120,10);
drawLisajous(120,120,10);
wavedelay();
setcolor(BLACK);
setfillstyle(SOLID_FILL,BLACK);
change_t2();
}
void main()
{ int key;
initG();
set();
for(;;)
{ drawall();
while(bioskey(1)) /*功能键操作*/
{
key=bioskey(0);
key=key&0xff?key&0xff:key>>8;
switch(key)
{
case VK_ESC: closeG();break;
case VK_SET: set(); break;
case VK_SPACE: getch();
default: ;
}/*end switch key*/
}
change_t2();
}/*end for*/
}



PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
我要评论
0
3
关闭 站长推荐上一条 /3 下一条