*C program for creating two dimensional shape of house, car, fish, man using lines, circles etc.. *
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int house[]={100,50,140,110,60,110,100,50};
int house2[]={100,50,270,50,330,110,110,110};
int fish[]={560,100,600,80,600,130,560,100};
int car[]={310,400,390,400,390,360,340,360,280,310,120,310,70,360,20,360,20,400,100,400};
int mirror1[]={90,360,125,325,185,325,185,360,90,360};
int mirror2[]={320,360,275,325,220,325,220,360,320,360};
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
drawpoly(4,house);
drawpoly(4,house2);
rectangle(60,110,140,250);
rectangle(140,110,330,250);
rectangle(155,130,190,160);
rectangle(200,180,270,250);
rectangle(280,130,315,160);
ellipse(480,100,0,360,80,30);
arc(370,110,350,25,85);
circle(420,100,5);
drawpoly(4,fish);
circle(500,200,40);
line(500,240,500,330);
line(500,260,450,290);
line(500,260,550,290);
line(500,330,450,360);
line(500,330,550,360);
drawpoly(10,car);
drawpoly(5,mirror2);
drawpoly(5,mirror1);
arc(125,400,0,180,25);
arc(285,400,0,180,25);
line(150,400,260,400);
circle(125,400,17);
circle(285,400,17);
getch();
closegraph();
}
Output:-
*C program for implementing DDA Line Drawing Algorithm:-*
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int count=1;
int gd=DETECT,gm;
int delay_time,x,y,dx,dy,increment;
int x1,y1,x2,y2;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("Enter Initial Line Coordinate:");
printf("\nEnter the value of x1:");
scanf("%d",&x1);
printf("Enter the value of y1:");
scanf("%d",&y1);
printf("\nEnter the value of x2:");
scanf("%d",&x2);
printf("Enter the value of y2:");
scanf("%d",&y2);
printf("Enter delay time:");
scanf("%d",&delay_time);
dx=abs(x2-x1);
dy=abs(y2-y1);
if(dx>=dy)
{
increment=dx;
}
else
{
increment=dy;
}
dx=dx/increment;
dy=dy/increment;
x=x1;
y=y1;
for(count=1;count<=increment;count++)
{
putpixel(x,y,WHITE);
x=x+dx;
y=y+dy;
delay(delay_time);
}
getch();
closegraph();
}
Output:-
*C program to display different types of lines like dotted,solid,dashed etc...:-*
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
int c,x,y;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("Enter the initial coordinate..");
printf("\nEnter x1:");
scanf("%d",&x);
printf("\nEnter y1:");
scanf("%d",&y);
for(c=0;c<4;c++)
{
setlinestyle(c,0,1);
line(x,y,x+200,y+100);
y=y+50;
}
getch();
closegraph();
}
Output:-
*C program to implement bresenham's line drawing algorithm-*
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
int dx,dy,p,x,y,x1,y1,x2,y2,delay_time;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("Enter the x1:");
scanf("%d",&x1);
printf("\nEnter the y1:");
scanf("%d",&y1);
printf("\nEnter the x2:");
scanf("%d",&x2);
printf("\nEnter the y2:");
scanf("%d",&y2);
printf("\nEnter the delay:");
scanf("%d",&delay_time);
dx=x2-x1;
dy=y2-y1;
p=2*(dy)-(dx);
x=x1;
y=y1;
while(x<x2)
{
if(p<=0)
{
x=x+1;
y=y;
p=p+2*(dy);
}
else
{
x=x+1;
y=y+1;
p=p+2*(dy-dx);
}
putpixel(x,y,WHITE);
delay(delay_time);
}
getch();
closegraph();
}
Output:-
*C program to implement bresenham's circle drawing algorithm-*
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void plot(int ,int ,int ,int);
void main()
{
int gd=DETECT,gm;
int x,y,p,xc,yc,r,delay_time;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("Enter x:");
scanf("%d",&xc);
printf("Enter y:");
scanf("%d",&yc);
printf("Enter radius:");
scanf("%d",&r);
x=0;
y=r;
p=1-r;
plot(xc,yc,x,y);
while(x<y)
{
x++;
if(p<0)
{
p=p+2*x+1;
}
else
{
y--;
p=p+2*(x-y)+1;
}
plot(xc,yc,x,y);
// delay(30);
}
getch();
closegraph();
}
void plot(int xc,int yc,int x,int y)
{
putpixel(xc+x,yc+y,WHITE);
putpixel(xc-x,yc+y,WHITE);
putpixel(xc+x,yc-y,WHITE);
putpixel(xc-x,yc-y,WHITE);
putpixel(xc+y,yc+x,WHITE);
putpixel(xc-y,yc+x,WHITE);
putpixel(xc+y,yc-x,WHITE);
putpixel(xc-y,yc-x,WHITE);
}
Output:-
*C program to perform a tranformation of a triangle.
(a)Rotating 45 degree about the origin and then translating one unit in x and y direction.
(b) Shift the image up by 2 units:-*
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
void triangle(int x1,int y1,int x2,int y2,int x3,int y3);
void rotate(int x1,int y1,int x2,int y2,int x3,int y3);
void translate(int x1,int y1,int x2,int y2,int x3,int y3);
void translate1(int x1,int y1,int x2,int y2,int x3,int y3);
void main()
{
int gd=DETECT,gm;
int x1,y1,x2,y2,x3,y3;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("Enter the x1 and y1 of triangle:");
scanf("%d %d",&x1,&y1);
printf("\nEnter the x2 and y2 of triangle:");
scanf("%d %d",&x2,&y2);
printf("\nEnter the x3 and y3 of triangle:");
scanf("%d %d",&x3,&y3);
triangle(x1,y1,x2,y2,x3,y3);
getch();
cleardevice();
rotate(x1,y1,x2,y2,x3,y3);
getch();
cleardevice();
triangle(x1,y1,x2,y2,x3,y3);
translate(x1,y1,x2,y2,x3,y3);
setcolor(WHITE);
translate1(x1,y1,x2,y2,x3,y3);
setcolor(WHITE);
triangle(x1,y1,x2,y2,x3,y3);
getch();
}
void triangle(int x1,int y1,int x2,int y2,int x3,int y3)
{
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
}
void rotate(int x1,int y1,int x2,int y2,int x3,int y3)
{
int x,y,c1,s1,c2,s2,c3,s3,p=x2,q=x2;
float angle;
printf("\nEnter the angle for rotation:");
scanf("%d",&angle);
cleardevice();
angle=(angle*3.14)/180;
c1=p+(x1-p)*cos(angle)-(y1-q)*sin(angle);
s1=q+(x1-p)*sin(angle)-(y1-q)*cos(angle);
c2=p+(x2-p)*cos(angle)-(y2-q)*sin(angle);
s2=q+(x2-p)*sin(angle)-(y2-q)*cos(angle);
c3=p+(x3-p)*cos(angle)-(y3-q)*sin(angle);
s3=q+(x3-p)*sin(angle)-(y3-q)*cos(angle);
printf("Rotation");
triangle(c1,s1,c2,s2,c3,s3);
}
void translate(int x1,int y1,int x2,int y2,int x3,int y3)
{
int x,y,s1,s2,s3,e1,e2,e3;
printf("\nEnter the translation unit:");
scanf("%d %d",&x,&y);
s1=x1+x;
e1=y1+y;
s2=x2+x;
e2=y2+y;
s3=x3+x;
e3=y3+y;
printf("\nAfter Translate");
triangle(s1,e1,s2,e2,s3,e3);
getch();
cleardevice();
}
void translate1(int x1,int y1,int x2,int y2,int x3,int y3)
{
int x,y,s1,s2,s3,e1,e2,e3;
printf("\nEnter the shifting units:");
scanf("%d %d",&x,&y);
cleardevice();
s1=x1+x;
e1=y1+y;
s2=x2+x;
e2=y2+y;
s3=x3+x;
e3=y3+y;
printf("After shifting..");
triangle(s1,e1,s2,e2,s3,e3);
}
Output:-
*C program to implement cohen sutherland line clipping algorithm-*
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT, gm;
float i,xmax,ymax,xmin,ymin,x1,y1,x2,y2,m;
float start[4],end[4],code[4];
clrscr();
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("\n\tPlease enter the bottom left co-ordinate of viewport: ");
scanf("%f %f",&xmin,&ymin);
printf("\n\tPlease enter the top right co-ordinate of viewport: ");
scanf("%f %f",&xmax,&ymax);
printf("\nPlease enter the co-ordinates for starting point of line: ");
scanf("%f %f",&x1,&y1);
printf("\nPlease enter the co-ordinates for ending point of line: ");
scanf("%f %f",&x2,&y2);
for(i=0;i <4;i++)
{
start[i]=0;
end[i]=0;
}
m=(y2-y1)/(x2-x1);
if(x1 <xmin) start[0]=1;
if(x1 >xmax) start[1]=1;
if(y1 >ymax) start[2]=1;
if(y1 <ymin) start[3]=1;
if(x2 <xmin) end[0]=1;
if(x2 >xmax) end[1]=1;
if(y2 >ymax) end[2]=1;
if(y2 <ymin) end[3]=1;
for(i=0;i <4;i++)
code[i]=start[i]&&end[i];
if((code[0]==0)&&(code[1]==0)&&(code[2]==0)&&(code[3]==0))
{
if((start[0]==0)&&(start[1]==0)&&(start[2]==0)&&(start[3]==0)&&(end[0]==0)&& (end[1]==0)&&(end[2]==0)&&(end[3]==0))
{
cleardevice();
printf("\n\t\tThe line is totally visible\n\t\tand not a clipping candidate");
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2);
getch();
}
else
{
cleardevice();
printf("\n\t\tLine is partially visible");
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2);
getch();
if((start[2]==0)&&(start[3]==1))
{
x1=x1+(ymin-y1)/m;
y1=ymin;
}
if((end[2]==0)&&(end[3]==1))
{
x2=x2+(ymin-y2)/m;
y2=ymin;
}
if((start[2]==1)&&(start[3]==0))
{
x1=x1+(ymax-y1)/m;
y1=ymax;
}
if((end[2]==1)&&(end[3]==0))
{
x2=x2+(ymax-y2)/m;
y2=ymax;
}
if((start[1]==0)&&(start[0]==1))
{
y1=y1+m*(xmin-x1);
x1=xmin;
}
if((end[1]==0)&&(end[0]==1))
{
y2=y2+m*(xmin-x2);
x2=xmin;
}
if((start[1]==1)&&(start[0]==0))
{
y1=y1+m*(xmax-x1);
x1=xmax;
}
if((end[1]==1)&&(end[0]==0))
{
y2=y2+m*(xmax-x2);
x2=xmax;
}
clrscr();
cleardevice();
printf("\n\t\tAfter clippling:");
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2);
getch();
}
}
else
{
clrscr();
cleardevice();
printf("\nLine is invisible");
rectangle(xmin,ymin,xmax,ymax);
}
getch();
closegraph();
}
Output:-
*C program to implement polygon clipping using Sutherland-Hodgeman polygon clipping:-*
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#define round(a) ((int)(a+0.5))
int k;
float xmin,ymin,xmax,ymax,arr[20],m;
void clipl(float x1,float y1,float x2,float y2)
{
if(x2-x1)
m=(y2-y1)/(x2-x1);
if(x1>=xmin && x2>=xmin)
{
arr[k]=x2;
arr[k+1]=y2;
k+=2;
}
if(x1<xmin && x2>=xmin)
{
arr[k]=xmin;
arr[k+1]=y1+m*(xmin-x1);
arr[k+2]=x2;
arr[k+3]=y2;
k+=4;
}
if(x1>=xmin && x2<xmin)
{
arr[k]=xmin;
arr[k+1]=y1+m*(xmin-x1);
k+=2;
}
}
void clipt(float x1,float y1,float x2,float y2)
{
if(y2-y1)
m=(x2-x1)/(y2-y1);
if(y1<=ymax && y2<=ymax)
{
arr[k]=x2;
arr[k+1]=y2;
k+=2;
}
if(y1>ymax && y2<=ymax)
{
arr[k]=x1+m*(ymax-y1);
arr[k+1]=ymax;
arr[k+2]=x2;
arr[k+3]=y2;
k+=4;
}
if(y1<=ymax && y2>ymax)
{
arr[k]=x1+m*(ymax-y1);
arr[k+1]=ymax;
k+=2;
}
}
void clipr(float x1,float y1,float x2,float y2)
{
if(x2-x1)
m=(y2-y1)/(x2-x1);
if(x1<=xmax && x2<=xmax)
{
arr[k]=x2;
arr[k+1]=y2;
k+=2;
}
if(x1>xmax && x2<=xmax)
{
arr[k]=xmax;
arr[k+1]=y1+m*(xmax-x1);
arr[k+2]=x2;
arr[k+3]=y2;
k+=4;
}
if(x1<=xmax && x2>xmax)
{
arr[k]=xmax;
arr[k+1]=y1+m*(xmax-x1);
k+=2;
}
}
void clipb(float x1,float y1,float x2,float y2)
{
if(y2-y1)
m=(x2-x1)/(y2-y1);
if(y1>=ymin && y2>=ymin)
{
arr[k]=x2;
arr[k+1]=y2;
k+=2;
}
if(y1<ymin && y2>=ymin)
{
arr[k]=x1+m*(ymin-y1);
arr[k+1]=ymin;
arr[k+2]=x2;
arr[k+3]=y2;
k+=4;
}
if(y1>=ymin && y2<ymin)
{
arr[k]=x1+m*(ymin-y1);
arr[k+1]=ymin;
k+=2;
}
}
void main()
{
int gdriver=DETECT,gmode,n,poly[20],i;
float xi,yi,xf,yf,polyy[20];
clrscr();
printf("Coordinates of rectangular clip window :\nxmin,ymin:");
scanf("%f%f",&xmin,&ymin);
printf("Coordinates of rectangular clip window :\nxmax,ymax:");
scanf("%f%f",&xmax,&ymax);
printf("\n\nPolygon to be clipped :\nNumber of sides :");
scanf("%d",&n);
printf("Enter the coordinates :");
for (i=0;i<2*n;i++)
scanf("%f",&polyy[i]);
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
for(i=0;i<2*n+2;i++)
poly[i]=round(polyy[i]);
initgraph(&gdriver,&gmode,"C:\\TURBOC3\\BGI");
rectangle(xmin,ymax,xmax,ymin);
printf("\tUNCLIPPED POLYGON");
fillpoly(n,poly);
getch();
cleardevice();
k=0;
for(i=0;i<2*n;i+=2)
clipl(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
n=k/2;
for(i=0;i<k;i++)
polyy[i]=arr[i];
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
k=0;
for(i=0;i<2*n;i+=2)
clipt(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
n=k/2;
for(i=0;i<k;i++)
polyy[i]=arr[i];
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
k=0;
for(i=0;i<2*n;i+=2)
clipr(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
n=k/2;
for(i=0;i<k;i++)
polyy[i]=arr[i];
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
k=0;
for(i=0;i<2*n;i+=2)
clipb(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
for(i=0;i<k;i++)
poly[i]=round(arr[i]);
if(k)
fillpoly(k/2,poly);
rectangle(xmin,ymax,xmax,ymin);
printf("\tCLIPPED POLYGON");
getch();
closegraph();
}
Output:-
*C program to implement boundary fill & flood fill algorithm to fill a polygon:-*
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void b_fill(int x,int y,int f,int b)
{
int c;
c=getpixel(x,y);
if((c!=b)&&(c!=f))
{
putpixel(x,y,f);
delay(5);
b_fill(x+1,y,f,b);
b_fill(x,y+1,f,b);
b_fill(x+1,y+1,f,b);
b_fill(x-1,y-1,f,b);
b_fill(x-1,y,f,b);
b_fill(x,y-1,f,b);
b_fill(x-1,y+1,f,b);
b_fill(x+1,y-1,f,b);
}
}
void flodfill(int x,int y,int f,int o)
{
int c;
c=getpixel(x,y);
if(c==o)
{
setcolor(f);
putpixel (x,y,f);
delay(5);
flodfill(x+1,y,f,o);
flodfill(x,y+1,f,o);
flodfill(x+1,y+1,f,o);
flodfill(x-1,y-1,f,o);
flodfill(x-1,y,f,o);
flodfill(x,y-1,f,o);
flodfill(x-1,y+1,f,o);
flodfill(x+1,y-1,f,o);
}
}
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("Boundary Fill Algorithm\n");
rectangle(50,50,100,100);
b_fill(55,55,WHITE,10);
getch();
cleardevice();
printf("Flood Fill Algoritm\n");
rectangle(50,50,100,100);
flodfill(51,51,WHITE,0);
getch();
}
Output:-
No comments:
Post a Comment