0% found this document useful (0 votes)
84 views23 pages

2016ump3537 2

This document introduces computer graphics and provides examples of computer graphics programming experiments. It defines computer graphics as images created using computers through the representation and manipulation of image data. Key developments in computer graphics have impacted media and revolutionized industries like animation and video games. Example applications mentioned include CAD, simulation, art, education, design, visualization, games and virtual reality. It then provides five programming experiments in C involving graphics concepts like drawing shapes, lines, circles and curves.

Uploaded by

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

2016ump3537 2

This document introduces computer graphics and provides examples of computer graphics programming experiments. It defines computer graphics as images created using computers through the representation and manipulation of image data. Key developments in computer graphics have impacted media and revolutionized industries like animation and video games. Example applications mentioned include CAD, simulation, art, education, design, visualization, games and virtual reality. It then provides five programming experiments in C involving graphics concepts like drawing shapes, lines, circles and curves.

Uploaded by

Nishant Garg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

1.

INTRODUCTION TO COMUTER GRAPHICS

What is computer graphics?

• Computer graphics deals with all aspects of creating images with a computerHardware

Software
Applications

Computer graphics are graphics created using computers and, more generally, the representation and manipulation of
image data by a computer. The development of computer graphics, or simply referred to as CG, has made computers
easier to interact with, and better for understanding and interpreting many types of data. Developments in computer
graphics have had a profound impact on many types of media and have revolutionized the animation and video game
industry. Typically, the term computer graphics refers to several different things: · the representation and manipulation
of image data by a computer · the various technologies used to create and manipulate images · the images so produced
and The subfield of computer science which studies methods for digitally synthesizing and manipulating visual content
Applications: · Computer Aided Design · Computer simulation · Digital art · Education · Graphic design ·
Information visualization · Scientific visualization · Video Games · Virtual reality · Web design
EXP 1

#include<graphics.h>

#include<conio.h>

void main()

int gd = DETECT,gm;

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

circle(280,220,100);

getch();

closegraph();

EXP 2

#include<stdio.h>

#include<graphics.h>

#include<math.h>

#include<conio.h>

#include<dos.h>

float round(float a);

void main()

{
int gd=DETECT,gm;

int x1,y1,x2,y2,steps,k;

float xincr,yincr,x,y,dx,dy;

printf("enter x1,y1");

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

printf("Enter x2,y2");

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

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

dx=x2-x1;

dy=y2-y1;

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

steps=abs(dx);

else

steps=abs(dy);

xincr=dx/steps;

yincr=dy/steps;

x=x1;

y=y1;

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

delay(10);

x+=xincr;

y+=yincr;

putpixel(round(x),round(y),WHITE) ;

outtextxy(200,20,"DDA");

outtextxy(x1+5,y1-5,"(x1,y1)");

outtextxy(x2+5,y2+5,"(x2,y2)");

getch();

closegraph();

float round(float a)

int b=a+0.5;
return b;

EXP 3

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

void main()

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

int gd = DETECT,gm;

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

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

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=(2*(dy-dx));

while(x<=x2)

if(p<0)

x=x+1;

p=p+p*dy;

else

x=x+1;

y=y+1;

p=p+(2*dy)-(2*dx);

putpixel(x,y,7);

getch();

closegraph();

}
EXP 4

#include<stdio.h>

#include<graphics.h>

#include<conio.h>

#include<math.h>

void bresenham_circle(const int h,const int k, const int r)

// cleardevice();

int x=0,y=r, p=(3-(2*r));

cleardevice();

line(320,1,320,480);

line(1,240,640,240);

do

putpixel((h+x),(k+y),RED);

putpixel((h+y),(k+x),15);

putpixel((h+y),(k-x),BLUE);

putpixel((h+x),(k-y),YELLOW);

putpixel((h-x),(k-y),GREEN);

putpixel((h-y),(k-x),RED);

putpixel((h-y),(k+x),15);

putpixel((h-x),(k+y),BLUE);

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;

initgraph(&driver,&mode,"..\\Bgi");

printf("\n ENTER THE VALUE OF [X-COORDINATE] :");

scanf("%d",&h);

printf("\n ENTER THE VALUE OF [Y-COORDINATE] :");

scanf("%d",&k);

printf("\n ENTER THE VALUE OF RADIUS :");

scanf("%d",&r);

bresenham_circle(320+h,240-k,r);

getch();

}
EXP 5 and EXP 6

