#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
// this function initializes graphics mode
// it will work only if you're using Borland C++ compiler & BGI drivers
// if you're using another compiler you should overwrite body of this function
void init_gr(void)
{
/* request autodetection */
int gdriver = DETECT, gmode, errorcode;
/* initialize graphics mode */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg
(errorcode
));
printf("Press any key to halt:");
getch();
exit(1); /* return with error code */
}
}
// this function shuts graphics mode down
// it will work only if you're using Borland C++ compiler & BGI drivers
// if you're using another compiler you should overwrite body of this function
void end_gr(void)
{
closegraph();
}
// this function moves CP to (x,y) position
// it will work only if you're using Borland C++ compiler & BGI drivers
// if you're using another compiler you should overwrite body of this function
void MoveTo(int x, int y)
{
moveto(x,y);
}
// this function draws a line to (x,y) position
// it will work only if you're using Borland C++ compiler & BGI drivers
// if you're using another compiler you should overwrite body of this function
void LineTo(int x, int y)
{
lineto(x,y);
}
// this function draws a line from (x1,y1) to (x2,y2)
// it will work only if you're using Borland C++ compiler & BGI drivers
// if you're using another compiler you should overwrite body of this function
void Line(int x1, int y1, int x2, int y2)
{
line(x1,y1,x2,y2);
}
const N=21; // number of points
const M=300.0; // number of steps between two points
// coordinates of all given points
int x[N]={50,25,50,125,200,225,275,475,590,615,615,615,600,475,275,225,200,125,50,25,50};
int y[N]={140,100,60,40,70,90,85,75,80,85,100,115,120,125,115,110,130,160,140,100,60};
// coefficients
float t,xA,xB,xC,xD,yA,yB,yC,yD,a0,a1,a2,a3,b0,b1,b2,b3;
int n,i,j,first;
int main(void)
{
// initializing graphics mode
init_gr();
/* mark the given points */
for (i=0;i<N;i++)
{
Line(x[i]-4,y[i]-4,x[i]+4,y[i]+4);
Line(x[i]+4,y[i]-4,x[i]-4,y[i]+4);
}
/* main loop */
first=1;
for(i=1;i<N-2;i++)
{
// calculating coefficients
xA=x[i-1]; xB=x[i]; xC=x[i+1]; xD=x[i+2];
yA=y[i-1]; yB=y[i]; yC=y[i+1]; yD=y[i+2];
a3=(-xA+3*(xB-xC)+xD)/6.0; b3=(-yA+3*(yB-yC)+yD)/6.0;
a2=(xA-2*xB+xC)/2.0; b2=(yA-2*yB+yC)/2.0;
a1=(xC-xA)/2.0; b1=(yC-yA)/2.0;
a0=(xA+4*xB+xC)/6.0; b0=(yA+4*yB+yC)/6.0;
for (j=0;j<M;j++) // drawing curve between two given points
{
t=(float)j/(float)M;
if (first)
{
first=0;
MoveTo(((a3*t+a2)*t+a1)*t+a0,((b3*t+b2)*t+b1)*t+b0);
} else LineTo(((a3*t+a2)*t+a1)*t+a0,((b3*t+b2)*t+b1)*t+b0);
}
}
/* clean up */
getch();
end_gr();
return 0;
}