Trinity Institute of Professional Studies
Affiliated To Guru Gobind Singh Indraprastha University
SECTOR 16-C, DWARAKA, NEW DELHI
Subject: Computer Graphics Practical Sub-Code: BCA 351
Assignment/ Case Study/PPT
-----------------------------------------------------------------------------------------------
Submitted By: Submitted to:
Name of Student: Avinash Singh Mankotia Name of Faculty: Dr. Krishna Singh
Enrollment Number: 01120602020 Designation: Associate Professor
Course: BCA Semester: 5th
Shift: First Year: 3rd
S.no Programs Page no
1. WAP to draw a line using DDA Algorithm.
2. WAP to draw a line using BRESENHAM’S Algorithm.
3. WAP to draw a circle using BRESENHAM’S Algorithm.
4. WAP to draw a Ellipse using Mid-point Ellipse Drawing
Algorithm.
5. WAP to rotate a Triangle about Origin.
6. WAP to Scale a triangle.
7. WAP to Translate a triangle
8. WAP to Rotate a Point about a Point.
9. WAP to rotate a point about Origin.
10. WAP to show line clipping.
WAP to draw a line using DDA Algorithm.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm,i;
float x,y,dx,dy,steps;
int x0,y0,x1,y1;
initgraph(&gd,&gm,"C://TC//BGI");
setcolor(WHITE);
printf("Enter coordinates for 1st point of line");
scanf("%d %d",&x0,&y0);
printf("Enter coordinates for 2nd point of line");
scanf("%d %d",&x1,&y1);
dx=(float)(x1-x0);
dy=(float)(y1-y0);
if(dx>=dy)
{
steps=dx;
}
else
{
steps=dy;
}
dx=dx/steps;
dy=dy/steps;
x=x0;
y=y0;
i=1;
while(i<=steps)
{
putpixel(x,y,WHITE);
x+=dx;
y+=dy;
i=i+1;
}
getch();
closegraph();
}
WAP to draw a line using BRESENHAM’S Algorithm.
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<graphics.h>
void lineBRES(int,int,int,int);
void main()
{
int x1,y1,xn,yn;
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TC\\BGI");
printf("Enter starting coordinates of line:");
scanf("%d%d",&x1,&y1);
printf("Enter ending coordinates of line:");
scanf("%d%d",&xn,&yn);
lineBRES (x1,y1,xn,yn);
getch();
}
void lineBRES(int x1,int y1, int xn, int yn)
{
int dx=xn-x1,dy=yn-y1;
int di=2*dy-dx;
int ds=2*dy,dt=2*(dy-dx);
putpixel(x1,y1,WHITE);
while(x1<xn)
{ x1+
+;
if(di<0)
di=di+ds;
else
{ y1+
+;
di=di+dt;
}
putpixel(x1,y1,WHITE);
delay(20);
}
}
WAP to draw a circle using BRESENHAM’S Algorithm.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void circleMidpoint(int,int,int);
void drawcircle(int,int,int,int);
void main()
{
int xc,yc,r;
int gd=DETECT,gm;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:\\TC\\BGI");
printf("\nEnter the coordinates of center:");
scanf("%d%d",&xc,&yc);
printf("\nEnter the radius:");
scanf("%d",&r);
circleMidpoint(xc,yc,r);
getch();
}
void circleMidpoint(int xc, int yc,int r)
{
int x=0,y=r;
int p=1-r;
while(x<y)
{
drawcircle(xc,yc,x,y);
x++;
if(p<0)
p=p+2*x+1;
else
{
y--;
p=p+2*(x-y)+1;
}
drawcircle(xc,yc,x,y);
delay(50);
}
}
void drawcircle(int xc,int yc,int x,int y)
{
putpixel(xc+x,yc+y,RED);
putpixel(xc-x,yc+y,RED);
putpixel(xc+x,yc-y,RED);
putpixel(xc-x,yc-y,RED);
putpixel(xc+y,yc+x,RED);
putpixel(xc-y,yc+x,RED);
putpixel(xc+y,yc-x,RED);
putpixel(xc-y,yc-x,RED);
}
WAP to draw a Ellipse using Mid-point Ellipse Drawing Algorithm.
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<graphics.h>
void ellipseMidpoint(float,float,float,float);
void drawEllipse(float,float,float,float);
void main()
{
float xc,yc,rx,ry;
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TC\\BGI");
printf("Enter the center coordinates of ellipse:\n");
scanf("%f%f",&xc,&yc);
printf("Enter the x-radius coordinate:\n");
scanf("%f",&rx);
printf("Enter the y-radius coordinate:\n");
scanf("%f",&ry);
ellipseMidpoint(xc,yc,rx,ry);
getch();
}
void ellipseMidpoint(float xc,float yc,float rx,float ry)
{
float rxSq=rx*rx;
float rySq=ry*ry;
float x=0,y=ry,p;
float px=0,py=2*rxSq*y;
drawEllipse(xc,yc,x,y);
//region1
p=rySq-(rxSq*ry)+(0.25*rxSq);
while(px<py)
{ x+
+;
px=px+2*rySq;
if(p<0)
p=p+rySq+px;
else
{
y--;
py=py-2+rxSq;
p=p+rySq+px-py;
}
drawEllipse(xc,yc,x,y);
delay(30);
}
//region2
p=rySq*(x+0.5)*(x+0.5)+rxSq*(y-1)*(y-1)-rxSq*rySq;
while(y>0)
{
y--;
py=py-2*rxSq;
if(p>0)
p=p+rxSq-py;
else
{ x+
+;
px=px+2*rySq;
p=p+rxSq-py+px;
}
drawEllipse(xc,yc,x,y);
delay(30);
}
}
void drawEllipse(float xc,float yc,float x,float 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);
}
WAP to rotate a Triangle about Origin.
#include<stdio.h>
#include<conio.h>
#include<graphics.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 main()
{
int gd=DETECT,gm;
int x1,y1,x2,y2,x3,y3;
initgraph(&gd,&gm,"C:\\TC\\BGI");
printf("Enter the 1st point for the trangle:");
scanf("%d%d",&x1,&y1);
printf("Enter the 2nd point for the trangle:");
scanf("%d%d",&x2,&y2);
printf("Enter the 3rd point for the trangle:");
scanf("%d%d",&x3,&y3);
triangle(x1,y1,x2,y2,x3,y3);
getch();
cleardevice();
rotate(x1,y1,x2,y2,x3,y3);
setcolor(1);
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,a1,b1,a2,b2,a3,b3;
float angle;
printf("Enter the angle for rotation:");
scanf("%f",&angle);
cleardevice();
angle=(angle*3.14)/180;
a1=(x1)*cos(angle)-(y1)*sin(angle);
b1=(x1)*sin(angle)+(y1)*cos(angle);
a2=(x2)*cos(angle)-(y2)*sin(angle);
b2=(x2)*sin(angle)+(y2)*cos(angle);
a3=(x3)*cos(angle)-(y3)*sin(angle);
b3=(x3)*sin(angle)+(y3)*cos(angle);
printf("Rotated");
triangle(a1,b1,a2,b2,a3,b3);
}
WAP to Scale a triangle.
#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<graphics.h>
void main()
{
float a[3][2];
float i,j,sx,sy;
cout<<"Enter th eco-ordinate of triangle";
for(i=0;i<3;i++)
for(j=0;j<3;j++)
cin>>a[i][j];
cout<<"\nEnter The Scaling Factor";
cin>>sx>>sy; clrscr();
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TC\\BGI");
line(a[0][0],a[0][1],a[1][0],a[1][1]);
line(a[1][0],a[1][1],a[2][0],a[2][1]);
line(a[2][0],a[2][1],a[0][0],a[0][1]);
for(i=0;i<3;i++)
{
a[i][0]*=sx;
a[i][1]*=sy;
}
setcolor(BLUE); line(a[0][0],a[0][1],a[1]
[0],a[1][1]);
line(a[1][0],a[1][1],a[2][0],a[2][1]);
line(a[2][0],a[2][1],a[0][0],a[0][1]);
getch();
closegraph();
}
WAP to Translate a triangle
#include<conio.h>
#include<graphics.h>
#include<stdio.h>
void main()
{
int gd=DETECT,gm;
int x,y,x1,y1,x2,y2,tx,ty;
clrscr();
initgraph(&gd,&gm," C://TC//BGI ");
printf("\n Please enter first coordinate of the triangle= ");
scanf("%d %d", &x,&y);
printf("\n Enter second coordinate of the trinagle = ");
scanf("%d %d",&x1,&y1);
printf("\n Enter third coordinate of the triangle = ");
scanf("%d %d",&x2,&y2);
printf("\n\t\t********** TRIANGLE before & after translation ***********");
line(x,y,x1,y1);
line(x1,y1,x2,y2);
line(x2,y2,x,y);
printf("\n Now enter the translation vector = ");
scanf("%d %d",&tx,&ty);
setcolor(RED);
line(x+tx,y+ty,x1+tx,y1+ty);
line(x1+tx,y1+ty,x2+tx,y2+ty);
line(x2+tx,y2+ty,x+tx,y+ty);
getch();
closegraph();
}
WAP to Rotate a Point about a Point.
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <graphics.h>
main()
{
int gd=DETECT,gm;
int x1,y1,x2,y2,x3,y3;
float angle,rad;
initgraph(&gd,&gm,"C://TC//BGI");
printf("Enter the coordinates of a point->");
scanf("%d%d",&x1,&y1);
putpixel(x1,y1,WHITE);
printf("Enter a point about which you want to rotate->");
scanf("%d%d",&x2,&y2);
putpixel(x2,y2,WHITE);
printf("Enter degree of rotation :");
scanf("%f",&angle);
rad=angle*(3.14/180.0);
printf("angle=%f",angle); printf("\
nrad=%f",rad);
x3=x1*cos(rad)-y1*sin(rad)-x2*cos(rad)+y2*sin(rad)+x2;
y3=x1*sin(rad)+y1*cos(rad)-x2*sin(rad)-y2*cos(rad)+y2;
printf("The new coordinates of a rotated point is : \n");
printf("%d,%d",x3,y3);
putpixel(x3,y3,WHITE);
getch();
return 0;
}
WAP to rotate a point about Origin.
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <graphics.h>
main()
{
int gd=DETECT,gm;
int x1,y1,x3,y3;
int x2=0,y2=0;
float angle,rad;
initgraph(&gd,&gm,"C://TC//BGI");
printf("Enter the coordinates of a point->");
scanf("%d%d",&x1,&y1);
putpixel(x1,y1,WHITE);
printf("Enter degree of rotation :");
scanf("%f",&angle);
rad=angle*(3.14/180.0);
printf("angle=%f",angle); printf("\
nrad=%f",rad);
x3=x1*cos(rad)-y1*sin(rad)-x2*cos(rad)+y2*sin(rad)+x2;
y3=x1*sin(rad)+y1*cos(rad)-x2*sin(rad)-y2*cos(rad)+y2;
printf("\nThe new coordinates of a rotated point is : \n");
printf("%d,%d",x3,y3);
putpixel(x3,y3,WHITE);
getch();
return 0;
}
WAP to show line clipping.
#include<graphics.h>
#include<conio.h>
#include<iostream.h>
#include<stdio.h>
#include<math.h>
int static LEFT=1,RIGHT=2,BOTTOM=4,TOP=8,xmin,ymin,xmax,ymax;
int getcode(int x,int y)
{
int code=0;
if(x<xmin)
{
code=code|LEFT;
}
if(x>xmax)
{
code=code|RIGHT;
}
if(y<ymin)
{
code=code|BOTTOM;
}
if(y>ymax)
{
code=code|TOP;
}
return code;
}
void main()
{
int gd=DETECT,gm,x1,y1,x2,y2,getcode1,getcode2,accept=0;
float m;
initgraph(&gd,&gm,"C:\\TC\\BGI");
cout<<"Enter coordinates of the clliping window(xmin,ymin,xmax,ymax)\n";
cin>>xmin>>ymin>>xmax>>ymax;
rectangle(xmin,ymin,xmax,ymax);
cout<<"Enter coordinate of a line:";
cin>>x1>>y1>>x2>>y2;
line(x1,y1,x2,y2);
getch();
getcode1=getcode(x1,y1);
getcode2=getcode(x2,y2);
cout<<getcode1<<getcode2;
while(1)
{
m=(float)(y2-y1)/(x2-x1);
if(getcode1==0 && getcode2==0)
{
accept=1;
break;
}
else if ((getcode1 & getcode2)!=0)
{
break;
}
else
{
int x,y,temp;
if(getcode1==0)
{
temp=getcode2;
}
else
{
temp=getcode1;
}
if(temp&TOP)
{
x=((ymax-y1)/m)+x1;
y=ymax;
}
else if(temp& BOTTOM)
{
y=ymin;
x=((ymin-y1)/m)+x1;
}
else if(temp&LEFT)
{
x=xmin;
y=(m*(xmin-x1))+y1;
}
else if(temp & RIGHT)
{
x=xmax;
y=(m*(xmax-x1))+y1;
}
if(temp==getcode1)
{
x1=x;
y1=y;
getcode1=getcode(x1,y1);
}
else
{
x2=x;
y2=y;
getcode2=getcode(x2,y2);
}
}
}
if(accept)
{
cleardevice();
cout<<"after clipping";
setcolor(RED);
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2);
getch();
}
}