i. TRANSLATION

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd = DETECT,gm;
int x1,y1,x2,y2,tx,ty,x3,y3,x4,y4;
initgraph(&gd,&gm,"C:\\Turboc3\\BGI");
printf("Enter the starting point of line segment");
scanf("%d%d",&x1,&y1);
printf("Enter the ending point of line segment");
scanf("%d%d",&x2,&y2);
printf("Enter translational distances tx,ty:\n");
scanf("%d%d",&tx,&ty);
setcolor(5);
line(x1,y1,x2,y2);
outtextxy(x2+2,y2+2,"Original line");
x2=x1+tx;
y3=y1+ty;
x4=x2+tx;
y4=y2+ty;
setcolor(7);
line(x3,y3,x4,y4);
outtextxy(x4+2,y4+2,"Line after translation");
getch();
}
ii. SCALING
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<stdio.h>
void main()
{
int gd =DETECT,gm;
float x1,y1,x2,y2,sx,sy,x3,y3,x4,y4;
initgraph(&gd,&gm,"C:\\Turboc3\\BGI");
printf("Enter the starting point coordinates");
scanf("%f%f",&x1,&y1);
printf("enter the ending point coordinates");
scanf("%f%f",&x2,&y2);
printf("Enter scaling factors sx,sy:\n");
scanf("%f%f",&sx,&sy);
setcolor(5);
line(x1,y1,x2,y2);
outtextxy(x2+2,y2+2,"Original line");
x3=x1*sx;
y3=y1*sy;
x4=x2*sx;
y4=y2*sy;
setcolor(7);
line(x3,y3,x4,y4);
outtextxy(x3+2,y3+2,"Line after Scaling");
getch();
}
iii. ROTATION
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd = DETECT,gm;
float x1,y1,x2,y2,x3,y3,x4,y4,a,t;
initgraph(&gd,&gm,"C:\\Turboc3\\BGI");
printf("ENter coordinates of starting points");
scanf("%f%f",&x1,&y1);
printf("Enter coordinates of ending point");
scanf("%f%f",&x2,&y2);
printf("Enter angle for rotation\n");
scanf("%f",&a);
setcolor(5);
line(x1,y1,x2,y2);
outtextxy(x2+2,y2+2,"Original Line");
t=a*(3.14/180);
x3=(x1*cos(t))-(y1*sin(t));
y3=(x1*sin(t))+(y1*cos(t));
x4=(x2*cos(t))-(y2*sin(t));
y4=(x2*sin(t))+(y2*cos(t));
setcolor(7);
line(x3,y3,x4,y4);
outtextxy(x3+2,y3+2,"Line after rotation");
getch();
}
iv. SHEARING

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\Tc\\BGI");
float x1,y1,x2,y2;
float shx,shy;
char ch;
x1=50;y1=100;x2=100;y2=200;
rectangle(x1,y1,x2,y2);
delay(10);
cout<<"enter the direction of shear : ";
cin>>ch;
if(ch=='x')
{
cout<<"enter x-direction of shear : ";
cin>>shx;
y1=y1+shx*x1;
y2=y2+shx*x2;
setcolor(RED);
rectangle(x1,y1,x2,y2);
}
else
{
cout<<"enter y-direction of shear : ";
cin>>shy;
x1=x1+shy*y1;
x2=x2+shy*y2;
setcolor(RED);
rectangle(x1,y1,x2,y2);
}
getch();
closegraph();
}

v. REFLECTION
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
int IncFlag;
int PolygonPoints[3][2]={{10,100},{110,100},{110,200}};
void PolyLine()
{
int iCnt;
cleardevice();
line(0,240,640,240);
line(320,0,320,480);
for(iCnt=0;iCnt<3;iCnt++)
{
line(PolygonPoints[iCnt][0],PolygonPoints[iCnt][1],
PolygonPoints[(iCnt+1)%3][0],PolygonPoints[(iCnt+1)%3][1]);
}
}
void reflect()
{
float angle;
int iCnt;
int Tx,Ty;
printf("endl");
for(iCnt=0;iCnt<3;iCnt++)
{
PolygonPoints[iCnt][1]=(480-PolygonPoints[iCnt][1]);
}
}
void main()
{
int gDriver=DETECT,gMode;
int iCnt;
initgraph(&gDriver,&gMode,"C:\\Turboc3\\BGI");
for(iCnt=0;iCnt<3;iCnt++)
{
PolygonPoints[iCnt][0]+=320;
PolygonPoints[iCnt][1]=240-PolygonPoints[iCnt][1];
}
PolyLine();
getch();
reflect();
PolyLine();
getch();
}
Before reflection

After Reflection
EXP 7

#include<stdio.h>
#include<stdlib.h>

#include<graphics.h>

#include<math.h>

#include<conio.h>

void bezier(int x[4], int y[4])

int gd= DETECT, gm;

int i;

double t;

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

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

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

