//program to print the parabola by second order mid point method (y^2=2ax)
Source code
#include<dos.h>
#include<math.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
void plotpara(int x,int y)
{
int xmax=getmaxx();
int ymax=getmaxy();
putpixel(x+xmax/2,ymax/2-y,15);
putpixel(x+xmax/2,ymax/2+y,15);
}
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int xmax, ymax,x,y;
float d;
int a,b,i,dn,dne,de;
printf("Enter the value a ");
scanf("%d",&a);
clrscr();
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
/* an error occurred */
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
setcolor(getmaxcolor());
xmax = getmaxx();
ymax = getmaxy();
x=xmax/2;
y=ymax/2;
d=1-a;
for(i=0;i<xmax;i++)
putpixel(i,y,15);
for(i=0;i<ymax;i++)
putpixel(x,i,15);
x=0;
y=0;
dn=3;
dne=3-2*a;
while(a>(y+1))
{
if(d<0)
{
d+=dn;
dn+=2;
dne+=2;
y++;
}
else
{
d+=dne;
x++;
y++;
dn+=2;
dne+=2;
}
plotpara(x,y);
delay(10);
}
d=pow((y+.5),2)-2*a*(x+1);
de=-2*a;
dne=2*(y+1)-2*a;
while(x<200)
{
if(d<0)
{
d+=dne;
x++;
y++;
dne+=2;
}
else
{
d+=de;
x++;
}
plotpara(x,y);
delay(10);
}
/* clean up */
getch();
closegraph();
return 0;
}
No comments:
Post a Comment