0% found this document useful (0 votes)
9 views25 pages

Aman-Cg File

This document is a practical file for a Computer Graphics course, detailing various programming experiments conducted by Aman Kumar. It includes source code for drawing lines and circles using different algorithms, implementing clipping techniques, and performing transformations such as translation and scaling. The file is structured with an index and individual sections for each experiment, showcasing the practical applications of computer graphics concepts.

Uploaded by

sb10092003
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)
9 views25 pages

Aman-Cg File

This document is a practical file for a Computer Graphics course, detailing various programming experiments conducted by Aman Kumar. It includes source code for drawing lines and circles using different algorithms, implementing clipping techniques, and performing transformations such as translation and scaling. The file is structured with an index and individual sections for each experiment, showcasing the practical applications of computer graphics concepts.

Uploaded by

sb10092003
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
You are on page 1/ 25

PRACTICAL FILE

OF
“COMPUTER GRAPHICS”

Submitted By:- Submitted To:-


AMAN KUMAR Er. Navneet Kaur
(1221163) – (2021220015) (Asst. Prof.)
BTECH -CSE-8TH SEM Deptt. Of CSE

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


SETH JAI PARKASH MUKAND LAL INSTITUTE OF ENGINEERING AND
TECHNOLOGY
RADAUR-135133 (YNR)

SESSION - 2022-25

(Affiliated To Kurukshetra University,Kurukshetra ,Haryana,India)

AMAN KUMAR(1221163) 1|Page


INDEX

S.No. Experiment Page-No. Date Remarks

To draw a line using simple DDA


1. 3-4
algorithm

To draw a line using Bresenham’s


2. 5-6
Line algorithm

To draw a circle using Bresenham’s


3. 7-8
Circle algorithm

To draw a circle using mid point


4. 9-10
circle algorithm

5. To implement Point Clipping 11-12

To implement line clipping using


6. Cohen-Sutherland line clipping 13-16
algo.

7. To Translate an object 17-19

8. To Scale any object 20-21

9. To rotate any object 22-23

To implement Boundary fill


10. 23-25
algorithm

AMAN KUMAR(1221163) 2|Page


PRACTICAL -01

Aim : Program To draw a line using simple DDA algorithm.

Source Code:-

#include <graphics.h>
#include <stdio.h>
#include <math.h>
#include <dos.h>

void main( )
{
float x,y,x1,y1,x2,y2,dx,dy,step;
int i,gd=DETECT,gm;

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

printf("Enter the value of x1 and y1 : ");


scanf("%f%f",&x1,&y1);
printf("Enter the value of x2 and y2: ");
scanf("%f%f",&x2,&y2);

dx=abs(x2-x1);
dy=abs(y2-y1);

if(dx>=dy)
step=dx;
else
step=dy;

dx=dx/step;
dy=dy/step;

x=x1;
y=y1;

i=1;
while(i<=step)
{
putpixel(x,y,5);
x=x+dx;
y=y+dy;
i=i+1;
delay(100);
}
getch();
closegraph();
}

AMAN KUMAR(1221163) 3|Page


AMAN KUMAR(1221163) 4|Page
PRACTICAL -02

Aim : Program To draw a line using Bresenham’s Line algorithm.

Source Code :

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
int x,y,x1,x2,y1,y2,dx,dy,p;
initgraph( &gd, &gm, "");
cleardevice();
printf("Enter the co-ordinates of line : ");
printf("\nStarting point : ");
scanf("%d %d",&x1,&y1);
printf("Ending point : ");
scanf("%d %d",&x2,&y2);
dx=x2-x1;
dy=y2-y1;
p=2*dy-dx;
x=x1;
y=y1;
putpixel(x,y,4);
while(x<x2)
{
if(p<0)
{
x++;
p=p+2*dy;
}
else
{
x++;
y++;
p=p+2*dy-2*dx;
}
putpixel(x,y,4);
}
getch();
closegraph();

AMAN KUMAR(1221163) 5|Page


AMAN KUMAR(1221163) 6|Page
PRACTICAL -03

Aim : Program To draw a circle using Bresenham’s Circle algorithm