double yt= pow(1-t,3)*y[0]+3*t*pow(1-t,32)*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],YELLOW);

getch();

closegraph();

return;

void main()

int x[4],y[4];

int i;

printf("Enter the x- and y-coordinates of the four control points\n");

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

scanf("%d%d",&x[i],&y[i]);

bezier(x,y);

}
EXP 8

#include<stdio.h>

#include<stdlib.h>

#include<conio.h>

#include<graphics.h>

#include<dos.h>

typedef struct coord

int x,y;

char code[4];

}PT;

void drawwindow();

void drawline(PT p1,PT p2,int c1);

PT setcode(PT p);

int visibility(PT p1,PT p2);

PT resetendpt(PT p1,PT p2);

void main()

int gd=DETECT,v,gm;

PT p1,p2,ptemp;
initgraph(&gd,&gm,"C:\\turboc3\\BGI");

cleardevice();

printf("\nEnter x1 and y1\n");

scanf("%d %d",&p1.x,&p1.y);

printf("\nEnter x2 and y2\n");

scanf("%d %d",&p2.x,&p2.y);

cleardevice();

printf("\n\nCLIPPING WINDOW");

drawwindow();

getch();

printf("\n\nBEFORE CLIPPING");

drawline(p1,p2,4);

getch();

p1=setcode(p1);

p2=setcode(p2);

v=visibility(p1,p2);

switch(v)

case 0: cleardevice();

drawwindow();

drawline(p1,p2,15);

break;

case 1: cleardevice();

drawwindow();

break;

case 2: cleardevice();

p1=resetendpt(p1,p2);

p2=resetendpt(p2,p1);
drawwindow();

printf("\n\t\tAFTER CLIPPING\n\n");

drawline(p1,p2,15);

break;

getch();

closegraph();

void drawwindow()

setcolor(RED);

setlinestyle(DOTTED_LINE,1,1);

line(150,100,100,100);

line(150,100,150,50);

line(450,100,500,100);

line(450,100,450,50);

line(450,350,500,350);

line(450,350,450,400);

line(150,350,150,400);

line(150,350,100,350);

line(150,100,100,100);

line(150,100,150,50);

setlinestyle(SOLID_LINE,1,1);

line(150,100,450,100);

line(450,100,450,350);

line(450,350,150,350);

line(150,350,150,100);

outtextxy(450,30,"1001");

outtextxy(470,200,"1000");

outtextxy(470,370,"1010");

outtextxy(300,370,"0010");
outtextxy(120,370,"0110");

outtextxy(120,200,"0100");

outtextxy(120,30,"0101");

outtextxy(300,30,"0001");

outtextxy(300,200,"0000");

void drawline(PT p1,PT p2,int c1)

setcolor(c1);

line(p1.x,p1.y,p2.x,p2.y);

PT setcode(PT p)

PT ptemp;

if(p.y<100)

ptemp.code[0]='1';

else

ptemp.code[0]='0';

if(p.y>350)

ptemp.code[1]='1';

else

ptemp.code[1]='0';

if(p.x>200)

ptemp.code[2]='1';

else

ptemp.code[2]='0';

if(p.x<150)

ptemp.code[3]='1';
else

ptemp.code[3]='0';

ptemp.x=p.x;

ptemp.y=p.y;

return(ptemp);

int visibility(PT p1,PT p2)

int i,flag=0;

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

if((p1.code[i]!='0') || (p2.code[i]!='0'))

flag=1;

if(flag==0)

return(0);

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

if((p1.code[i]==p2.code[i]) && (p1.code[i]=='1'))

flag='0';

if(flag==0)

return(1);

return(2);

}
PT resetendpt(PT p1,PT p2)

PT temp;

int x,y,i;

float m,k;

if(p1.code[3]=='1')

x=150;

if(p1.code[2]=='1')

x=450;

if((p1.code[3]=='1') || (p1.code[2]=='1'))

m=(float)(p2.y-p1.y)/(p2.x-p1.x);

k=(p1.y+(m*(x-p1.x)));

temp.y=k;

temp.x=x;

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

temp.code[i]=p1.code[i];

if(temp.y<=350 && temp.y>=100)

return (temp);

if(p1.code[0]=='1')

y=100;

if(p1.code[1]=='1')

y=350;

if((p1.code[0]=='1') || (p1.code[1]=='1'))
{

m=(float)(p2.y-p1.y)/(p2.x-p1.x);

k=(float)p1.x+(float)(y-p1.y)/m;

temp.x=k;

temp.y=y;

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

temp.code[i]=p1.code[i];

return(temp);

else

return(p1);

return(p2);

1.

2.

You might also like