5. 5
Program 2
WRITE A C PROGRAM TO DRAW MIDPOINT CIRCLE ALGORITHM.
# include<stdio.h>
# include<graphics.h>
# include<conio.h>
# include<math.h>
void bresenham_circle(const int h,const int k,const int r)
{
int x=0,y=r,p=(3-(2*r));
// cleardevice();
line(320,1,320,480);
line(1,240,640,240);
do
{
delay(15);
putpixel((h+x),(k+y),25);
putpixel((h+y),(k+x),15);
putpixel((h+y),(k-x),25);
putpixel((h+x),(k-y),15);
putpixel((h-x),(k-y),25);
putpixel((h-y),(k-x),15);
putpixel((h-y),(k+x),25);
putpixel((h-x),(k+y),15);
x++;
if(p<0)
p+=((4*x)+6);
else
{
y--;
p+=((4*(x-y))+10);
}
}
while(x<=y);
}
void main(void)
{
int driver=VGA,mode=VGAHI,h,k,r;
6. 6
initgraph(& driver,& mode,"c:tcbgi");
printf("n ENTER THE VALUE OF [H-COORDINATE]: ");
scanf("%d",&h);
printf("n ENTER THE VALUE OF [K-COORDINATE]: ");
scanf("%d",&k);
printf("n ENTER THE VALUE OF THE RADIUS: ");
scanf("%d",&r);
bresenham_circle(320+h,240-k,r);
do
{
r--;
bresenham_circle(320+h,240-k,r);
}while(r!=0);
getche();
}
24. 24
Program 7
WRITE A C PROGRAM TO DRAW A POLYGON AND PERFORM THE FOLLOWING
OPERATIONS:-
ROTATION
TRANSLATION
SCALING.
#include<stdio.h>
#include<graphics.h>
#include<stdlib.h>
#include<conio.h>
#include<math.h>
int *x,*y,i,nin;
float x1,y1,theta;
void drawpolygon(int *x,int *y)
{
int gd=DETECT,gm,ch=0,x1,y1,theta;
//registerbgidriver(EGAVGA_driver);
initgraph(&gd,&gm,"");
for (i=0;i<(nin-1);i++)
line(*(x+i),*(y+i),*(x+i+1),*(y+i+1));
line(*(x+nin-1),*(y+nin-1),*x,*y);
getch();
closegraph();
}
void translate(float x1,float y1)
{
for (i=0;i<nin;i++)
{
*(x+i)+=x1;
*(y+i)+=y1;
}
}
void scale(float x1,float y1)
{
int a,b;
a=*x;
25. 25
b=*y;
translate(-a,-b);
for (i=0;i<nin;i++)
{
*(x+i)*=x1;
*(y+i)*=y1;
}
translate(a,b);
}
void rotate(float theta)
{
int a,b,c,d;
c=*x;
d=*y;
translate(-c,-d);
for (i=0;i<nin;i++)
{
a=(*(x+i)*cos(theta))-(*(y+i)*sin(theta));
b=(*(x+i)*sin(theta))+(*(y+i)*cos(theta));
*(x+i)=a;
*(y+i)=b;
}
translate(c,d);
}
void main(void)
{
int ch;
x=(int *)malloc(sizeof(int)*10);
y=(int *)malloc(sizeof(int)*10);
clrscr();
printf("ENTER NUMBER OF SIDES IN POLYGON : ");
scanf("%d",&nin);
printf("ENTER THE COORDINATES OF THE VERTICES (x,y) :n");
for (i=0;i<nin;i++)
{
printf("(i+1) : ");
scanf("%d%d",&(*(x+i)),&(*(y+i)));
}
drawpolygon(x,y);
while (ch!=4)
{
printf("YOUR OPTIONS :n");
26. 26
printf("1)TRANSLATEn");
printf("2)SCALEn");
printf("3)ROTATEn");
printf("4)EXITn");
printf("nYOUR CHOICE : ");
scanf("%d",&ch);
clrscr();
switch(ch)
{
case 1:printf("TRANSLATION IN X-DIRECTION : ");
scanf("%d",&x1);
printf("TRANSLATION IN Y-DIRECTION : ");
scanf("%d",&y1);
translate(x1,y1);
drawpolygon(x,y);
break;
case 2:printf("SCALING IN X-DIRECTION : ");
scanf("%f",&x1);
printf("SCALING IN Y-DIRECTION : ");
scanf("%f",&y1);
scale(x1,y1);
drawpolygon(x,y);
break;
case 3:printf("ANGLE OF ROTATION(ANTI-CLOCKWISE IS POSITIVE) : ");
scanf("%f",&theta);
theta*=3.1415/180;
rotate(-theta);
drawpolygon(x,y);
break;
case 4:exit(0);
}
}
}
32. 32
Program 9
WRITE A C PROGRAM TO DRAW A SOLID AND FIND ITS VANISHING POINT.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void createsolid();
void main()
{
int gd = DETECT,gm = DETECT;
initgraph(&gd,&gm,"c:tcbgi");
clrscr();
createsolid();
getch();
closegraph();
}
void createsolid()
{
//The coordinates of first rectangle
line(250,200,370,200);
line(370,200,370,320);
line(370,320,250,320);
line(250,200,250,320);
//The coordinates of scond rectangle
line(280,150,400,150);
line(400,150,400,270);
line(400,270,280,270);
line(280,150,280,270);
//The coordinates of the lines
line(250,200,280,150);
line(370,200,400,150);
line(370,320,400,270);
line(250,320,280,270);
//The coordinates of the X,Y and Z axis
line(320,300,600,300);
line(320,50,320,300);
line(320,300,170,450);
//Coordinates for vanishing point
putpixel(190,430,RED);
}