Source Code :

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
int x,y,p,q,d,r;
initgraph( &gd, &gm, "c:\\turboc3\\bgi");
cleardevice();
printf("Enter the center of circle :\n");
printf(" p : ");
scanf("%d",&p);
printf(" q : ");
scanf("%d",&q);
printf("Enter radius : ");
scanf("%d",&r);
d=3-2*r;
x=0;
y=r;
while(x<=y)
{
putpixel(p+x,q+y,4);
putpixel(p+y,q+x,4);
putpixel(p-y,q+x,4);
putpixel(p-x,q+y,4);
putpixel(p-x,q-y,4);
putpixel(p-y,q-x,4);
putpixel(p+y,q-x,4);
putpixel(p+x,q-y,4);
if(d<0)
d+=4*x+6;
else
{
d+=4*(x-y)+10;
y--;
}
x++;
}
getch();

AMAN KUMAR(1221163) 7|Page


AMAN KUMAR(1221163) 8|Page
PRACTICAL -04

Aim : Program To draw a circle using mid point circle algorithm

Source Code :

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
int x,y,p,q,d,r;
initgraph( &gd, &gm, "");
cleardevice();
printf("Enter the center of circle :\n");
printf(" p : ");
scanf("%d",&p);
printf(" q : ");
scanf("%d",&q);
printf("Enter radius : ");
scanf("%d",&r);
d=1-r;
x=0;
y=r;
while(x<=y)
{
putpixel(p+x,q+y,BLUE);
putpixel(p+y,q+x,GREEN);
putpixel(p-y,q+x,RED);
putpixel(p-x,q+y,YELLOW);
putpixel(p-x,q-y,12);
putpixel(p-y,q-x,14);
putpixel(p+y,q-x,15);
putpixel(p+x,q-y,6);
if(d<0)
d+=2*(x+1)+1;
else
{
d+=2*(x-y)+5;
y--;
}
x++;
}
getch();

AMAN KUMAR(1221163) 9|Page


AMAN KUMAR(1221163) 10 |
Page
PRACTICAL -05

Aim : Program To implement Point Clipping

Source Code :

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm,x,y,minx=200,miny=150,maxx=400,maxy=300;
initgraph( &gd, &gm, "c:\\turboc3\\bgi");
cleardevice();
rectangle(minx,miny,maxx,maxy);
printf("Enter the co-ordinates of point : ");
scanf("%d %d",&x,&y);
if(x<maxx&&x>minx)
{
if(y<maxy&&y>miny)
{
printf("Point is inside the clipping window");
putpixel(x,y,15);
circle(x,y,2);
}
}
else
printf("Point is outside the clipping window");
getch();
closegraph();
}

AMAN KUMAR(1221163) 11 |
Page
AMAN KUMAR(1221163) 12 |
Page
PRACTICAL -06

Aim : Program To implement line clipping using Cohen-Sutherland line clipping


algo.

