Добавил:
korayakov
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:
//Ї®бв஥ЁҐ б®бв ў®© Ї®ўҐае®бвЁ ЃҐ§мҐ (ЄгЎЁзҐбЄЁ© бЇ« ©)
//3 н«Ґ¬Ґв алҐ Ї®ўҐае®бвЁ ЃҐ§мҐ
#include <conio.h>
#include <stdio.h>
#include <process.h>
#include <graphics.h>
#include <math.h>
#include <BIOS.h>
#include <DOS.h>
union inkey //§¤Ґбм еа Ёвбп Ёд®а¬ жЁп ® ¦ в®© Є« ўЁиҐ
{ //ch[0] -¬« ¤иЁ© Ў ©в, ch[1] -бв аиЁ© Ў ©в
char ch[2]; //ch[1] -Є®¤ бЄ Ёа®ў Ёп Є« ўЁиЁ
int i; //ch[0] -ASCII Є®¤
} c; //i -®ЎкҐ¤ЁҐЁҐ ¤ўге Ў ©в ў б«®ў®
double Px[4][4], Py[4][4], Pz[4][4]; //иҐбв ¤ж вм 㧫.в®зҐЄ Ї®ўҐае®бвЁ ЃҐ§мҐ
//дгЄжЁп ўлЎа ®© Ї®ўҐае®бвЁ
double functionZ(double x,double y)
{
return 0.5-exp(-5*(x*x+y*y));
}
//§ ¤ Ґ¬ § 票п 㧫®ўле в®зҐЄ
void basePoint(double fi1, double fi2, double rad1, double rad2)
{
double fi, r, dfi=(fi2-fi1)/3, dr=(rad2-rad1)/3;
int i, j;
for(i=0; i<4; i++)
for(j=0; j<4; j++)
{
fi=fi1+dfi*i; r=rad1+dr*j;
Py[j][i]=r*sin(M_PI/180.0*fi);
Px[j][i]=r*cos(M_PI/180.0*fi);
}
for(j=0; j<4; j++)
for(i=0; i<4; i++)
Pz[j][i]=functionZ(Px[j][i],Py[j][i]);
}
double fi,teta,sf,cf,st,ct;
//ЇҐаҐе®¤ ў ўЁ¤®ўго бЁб⥬㠪®®а¤Ё в Ё ®ав®Ја дЁзҐбЄ®Ґ Їа®ҐЄвЁа®ў ЁҐ
double Xe(double x,double y)
{
return -sf*x+cf*y;
}
//ЇҐаҐе®¤ ў ўЁ¤®ўго бЁб⥬㠪®®а¤Ё в Ё ®ав®Ја дЁзҐбЄ®Ґ Їа®ҐЄвЁа®ў ЁҐ
double Ye(double x,double y,double z)
{
return -ct*cf*x-ct*sf*y+st*z;
}
double xe1, xe2, ye1, ye2;
//ЇҐаҐў®¤ ўЁ¤®ўле Є®®а¤Ё в ў Є®®а¤Ё вл ЇЁЄбҐ«Ґ©
int pixelX(double xe)
{
return (int)(getmaxx()*(xe-xe1)/(xe2-xe1));
}
//ЇҐаҐў®¤ ўЁ¤®ўле Є®®а¤Ё в ў Є®®а¤Ё вл ЇЁЄбҐ«Ґ©
int pixelY(double ye)
{
return (int)(getmaxy()*(ye-ye2)/(ye1-ye2));
}
double Bezie(int i, double t) //зҐвлॠдгЄжЁЁ ЃҐ§мҐ (ЃҐаи⥩ )
{
double b;
switch(i)
{
case 0 : b=(1-t)*(1-t)*(1-t); break;
case 1 : b=3*t*(1-t)*(1-t); break;
case 2 : b=3*t*t*(1-t); break;
case 3 : b=t*t*t; break;
default : printf("\nError in function Bezie"); exit(1);
}
return b;
}
double splineX(double u,double v)
{
double S1=0, S2;
int i,j;
for(i=0; i<4; i++)
{
S2=0;
for(j=0; j<4; j++)
S2 += Px[i][j]*Bezie(j,v);
S1 += S2*Bezie(i,u);
}
return S1;
}
double splineY(double u,double v)
{
double S1=0, S2;
int i,j;
for(i=0; i<4; i++)
{
S2=0;
for(j=0; j<4; j++)
S2 += Py[i][j]*Bezie(j,v);
S1 += S2*Bezie(i,u);
}
return S1;
}
double splineZ(double u,double v)
{
double S1=0, S2;
int i,j;
for(i=0; i<4; i++)
{
S2=0;
for(j=0; j<4; j++)
S2 += Pz[i][j]*Bezie(j,v);
S1 += S2*Bezie(i,u);
}
return S1;
}
int kl=1;
void baseSurface(double fi1, double fi2, double rad1, double rad2, double scale)
{
//§ ¤ Ґ¬ § 票п 㧫®ўле в®зҐЄ
basePoint(fi1,fi2,rad1,rad2);
//Є®®а¤Ё вл Їа®ҐЄвЁа㥬®© в®зЄЁ
double xe, ye;
//Є®®а¤Ё вл ЇЁЄбҐ«®ў
int x1,y1,x2,y2;
int i,j;
if(kl==1)
{
for(i=0; i<4; i++)
for(j=0; j<3; j++)
{
xe=scale*Xe(Px[i][j],Py[i][j]);
ye=scale*Ye(Px[i][j],Py[i][j],Pz[i][j]);
x1=pixelX(xe);
y1=pixelY(ye);
xe=scale*Xe(Px[i][j+1],Py[i][j+1]);
ye=scale*Ye(Px[i][j+1],Py[i][j+1],Pz[i][j+1]);
x2=pixelX(xe);
y2=pixelY(ye);
line(x1,y1,x2,y2);
}
for(j=0; j<4; j++)
for(i=0; i<3; i++)
{
xe=scale*Xe(Px[i][j],Py[i][j]);
ye=scale*Ye(Px[i][j],Py[i][j],Pz[i][j]);
x1=pixelX(xe);
y1=pixelY(ye);
xe=scale*Xe(Px[i+1][j],Py[i+1][j]);
ye=scale*Ye(Px[i+1][j],Py[i+1][j],Pz[i+1][j]);
x2=pixelX(xe);
y2=pixelY(ye);
line(x1,y1,x2,y2);
}
}
int N=100;
double u,v,du=1.0/N,dv=1.0/N;
for(j=0; j<4; j++)
{
v=1.0/3.0*j;
for(i=0; i<N; i++)
{
u=du*i;
xe=scale*Xe(splineX(u,v),splineY(u,v));
ye=scale*Ye(splineX(u,v),splineY(u,v),splineZ(u,v));
x1=pixelX(xe);
y1=pixelY(ye);
putpixel(x1,y1,14);
}
}
for(j=0; j<4; j++)
{
u=1.0/3.0*j;
for(i=0; i<N; i++)
{
v=dv*i;
xe=scale*Xe(splineX(u,v),splineY(u,v));
ye=scale*Ye(splineX(u,v),splineY(u,v),splineZ(u,v));
x1=pixelX(xe);
y1=pixelY(ye);
putpixel(x1,y1,12);
}
}
}
void main()
{
int driver=DETECT;
int mode;
int res;
initgraph(&driver,&mode,"c:\\mathemat\\borlandc\\bgi");
if((res=graphresult())!=grOk)
{
printf("\nGraphics error: %s",grapherrormsg(res));
exit(1);
}
//¬ ЄбЁ¬ «млҐ ¤«Ёл ®бҐ© -¬Ёа®ўлҐ Є®®а¤Ё вл
double xmax=1.2, ymax=1.2, zmax=1.2;
//а §¬Ґал ўЁ¤®ў®Ј® ®Є ў Ї«®бЄ®бвЁ Їа®ҐЄвЁа®ў Ёп
double pixelxy=(double)getmaxy()/(double)getmaxx();
xe1=-2; xe2=2; ye1=-2*pixelxy; ye2=2*pixelxy;
// setgraphmode(1);
double fi1, fi2, rad1, rad2;
//Є®®а¤Ё вл Їа®ҐЄвЁа㥬®© в®зЄЁ
double xe, ye;
//Є®®а¤Ё вл ЇЁЄбҐ«®ў
int x1,y1,x2,y2;
fi=30, teta=60; // з «м®Ґ Ї®«®¦ҐЁҐ ўЁ¤®ў®© бЁбвҐ¬л Є®®а¤Ё в
double scale=1; //¬ҐпҐ¬ а §¬Ґал ®ЎкҐЄв ¬Ґпп Є®нддЁжЁҐв scale
// int num=1;
lbl_1:
// setactivepage(num);
cleardevice();
line(0,0,getmaxx(),0);
line(getmaxx(),0,getmaxx(),getmaxy());
line(0,0,0,getmaxy());
line(0,getmaxy(),getmaxx(),getmaxy());
outtextxy(10,10,"3 elementary Bezie surfaces");
outtextxy(10,20,"Arrows - rotation of the object");
outtextxy(10,30,"U - increase of the object");
outtextxy(10,40,"D - decrease of the object");
outtextxy(10,50,"S - remove sceleton");
char ss[20];
sprintf(ss,"fi= %4.0lf",fi);
outtextxy(550,50,ss);
sprintf(ss,"teta= %4.0lf",teta);
outtextxy(550,70,ss);
//ўлзЁб«ҐЁҐ бЁгб®ў Ё Є®бЁгб®ў ®аЁҐвЁа®ўЄЁ ўЁ¤®ў®© бЁбвҐ¬л Є®®а¤Ё в
sf=sin(M_PI*fi/180);
cf=cos(M_PI*fi/180);
st=sin(M_PI*teta/180);
ct=cos(M_PI*teta/180);
setcolor(3);
//®бм Ox
xe=Xe(0,0);
ye=Ye(0,0,0);
x1=pixelX(xe);
y1=pixelY(ye);
xe=scale*Xe(xmax,0);
ye=scale*Ye(xmax,0,0);
x2=pixelX(xe);
y2=pixelY(ye);
line(x1,y1,x2,y2);
outtextxy(x2,y2," X");
//®бм Oy
xe=Xe(0,0);
ye=Ye(0,0,0);
x1=pixelX(xe);
y1=pixelY(ye);
xe=scale*Xe(0,ymax);
ye=scale*Ye(0,ymax,0);
x2=pixelX(xe);
y2=pixelY(ye);
line(x1,y1,x2,y2);
outtextxy(x2,y2," Y");
//®бм Oz
xe=Xe(0,0);
ye=Ye(0,0,0);
x1=pixelX(xe);
y1=pixelY(ye);
xe=scale*Xe(0,0);
ye=scale*Ye(0,0,zmax);
x2=pixelX(xe);
y2=pixelY(ye);
line(x1,y1,x2,y2);
outtextxy(x2,y2," Z");
setcolor(15);
//а §¬Ґал ®ЎкҐЄв ў Ї«®бЄ®бвЁ xy -¬Ёа®ўлҐ Є®®а¤Ё вл
fi1=0, fi2=90, rad1=0.1, rad2=1;
baseSurface(fi1,fi2,rad1,rad2,scale);
fi1=90, fi2=180, rad1=0.1, rad2=1;
baseSurface(fi1,fi2,rad1,rad2,scale);
fi1=180, fi2=270, rad1=0.1, rad2=1;
baseSurface(fi1,fi2,rad1,rad2,scale);
// fi1=0, fi2=90, rad1=0.4, rad2=1;
// baseSurface(fi1,fi2,rad1,rad2,scale);
// fi1=90, fi2=180, rad1=0.4, rad2=1;
// baseSurface(fi1,fi2,rad1,rad2,scale);
// fi1=180, fi2=270, rad1=0.4, rad2=1;
// baseSurface(fi1,fi2,rad1,rad2,scale);
// fi1=270, fi2=360, rad=1;
// baseSurface(fi1,fi2,rad,scale);
// setvisualpage(num);
// if(num==0) num=1;
// else num=0;
while(!bioskey(1)); //Їа®ўҐапҐв Ґбвм «Ё бЁ¬ў®« ў ЎгдҐаҐ ўў®¤
//дгЄжЁп Ґ ®бв® ў«Ёў Ґв Їа®Ја ¬¬г
c.i=bioskey(0); //ў®§ўа й Ґв Є®¤ ¦ в®© Є« ўЁиЁ (ЇаҐалў ЁҐ 0x14)
//Ё § ЇЁблў Ґв ў ®ЎкҐ¤ЁҐҐ б
//Ґб«Ё ЎгдҐа Їгбв, в® Їа®Ја ¬¬ ®бв ў«Ёў Ґвбп
if(c.ch[0]==0 && c.ch[1]==72) {teta += 5; goto lbl_1; }
if(c.ch[0]==0 && c.ch[1]==80) {teta -= 5; goto lbl_1; }
if(c.ch[0]==0 && c.ch[1]==75) {fi += 5; goto lbl_1; }
if(c.ch[0]==0 && c.ch[1]==77) {fi -= 5; goto lbl_1; }
if(c.ch[0]==117 && c.ch[1]==22) {scale += 0.1; goto lbl_1; }
if(c.ch[0]==100 && c.ch[1]==32) {scale -= 0.1; goto lbl_1; }
if(c.ch[0]==115 && c.ch[1]==31)
{ if(kl==0) kl=1;
else kl=0; goto lbl_1; }
// getch();
closegraph();
}