0% found this document useful (0 votes)
4 views38 pages

CG Programs

The document contains multiple C programming experiments focused on various graphics algorithms, including DDA and Bresenham's line drawing algorithms, midpoint circle and ellipse generation, and fill algorithms. It also covers Bezier curves and 2D geometric transformations such as translation, scaling, rotation, and reflection. Additionally, it includes an implementation of the Cohen-Sutherland line clipping algorithm.

Uploaded by

piyushbpatil21
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views38 pages

CG Programs

The document contains multiple C programming experiments focused on various graphics algorithms, including DDA and Bresenham's line drawing algorithms, midpoint circle and ellipse generation, and fill algorithms. It also covers Bezier curves and 2D geometric transformations such as translation, scaling, rotation, and reflection. Additionally, it includes an implementation of the Cohen-Sutherland line clipping algorithm.

Uploaded by

piyushbpatil21
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

//Exp 1: To study and implement DDA line drawing algorithm

# include <stdio.h>

# include <conio.h>

# include <graphics.h>

# include <ctype.h>

# include <math.h>

# include <stdlib.h>

void draw(int x1,int y1,int x2,int y2);

int main(void)

int x1,y1,x2,y2;

int gdriver=DETECT,gmode,gerror;

printf("\n Enter the x and y value for starting point:\n");

scanf("%d%d",&x1,&y1);

printf("\n Enter the x and y value for ending point:\n");

scanf("%d%d",&x2,&y2);

clrscr();

initgraph(&gdriver,&gmode,"E:\\TC\\BGI\\");

draw(x1,y1,x2,y2);

getch();

return 0;

void draw(int x1,int y1,int x2,int y2)

float x,y,xinc,yinc,dx,dy;
int k,step;

dx=x2-x1;

dy=y2-y1;

if(abs(dx)>abs(dy))

step=abs(dx);

else

step=abs(dy);

xinc=dx/step;

yinc=dy/step;

x=x1;

y=y1;

putpixel(abs(x),abs(y),111);

for(k=1;k<=step;k++)

x=x+xinc;

y=y+yinc;

putpixel(abs(x),abs(y),111);

OUTPUT:

Enter the x and y value for starting point: 50 60

Enter the x and y value for ending point: 70 90


//Exp 2: To implement Bresenham’s line drawing algorithm

# include <stdio.h>

# include <conio.h>

# include <graphics.h>

void main( )

int x,y,x1,y1,x2,y2,p,dx,dy;

int gdriver=DETECT,gmode;

initgraph(&gdriver,&gmode,"C:\\tc\\BGI:");

printf("\nEnter the x-coordinate of the first point ::");

scanf("%d",&x1);

printf("\nEnter the y-coordinate of the first point ::");

scanf("%d",&y1);

printf("\nEnter the x-coordinate of the second point ::");

scanf("%d",&x2);

printf("\nEnter the y-coordinate of the second point ::");

scanf("%d",&y2);

x=x1;

y=y1;

dx=x2-x1;

dy=y2-y1;

putpixel(x,y,2);

p=(2dy-dx);

while(x<=x2)