Source Code :
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>
void main()
{
int rcode_begin[4]={0,0,0,0},rcode_end[4]={0,0,0,0},region_code[4];
int W_xmax,W_ymax,W_xmin,W_ymin,flag=0;
float slope;
int x,y,x1,y1,i, xc,yc;
int gr=DETECT,gm;
initgraph(&gr,&gm,"c:\\turboc3\\bgi");
printf("\n Now, enter XMin, YMin =");

scanf("%d %d",&W_xmin,&W_ymin);
printf("\n First enter XMax, YMax =");
scanf("%d %d",&W_xmax,&W_ymax);
printf("\n Please enter intial point x and y= ");
scanf("%d %d",&x,&y);
printf("\n Now, enter final point x1 and y1= ");
scanf("%d %d",&x1,&y1);
cleardevice();
rectangle(W_xmin,W_ymin,W_xmax,W_ymax);
line(x,y,x1,y1);
line(0,0,600,0);
line(0,0,0,600);
if(y>W_ymax) {
rcode_begin[0]=1;
flag=1 ;
}
if(y<W_ymin) {
rcode_begin[1]=1;
flag=1;
}
if(x>W_xmax) {
rcode_begin[2]=1;
flag=1;
}
if(x<W_xmin) {
rcode_begin[3]=1;
flag=1;
AMAN KUMAR(1221163) 13 |
Page
}
if(y1>W_ymax){
rcode_end[0]=1;
flag=1;
}
if(y1<W_ymin) {
rcode_end[1]=1;
flag=1;
}
if(x1>W_xmax){
rcode_end[2]=1;
flag=1;
}
if(x1<W_xmin){
rcode_end[3]=1;
flag=1;
}
if(flag==0)
{
printf("No need of clipping as it is already in window");
}
flag=1;
for(i=0;i<4;i++){
region_code[i]= rcode_begin[i] && rcode_end[i] ;
if(region_code[i]==1)
flag=0;
}
if(flag==0)
{
printf("\n Line is completely outside the window");
}
else{
slope=(float)(y1-y)/(x1-x);
if(rcode_begin[2]==0 && rcode_begin[3]==1) //left
{
y=y+(float) (W_xmin-x)*slope ;
x=W_xmin;
}
if(rcode_begin[2]==1 && rcode_begin[3]==0) // right
{
y=y+(float) (W_xmax-x)*slope ;
x=W_xmax;
}
if(rcode_begin[0]==1 && rcode_begin[1]==0) // top
{
x=x+(float) (W_ymax-y)/slope ;
y=W_ymax;

AMAN KUMAR(1221163) 14 |
Page
if(rcode_begin[0]==0 && rcode_begin[1]==1)
{
x=x+(float) (W_ymin-y)/slope ;
y=W_ymin;
}
if(rcode_end[2]==0 && rcode_end[3]==1)
{
y1=y1+(float) (W_xmin-x1)*slope ;
x1=W_xmin;
}
if(rcode_end[2]==1 && rcode_end[3]==0)
{
y1=y1+(float) (W_xmax-x1)*slope ;
x1=W_xmax;
}
if(rcode_end[0]==1 && rcode_end[1]==0)
{
x1=x1+(float) (W_ymax-y1)/slope ;
y1=W_ymax;
}
if(rcode_end[0]==0 && rcode_end[1]==1)
{
x1=x1+(float) (W_ymin-y1)/slope ;
y1=W_ymin;
}
}
delay(1000);
clearviewport();
rectangle(W_xmin,W_ymin,W_xmax,W_ymax);
line(0,0,600,0);
line(0,0,0,600);
setcolor(RED);
line(x,y,x1,y1);
getch();
closegraph();
}

AMAN KUMAR(1221163) 15 |
Page
AMAN KUMAR(1221163) 16 |
Page
PRACTICAL -07

Aim : Program To translate any object

Source Code :

#include<stdio.h>
#include<conio.h>
#include<graphics.h>

void x_translate(int, int, int, int, int);


void y_translate(int, int, int, int, int);
void translate(int, int, int, int, int);

void main()
{
int gd=DETECT,gm;
int x1=50,y1=50,x2=150,y2=100,T,choice;

initgraph(&gd,&gm,"c:\\turboc3\\bgi");
outtext("Translation of Object.");
outtextxy(50,30,"Press any key to continue");
rectangle(x1,y1,x2,y2); //original shape
getch();
closegraph();

printf("Enter Your Choice :");


printf("\n1. Translate Horizontally");
printf("\n2. Translate Vertically");
printf("\n3. Translate Diagonally]\n\n");
scanf("%d",&choice);
printf("\n\nEnter the translation factor : ");
scanf("%d",&T);

switch(choice) //calling the functions according to the choice


{
case 1: x_translate(x1,y1,x2,y2,T);
break;
case 2: y_translate(x1,y1,x2,y2,T);
break;
case 3: translate(x1,y1,x2,y2,T);
break;
default: printf("\nInvalid Choice!");
}
getch();
closegraph();
}

AMAN KUMAR(1221163) 17 |
Page
void x_translate(int x1, int y1, int x2, int y2, int T)
{
int gd=DETECT,gm;

initgraph(&gd,&gm,"c:\\turboc3\\bgi");
rectangle(x1,y1,x2,y2);
x1=x1+T;
x2=x2+T;
setcolor(RED);
rectangle(x1,y1,x2,y2);
}

void y_translate(int x1, int y1, int x2, int y2, int T)
{
int gd=DETECT,gm;

initgraph(&gd,&gm,"c:\\turboc3\\bgi");
rectangle(x1,y1,x2,y2);
y1=y1+T;
y2=y2+T;
setcolor(RED);
rectangle(x1,y1,x2,y2);
}

void translate(int x1, int y1, int x2, int y2, int T)
{
int gd=DETECT,gm;

initgraph(&gd,&gm,"c:\\turboc3\\bgi");
rectangle(x1,y1,x2,y2);
x1=x1+T;
x2=x2+T;
y1=y1+T;
y2=y2+T;
setcolor(RED);
rectangle(x1,y1,x2,y2);
}

AMAN KUMAR(1221163) 18 |
Page
AMAN KUMAR(1221163) 19 |
Page
PRACTICAL -08

Aim : Program To Scale any object

Source Code :

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
int x1,y1,x2,y2,x3,y3,mx,my;
void main()
{
intgd=DETECT,gm;
int x,y,a1,a2,a3,b1,b2,b3;
intmx,my;
int c;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("Enter the 1st point for the triangle:");
scanf("%d%d",&x1,&y1);
printf("Enter the 2nd point for the triangle:");
scanf("%d%d",&x2,&y2);
printf("Enter the 3rd point for the triangle:");
scanf("%d%d",&x3,&y3);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
printf("Enter the scalling coordinates");
scanf("%d%d",&x,&y);
mx=(x1+x2+x3)/3;
my=(y1+y2+y3)/3;
a1=mx+(x1-mx)*x;
b1=my+(y1-my)*y;
a2=mx+(x2-mx)*x;
b2=my+(y2-my)*y;
a3=mx+(x3-mx)*x;
b3=my+(y3-my)*y;
line(a1,b1,a2,b2);
line(a2,b2,a3,b3);
line(a3,b3,a1,b1);
getch();
}

AMAN KUMAR(1221163) 20 |
Page
AMAN KUMAR(1221163) 21 |
Page
PRACTICAL -09

Aim : Program To Rotate any object

Source Code :

#include<stdio.h>
#include<graphics.h>
#include<math.h>
main()
{
int gd=0,gm,x1,y1,x2,y2,x3,y3;
double s,c, angle;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
setcolor(RED);
printf("Enter coordinates of triangle: ");
scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2, &x3, &y3);

cleardevice();
line(x1,y1,x2,y2);
line(x2,y2, x3,y3);
line(x3, y3, x1, y1);
getch();
setbkcolor(BLACK);
printf("Enter rotation angle: ");
scanf("%lf", &angle);

c = cos(angle *M_PI/180);
s = sin(angle *M_PI/180);
x1 = floor(x1 * c + y1 * s);
y1 = floor(-x1 * s + y1 * c);
x2 = floor(x2 * c + y2 * s);
y2 = floor(-x2 * s + y2 * c);
x3 = floor(x3 * c + y3 * s);
y3 = floor(-x3 * s + y3 * c);
cleardevice();
line(x1, y1 ,x2, y2);
line(x2,y2, x3,y3);
line(x3, y3, x1, y1);
getch();
closegraph();
return 0;
}

AMAN KUMAR(1221163) 22 |
Page
AMAN KUMAR(1221163) 23 |
Page
PRACTICAL -10

Aim : Program To implement Boundary fill algorithm.

Source Code :

#include<stdio.h>
#include<conio.h>
#include<graphics.h>

void boundary_fill(int, int, int, int);

void main()
{
int gd=DETECT,gm;
int x1=50,y1=50,x2=120,y2=120;
int x0=x1+20,y0=y1+20;
int bound_col=15, fill_col=5;

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

outtext("This is Boundary fill.");


rectangle(x1,y1,x2,y2);
boundary_fill(x0,y0,bound_col,fill_col);

getch();
closegraph();
}

void boundary_fill(int x, int y, int bound_col, int fill_col)


{
if(getpixel(x,y)!=bound_col && getpixel(x,y)!=fill_col)
{
delay(2);
putpixel(x,y,fill_col);
boundary_fill(x+1,y,bound_col,fill_col);
boundary_fill(x-1,y,bound_col,fill_col);
boundary_fill(x,y+1,bound_col,fill_col);
boundary_fill(x,y-1,bound_col,fill_col);
boundary_fill(x+1,y+1,bound_col,fill_col);
boundary_fill(x-1,y+1,bound_col,fill_col);
boundary_fill(x+1,y-1,bound_col,fill_col);
boundary_fill(x-1,y-1,bound_col,fill_col);
}
}

AMAN KUMAR(1221163) 24 |
Page
AMAN KUMAR(1221163) 25 |
Page

You might also like