{
if(p<0)

x=x+1;

p=2*x-dx;

else

x=x+1;

y=y+1;

p=p+2*dy;

putpixel(x,y,7);

getch();

closegraph();

OUTPUT:

Enter the x-coordinate of the first point ::40

Enter the y-coordinate of the first point ::50

Enter the x-coordinate of the second point ::60

Enter the y-coordinate of the second point ::80


//Exp 3: To implement midpoint circle generation algorithm

#include<graphics.h>

#include<conio.h>

#include<stdio.h>

void main()

int x,y,x_mid,y_mid,radius,dp;

int g_mode,g_driver=DETECT;

clrscr();

initgraph(&g_driver,&g_mode,"C:\\TURBOC3\\BGI");

printf("*********** MID POINT Circle drawing algorithm ********\n\n");

printf("\nenter the coordinates= ");

scanf("%d %d",&x_mid,&y_mid);

printf("\n now enter the radius =");

scanf("%d",&radius);

x=0;

y=radius;

dp=1-radius;

do

putpixel(x_mid+x,y_mid+y,YELLOW);

putpixel(x_mid+y,y_mid+x,YELLOW);

putpixel(x_mid-y,y_mid+x,YELLOW);

putpixel(x_mid-x,y_mid+y,YELLOW);

putpixel(x_mid-x,y_mid-y,YELLOW);
putpixel(x_mid-y,y_mid-x,YELLOW);

putpixel(x_mid+y,y_mid-x,YELLOW);

putpixel(x_mid+x,y_mid-y,YELLOW);

if(dp<0) {

dp+=(2*x)+1;

else{

y=y-1;

dp+=(2*x)-(2*y)+1;

x=x+1;

}while(y>x);

getch();

OUTPUT:
//Exp 4: To implement midpoint ellipse generation algorithm

#include<conio.h>

#include<stdio.h>

#include<graphics.h>

void main()

int gd=DETECT,gm;

float x,y,xc,yc,rx,ry,pk,pk1;

clrscr();

initgraph(&gd,&gm,"..\\bgi");

printf("Mid point ellipse drawing algorithm\n");

printf("Enter Center for ellipse\nx : ");

scanf("%f",&xc);

printf("y : ");

scanf("%f",&yc);

printf("Enter x-radius and y-radius\nx-radius : ");

scanf("%f",&rx);

printf("y-radius : ");

scanf("%f",&ry);

x=0;

y=ry;

pk=(ry*ry)-(rx*rx*ry)+((rx*rx)/4);

while((2*x*ry*ry)<(2*y*rx*rx))

{
if(pk<=0)

x=x+1;

pk1=pk+(2*ry*ry*x)+(ry*ry);

else

x=x+1;

y=y-1;

pk1=pk+(2*ry*ry*x)-(2*rx*rx*y)+(ry*ry);

pk=pk1;

putpixel(xc+x,yc+y,2);

putpixel(xc-x,yc+y,2);

putpixel(xc+x,yc-y,2);

putpixel(xc-x,yc-y,2);

pk=((x+0.5)*(x+0.5)*ry*ry)+((y-1)*(y-1)*rx*rx)-(rx*rx*ry*ry);

while(y>0)

if(pk>0)

y=y-1;

pk1=pk-(2*rx*rx*y)+(rx*rx);

}
else

x=x+1;

y=y-1;

pk1=pk+(2*ry*ry*x)-(2*rx*rx*y)+(rx*rx);

pk=pk1;

putpixel(xc+x,yc+y,2);

putpixel(xc-x,yc+y,2);

putpixel(xc+x,yc-y,2);

putpixel(xc-x,yc-y,2);

line(xc+rx,yc,xc-rx,yc);

line(xc,yc+ry,xc,yc-ry);

outtextxy(xc+(1.2*rx),yc-(1.2*ry),"(x,y)");

outtextxy(xc-(1.2*rx),yc+(1.2*ry),"(-x,-y)");

outtextxy(xc+(1.2*rx),yc+(1.2*ry),"(x,-y)");

outtextxy(xc-(1.2*rx),yc-(1.2*ry),"(-x,y)");

getch();

OUTPUT:
//Exp 5: To implement Boundary fill algorithm & flood fill algorithm

//Boundary fill algorithm

#include<graphics.h>

#include<stdio.h>

#include<conio.h>

void boundaryFill8(int x, int y, int fill_color,int boundary_color)

if(getpixel(x, y) != boundary_color &&

getpixel(x, y) != fill_color)

putpixel(x, y, fill_color);

boundaryFill8(x + 1, y, fill_color, boundary_color);

boundaryFill8(x, y + 1, fill_color, boundary_color);

boundaryFill8(x - 1, y, fill_color, boundary_color);

boundaryFill8(x, y - 1, fill_color, boundary_color);

boundaryFill8(x - 1, y - 1, fill_color, boundary_color);

boundaryFill8(x - 1, y + 1, fill_color, boundary_color);

boundaryFill8(x + 1, y - 1, fill_color, boundary_color);

boundaryFill8(x + 1, y + 1, fill_color, boundary_color);

void main()

int gd = DETECT, gm;

initgraph(&gd, &gm, "c:\\Turboc3\\bgi");


// Rectangle function

rectangle(50, 50, 100, 100);

// Function calling

boundaryFill8(55, 55, 4, 15);

delay(10000);

getch();

closegraph();

OUTPUT:

//Flood-fill algorithm

#include<stdio.h>

#include<graphics.h>

#include<conio.h>

void floodfill(intx,inty,intold,intnewcol)

{
int current;

current=getpixel(x,y);

if(current==old)

delay(5);

putpixel(x,y,newcol);

floodfill(x+1,y,old,newcol);

floodfill(x-1,y,old,newcol);

floodfill(x,y+1,old,newcol);

floodfill(x,y-1,old,newcol);

floodfill(x+1,y+1,old,newcol);

floodfill(x-1,y+1,old,newcol);

floodfill(x+1,y-1,old,newcol);

floodfill(x-1,y-1,old,newcol);

void main()

intgd=DETECT,gm;

initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");

rectangle(50,50,150,150);

floodfill(70,70,0,15);

getch();

closegraph();

}
OUTPUT:
//Exp 6: To generate a smooth curve by using Bezier curve

#include <stdio.h>

#include <graphics.h>

#include <math.h>

int x[4]={200,300,300,100};

int y[4]={300,400,300,200};

void bezier ()

int i;

double t,xt,yt;

for (t = 0.0; t < 1.0; t += 0.0005)

xt = pow(1-t,3)*x[0]+3*t*pow(1-t,2)*x[1]+3*pow(t,2)*(1-t)*x[2]+pow(t,3)*x[3];

yt = pow(1-t,3)*y[0]+3*t*pow(1-t,2)*y[1]+3*pow(t,2)*(1-t)*y[2]+pow(t,3)*y[3];

putpixel (xt, yt,WHITE);

for (i=0; i<4; i++)

putpixel (x[i], y[i], WHITE);

getch();

closegraph();

void main()

int gd = DETECT, gm;

initgraph (&gd, &gm, "C:\\TURBOC3\\bgi");


bezier ();

OUTPUT:
//Exp 7: To apply the basic 2D transformations such as translation, Scaling, Rotation, shearing
and reflection for a given 2D object

#include <graphics.h>

#include <stdio.h>

#include <conio.h>

#include <math.h>

float x1, y1, x2, y2, x3, y3, x, y, rx1, ry1, rx2, ry2, rx3, ry3, t, vx1, vy1, vx2, vy2, vx3, vy3;

float w1 = 5, w2 = 5, w3 = 635, w4 = 465, v1 = 425, v2 = 75, v3 = 550, v4 = 250;

int gd,gm,ch;

void original ();

void triangle (float, float, float, float, float, float);

void rotate (float, float, float);

void main ()

clrscr ();

printf("\n\t\t ***** 2D Geometric Transformations *****");

printf("\n\n Enter the coordinates of triangle (x1,y1,x2,y2,x3, y3): \n");

scanf(“%f %f %f %f %f %f”,&x1,&y1,& x2,& y2,&x3,&y3);

original ();

closegraph ();

do

printf( "\n\n Choose any one Transformation : ");

printf("\n\t [Link] \n\t [Link] \n\t [Link] \n\t [Link]");

printf("\n\t [Link]”);
printf("\n\n Enter your choice : \t");

scanf(“%d”,&ch);

switch (ch)

case 1:

printf("\n Enter the translation factors (x,y): \t\t");

printf(“%f %f”,&x,&y);

original ();

triangle (x1+x, y1+y, x2+x, y2+y, x3+x, y3+y);

closegraph ();

break;

case 2:

printf( "\n Enter the angle of rotation : \t\t");

scanf(“%f”,&t);

printf("\n Enter the reference point of rotation (rx,ry) : \t");

scanf(“%f %f”,&x,&y);

original ();

rotate (t, x, y);

closegraph ();

break;

case 3:

printf("\n Enter the scaling factors (x,y): \t\t");

scanf(“%f %f”,&x,&y);

original ();

triangle (x1*x, y1*y, x2*x, y2*y, x3*x, y3*y);


closegraph ();

break;

case 4:

original ();

rotate (180, x2, y2);

closegraph ();

break;

case 5:

printf("\n Enter the Shear Value : \t\t");

scanf(“%f”,&x);

original ();

triangle (x1, y1, x2+x, y2, x3, y3);

closegraph ();

break;

} while (ch<= 5);

getch ();

void original ( )

initgraph (&gd, &gm, "C:\\TC\\BGI");

triangle (x1, y1, x2, y2, x3, y3);

void triangle (float x1, float y1, float x2, float y2, float x3, float y3)

{
line (x1, y1, x2, y2);

line (x2, y2, x3, y3);

line (x3, y3, x1, y1);

getch ();

void rotate (float t, float rx, float ry)

t = t * (3.14 / 180);

rx1 = rx + (x1 - rx) * cos (t) - (y1 - ry) * sin (t);

ry1 = ry + (x1 - rx) * sin (t) + (y1 - ry) * cos (t);

rx2 = rx + (x2 - rx) * cos (t) - (y2 - ry) * sin (t);

ry2 = ry + (x2 - rx) * sin (t) + (y2 - ry) * cos (t);

rx3 = rx + (x3 - rx) * cos (t) - (y3 - ry) * sin (t);

ry3 = ry + (x3 - rx) * sin (t) + (y3 - ry) * cos (t);

triangle (rx1, ry1, rx2, ry2, rx3, ry3);

OUTPUT:
//Exp 8: Program for Cohen Sutherland Line clipping algorithm in C

#include<graphics.h>

#include<iostream.h>

#include<conio.h>

float x1,y1,x2,y2,wx1,wy1,wx2,wy2,m;

int gd,gm;

void main()

clrscr();

cout<<"\nEnter the clip window coordinates : ";

cin>>wx1>>wy1>>wx2>>wy2;

cout<<"\nEnter the line end points : ";

cin>>x1>>y1>>x2>>y2;

m=(y2-y1)/(x2-x1);

initgraph(&gd,&gm,"C:\\TC\\BGI");

cout<<"\n\n\t\t\t\tBefore Clipping";

rectangle(wx1,wy1,wx2,wy2);

line(x1,y1,x2,y2);

getch();

closegraph();

if(x1<wx1)

y1=y1+(wx1-x1)*m;

x1=wx1;

}
if(x2>wx2)

y2=y2-(x2-wx2)*m;

x2=wx2;

if(y2>wy2)

x2=x2-((y2-wy2)/m);

y2=wy2;

if(y1<wy1)

x1=x1+(wy1-y1)/m;

y1=wy1;

if(x2<wx1)

y2=y2+(wx1-x2)*m;

x2=wx1;

if(x1>wx2)

y1=y1-(x1-wx2)*m;

x1=wx2;

}
if(y2<wy1)

x2=x2+((wy1-y2)/m);

y2=wy1;

if(y1>wy2)

x1=x1-((y1-wy2)/m);

y1=wy2;

initgraph(&gd,&gm,"C:\\TC\\BGI");

cout<<"\n\n\t\t\t\tAfter Clipping";

rectangle(wx1,wy1,wx2,wy2);

line(x1,y1,x2,y2);

getch();

OUTPUT:

Enter the clip window coordinates: 200 200 400 400

Enter the line end points: 150 150 350 500


//Exp 9: Program for Sutherland-Hodgman polygon clipping algorithm

#include<stdio.h>

#include<graphics.h>

#include<conio.h>

#include<stdlib.h>

int main()

int gd,gm,n,*x,i,k=0;

//window coordinates int


wx1=220,wy1=140,wx2=420,wy2=140,wx3=420,wy3=340,wx4=220,wy4=340;

int w[]={220,140,420,140,420,340,220,340,220,140};//array for drawing window

detectgraph(&gd,&gm);

initgraph(&gd,&gm,"c:\\turboc3\\bgi"); //initializing graphics

printf("Window:-");

setcolor(RED); //red colored window

drawpoly(5,w); //window drawn

printf("Enter the no. of vertices of polygon: ");

scanf("%d",&n);

x = malloc(n*2+1);

printf("Enter the coordinates of points:\n");

k=0;

for(i=0;i<n*2;i+=2) //reading vertices of polygon

printf("(x%d,y%d): ",k,k);

scanf("%d,%d",&x[i],&x[i+1]);
k++;

x[n*2]=x[0]; //assigning the coordinates of first vertex to last additional vertex for drawpoly
method.

x[n*2+1]=x[1];

setcolor(WHITE);

drawpoly(n+1,x);

printf("\nPress a button to clip a polygon..");

getch();

setcolor(RED);

drawpoly(5,w);

setfillstyle(SOLID_FILL,BLACK);

floodfill(2,2,RED);

gotoxy(1,1); //bringing cursor at starting position

printf("\nThis is the clipped polygon..");

getch();

cleardevice();

closegraph();

return 0;

OUTPUT:
//Exp 10: Implementation of Perspective and parallel projection of 3d Object

#include<graphics.h>

#include<iostream.h>

#include<conio.h>

int gd,gm,x1,y1,x2,y2,dep,ch;

void main()

cout<<"\n Enter the TOP-LEFT and BOTTOM-RIGHT CORNER:";

cin>>x1>>y1>>x2>>y2;

cout<<"\n Enter the depth along z axis:";

cin>>dep;

do

cout<<"Choose any one projection:\n\[Link] Projection\n\[Link] Projection\nEnter


your choice:";

cin>>ch;

initgraph(&gd,&gm,"C:\\TC\\BGI");

switch(ch)

case 1:

rectangle(x2+100,y1,x2+100+dep,y2);

outtextxy(x2+100,y1-10,"SIDE VIEW");

rectangle(x1,y1,x2,y2);

outtextxy(x1,y1-10,"FRONT VIEW");

rectangle(x1,y1-(y2-y1),x2,x1+dep-(y2-y1));
outtextxy(x1,y1-(y2-y1)-10,"TOP VIEW");

getch();

closegraph();

break;

case 2:

bar3d(x1,y1,x2,y2,dep,1);

getch();

closegraph();

break;

}while(ch<3);

OUTPUT:

You might also like