You are on page 1of 33

Computer graphics practical

1.WAP FOR 2D LINE DRWAING AS RASTER GRAPHICS DISPLAY.


A) BRESENHAM’S ALGORITHM FOR LINE DRAWING.
B) DDA LINE DRWAING ALGORITHM.

2.WAP FOR CIRCLE DRWAING AS RASTER GRAPHICS DISPLAY.


/*Bresenham's Circle Drawing Algorithm*/

3.WAP FOR POLYGON FILLING AS RASTER GRAPHICS DISPLAY.

4.WAP FOR LINE CLIPPING.

5.WAP FOR POLYGON CLIPPING.

6.WRITE A PROGRAM FOR DISLPAYING 3D OBJECTS AS DISPLAY USING


PERSPECTIVE TRANSFORMATION.

7. WRITE A PROGRAM T O SCALE THE TRIANGLE.

8.WRITE A PROGRAM TO SHOW BITMAP IMAGE ON YOUR COMPUTER SCREEN.

9. PROGRAM TO DRAW A HUT USING SIMPLE GRAPHIC FUNCTIONS.

10. STUDY OF ADOBE FLASH.

1.Wap for 2D line drawing as raster graphics display.


Experiment 1(a): BRESENHAM’S ALGORITHM FOR LINE DRAWING.

1. Start.
2. Declare variables x,y,x1,y1,x2,y2,p,dx,dy and also declare gdriver=DETECT,gmode.
3. Initialize the graphic mode with the path location in TC folder.
4. Input the two line end-points and store the left end-points in (x1,y1).
5. Load (x1,y1) into the frame buffer; that is, plot the first point put x=x1,y=y1.
6. Calculate dx=x2-x1 and dy=y2-y1,and obtain the initial value of decision parameter p as:
a. p=(2dy-dx).
7. Starting from first point (x,y) perform the following test:
8. Repeat step 9 while(x<=x2).
9. If p<0,next point is (x+1,y) and p=(p+2dy).
10. Otherwise, the next point to plot is (x+1,y+1) and p=(p+2dy-2dx).
11. Place pixels using putpixel at points (x,y) in specified colour.
12. Close Graph.
13. Stop.

WAP TO DRAW A LINE USING MID POINT ALGORITHM OR BRESENHAM’S


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

Experiment 1(b): ALGORITHM TO DRAW A LINE USING DDA ALGORITHM.

1. Start.
2. Declare variables x,y,x1,y1,x2,y2,k,dx,dy,s,xi,yi and also declare
gdriver=DETECT,gmode.
3. Initialise the graphic mode with the path location in TC folder.
4. Input the two line end-points and store the left end-points in (x1,y1).
5. Load (x1,y1) into the frame buffer;that is,plot the first point.put x=x1,y=y1.
6. Calculate dx=x2-x1 and dy=y2-y1.
7. If abs(dx) > abs(dy), do s=abs(dx).
8. Otherwise s= abs(dy).
9. Then xi=dx/s and yi=dy/s.
10. Start from k=0 and continuing till k<s,the points will be
i. x=x+xi.
ii. y=y+yi.
11. Place pixels using putpixel at points (x,y) in specified colour.
12. Close Graph.
13. Stop.
WAP TO DRAW A LINE USING DDA ALGORITHM.

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int x,y,x1,x2,y1,y2,k,dx,dy,s,xi,yi;
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"C:\\tc\\bgi:");
printf("enter first point");
scanf("%d%d",&x1,&y1);
printf("enter second point");
scanf("%d%d",&x2,&y2);
x=x1;
y=y1;
putpixel(x,y,7);
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>abs(dy))
s=abs(dx);
else
s=abs(dy);
xi=dx/s;
yi=dy/s;
x=x1;
y=y1;
putpixel(x,y,7);
for(k=0;k<s;k++)
{
x=x+xi;
y=y+yi;
putpixel(x,y,7);
}
getch();
closegraph();
}

2. WAP FOR CIRCLE DRAWING AS RASTER GRAPHICS DISPLAY.


/*Bresenham's Circle Drawing Algorithm*/

# include<stdio.h>
# include<conio.h>
# include<graphics.h>
void main()
{
int gd=DETECT,gm,xc,yc,r,x,y,Pk;
clrscr();
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
printf("*** Bresenham's Circle Drawing Algorithm ***\n");
printf("Enter the value of Xc\t");
scanf("%d",&xc);
printf("Enter the value of yc\t");
scanf("%d",&yc);
printf("Enter the Radius of circle\t");
scanf("%d",&r);
x=0;
y=r;
putpixel(xc+x,yc-y,1);
Pk=3-(2*r);
for(x=0;x<=y;x++)
{
if (Pk<0)
{
y=y;
Pk=(Pk+(4*x)+6);
}
else
{
y=y-1;
Pk=Pk+((4*(x-y)+10));
}
putpixel(xc+x,yc-y,7);
putpixel(xc-x,yc-y,7);
putpixel(xc+x,yc+y,7);
putpixel(xc-x,yc+y,7);
putpixel(xc+y,yc-x,7);
putpixel(xc-y,yc-x,7);
putpixel(xc+y,yc+x,7);
putpixel(xc-y,yc+x,7);
delay(100);

}
getch();

}
Experiment 3:
WRITE A
PROGRAM FOR
POLYGON
FILLING AS
RASTER
GRAPHICS
DISPLAY

#include<conio.h>
#include<iostream.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
#include<process.h>
void main()
{
int graphdriver=DETECT,graphmode;
initgraph(&graphdriver,&graphmode,"...\\bgi");
int p=1,x;

int a[12]={100,100,150,150,200,100,200,200,100,200,100,100};
drawpoly(6,a);

for(int i=100;i<200;i++)
{ p=1;
for(int j=100;j<=200;j++)
{
x=getpixel(j,i);
for(int d=0;d<11;d++)
{
if(j==a[d]&&i==a[d+1] )
break;
else
{
if(x>0&&d==10)
p++;
if(p%2==0)
putpixel(j,i,4);
}}}}
getch();
closegraph();
}
OUTPUT
Experiment 4: WRITE A PROGRAM FOR LINE CLIPPING
ALGORITHM TO CLIP A LINE.

1. Start.
2. Initialize the graphic system using initgraph function.
3. Get the input of window co ordinates from the user and draw a window.
4. Get the input of line co ordinates from user and draw the line.
5. Calculate the region code of each end point of line using relation given in steps 6 to step
6. Let (x,y) be the co ordinates of end point of line and (xmin,ymin), (xmax,ymax) be co
ordinates of world window
7. If y –ymax = +ve
8. MSB region code = 1.
9. Else MSB region code = 0.
10. If ymin – y = +ve
11. Region code = 1.
12. Else Region code = 0.
13. If x – xmax = +ve
14. Region code = 1.
15. Else Region code = 0.
16. If xmin – x = +ve
17. LSB Region code = 1.
18. Else LSB Region code = 0.
19. Calculate region code of both end points.
20. Logically and both region code.
21. If Logically anded result is = 0
22. Line is not a clipping candidate.
23. Else.
24. Line is a clipping candidate.
25. Calculate slope of line using formula slope=(y2-y1)/(x2-x1).
26. If line is to be horizontally clipped.
27. New y = ymin or ymax.
28. New x = x1 + ((new y - y1)/slope).
29. If line is to be vertically clipped.
30. New x = xmin or xmax.
31. New y = y1+slope*(new x –x1).
32. Clip the lines from these intersection points.
33. Display the new line.
34. Close the graphic system.
35. Stop.

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void storepoints(int,int,int,int,int,int,int[]);
void main()
{
int gdriver=DETECT,gmode;
int x1,x2,y1,y2,xmax,ymax,xmin,ymin,a[10],b[10],xi1,xi2,yi1,yi2,flag=0;
float m;
int i;
clrscr();

printf("output");
printf("\n");
printf("enter the value of x1,y1,x2,y2: >");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
printf("enter the value of xmax,ymax,xmin,ymin:");
scanf("%d%d%d%d",&xmax,&ymax,&xmin,&ymin);
storepoints(x2,y2,ymin,ymax,xmax,xmin,b);
for(i=1;i<=4;i++)
{
if(a[i]*b[i]==0)
flag=1;
}
else
flag=0;

if(flag==1)
{
m=(y2-y1)/(x2-x1);
xi1=x1;
yi1=y1;
}
if(a[1]==1)
{
yi1=ymax;
xi1=x1+((1/m)*(yi1-y1));
}
else
{
if(a[2]==1)
{
yi1=ymin;
xi1=x1+((1/m)*(yi1-y1));
}
}
if(a[3]==1)
{
xi1=xmax;
yi1=y1+(m*(xi1-x1));
}
if(a[4]==1)
{
xi1=xmin;
yi1=y1+(m*(xi1-x1));
}
else

if(b[1]==1)
{
yi2=ymax;
xi2=x2+((1/m)*(yi2-y2));
}
else
if(b[2]==1)
{
yi2=ymin;
xi2=x2+((1/m)*(yi2-y2));
}
else

if(b[3]==1)
{
xi2=xmax;
yi2=y2+((1/m)*(xi2-x2));
}
else

if(b[4]==1)
{
xi2=xmin;
yi2=y2+(m*(xi2-x2));
}
clrscr();
initgraph(&gdriver,&gmode,"c://tc//bgi:");
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2);
delay(5000);
closegraph();
clrscr();
initgraph(&gdriver,&gmode,"c://tc//bgi:");
line(xi1,yi1,xi2,yi2);
rectangle(xmin,ymin,xmax,ymax);
if(flag==0)
{
printf("\n no clipping is required");
}
getch();
closegraph();
}
void storepoints(int x1,int y1,int ymax,int xmax,int xmin,int ymin,int c[10])
{
if((y1-ymax)>0)
c[1]=1;
else
c[1]=0;
if((ymin-y1)>0)
c[2]=1;
else
c[2]=0;
if((x1-xmax)>0)
c[3]=1;
else
c[3]=0;
if((xmin-x1)>0)
c[4]=1;
else
c[4]=0;
}
OUTPUT

enter the value of x1,y1,x2,y2: >10


10
100
100
enter the value of xmax,ymax,xmin,ymin50
50
0
0

EXPERIMENT 5: WRITE A PROGRAM FOR POLYGON CLIPPING

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void clip(float,float,float);
int i,j=0,n;
int rx1,rx2,ry1,ry2;
float x1[8],y1[8];
void main()
{
int gd=DETECT,gm;
int i,n;
float x[8],y[8],m;
clrscr();
initgraph(&gd,&gm,"");
printf("coordinates for rectangle : ");
scanf("%d%d%d%d",&rx1,&ry1,&rx2,&ry2);
printf("no. of sides for polygon : ");
scanf("%d",&n);
printf("coordinates : ");
for(i=0;i<n;i++)
{
scanf("%f%f",&x[i],&y[i]);
}
cleardevice();
outtextxy(10,10,"Before clipping");
outtextxy(10,470,"Press any key....");
rectangle(rx1,ry1,rx2,ry2);
for(i=0;i<n-1;i++)
line(x[i],y[i],x[i+1],y[i+1]);
line(x[i],y[i],x[0],y[0]);
getch();
cleardevice();
for(i=0;i<n-1;i++)
{
m=(y[i+1]-y[i])/(x[i+1]-x[i]);
clip(x[i],y[i],m);
clip(x[i+1],y[i+1],m);
}
m=(y[i]-y[0])/(x[i]-x[0]);
clip(x[i],y[i],m);
clip(x[0],y[0],m);
outtextxy(10,10,"After clipping");
outtextxy(10,470,"Press any key....");
rectangle(rx1,ry1,rx2,ry2);
for(i=0;i<j-1;i++)
line(x1[i],y1[i],x1[i+1],y1[i+1]);
getch();
}

void clip(float e,float f,float m)


{
while(e<rx1 e>rx2 f<ry1 f>ry2)
{
if(e<rx1)
{
f+=m*(rx1-e);
e=rx1;
}
else if(e>rx2)
{
f+=m*(rx2-e);
e=rx2;
}
if(f<ry1)
{
e+=(ry1-f)/m;
f=ry1;
}
else if(f>ry2)
{
e+=(ry2-f)/m;
f=ry2;
}
}
x1[j]=e;
y1[j]=f;
j++;
}
EXPERIMENT 6: WRITE A PROGRAM FOR DISPLAYING 3D OBJECTS AS DISPLAY USING
PERSPECTIVE TRANSFORMATION.

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

main()
{

int x1,y1,x2,y2,gd,gm;
int ymax,a[4][8];
float par[4][4],b[4][8];
int i,j,k,m,n,p;
int xp, yp, zp, x, y, z;

a[0][0] = 100; a[1][0] = 100; a[2][0] = -100;


a[0][1] = 200; a[1][1] = 100; a[2][1] = -100;

a[0][2] = 200; a[1][2] = 200; a[2][2] = -100;


a[0][3] = 100; a[1][3] = 200; a[2][3] = -100;

a[0][4] = 100; a[1][4] = 100; a[2][4] = -200;


a[0][5] = 200; a[1][5] = 100; a[2][5] = -200;

a[0][6] = 200; a[1][6] = 200; a[2][6] = -200;


a[0][7] = 100; a[1][7] = 200; a[2][7] = -200;

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

ymax = getmaxy();
xp = 300; yp = 320; zp = 100;

for(j=0; j<8; j++)


{
x = a[0][j]; y = a[1][j]; z = a[2][j];

b[0][j] = xp - ( (float)( x - xp )/(z - zp)) * (zp);


b[1][j] = yp - ( (float)( y - yp )/(z - zp)) * (zp);
}

/*- front plane display -*/

for(j=0;j<3;j++)
{
x1=(int) b[0][j]; y1=(int) b[1][j];
x2=(int) b[0][j+1]; y2=(int) b[1][j+1];
line( x1,ymax-y1,x2,ymax-y2);

}
x1=(int) b[0][3]; y1=(int) b[1][3];
x2=(int) b[0][0]; y2=(int) b[1][0];
line( x1, ymax-y1, x2, ymax-y2);

/*- back plane display -*/


setcolor(11);
for(j=4;j<7;j++)
{
x1=(int) b[0][j]; y1=(int) b[1][j];
x2=(int) b[0][j+1]; y2=(int) b[1][j+1];
line( x1, ymax-y1, x2, ymax-y2);

}
x1=(int) b[0][7]; y1=(int) b[1][7];
x2=(int) b[0][4]; y2=(int) b[1][4];
line( x1, ymax-y1, x2, ymax-y2);

setcolor(7);
for(i=0;i<4;i++)
{
x1=(int) b[0][i]; y1=(int) b[1][i];
x2=(int) b[0][4+i]; y2=(int) b[1][4+i];
line( x1, ymax-y1, x2, ymax-y2);
}
getch(); getch();

7.WRITE A PROGRAM T O SCALE THE TRIANGLE.


#include<iostream.h>
#include<conio.h>
#include<graphics.h>O
void main()
{
int gd=DETECT,gm;
initgraph(&gd, &gm,"");
cleardevice();
int x1,y1,x2,y2,x3,y3,x4,y4;
float sx,sy;
cout<<"Enter the first coordinates of triangle\n";
cin>>x1>>y1;
cout<<"Enter the second coordinates of triangle\n";
cin>>x2>>y2;
cout<<"Enter the third coordinates of triangle\n";
cin>>x3>>y3;
int poly[8]={x1,y1,x2,y2,x3,y3,x1,y1};
cleardevice();
drawpoly(4,poly);
getch();
cout<<"Enter the scaling factors\n";
cin>>sx>>sy;
x4=sx*x1-x1;
y4=sy*y1-y1;

x1=sx*x1-x4;
y1=sy*y1-y4;
x2=sx*x2-x4;
y2=sy*y2-y4;
x3=sx*x3-x4;
y3=sy*y3-y4;
poly[0]=x1;
poly[1]=y1;
poly[2]=x2;
poly[3]=y2;
poly[4]=x3;
poly[5]=y3;
poly[6]=x1;
poly[7]=y1;
getch();
cleardevice();
drawpoly(4,poly);
getch();
closegraph();
}
OUTPUT
8.WRITE A PROGRAM TO SHOW A BITMAP IMAGE ON YOUR COMPUTER SYSTEM.

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

struct BMP
{
char Type[2]; //File type. Set to "BM".
unsigned long Size; //Size in BYTES of the file.
unsigned long Reserved; //Reserved. Set to zero.
unsigned long OffSet; //Offset to the data.
unsigned long headsize; //Size of rest of header. Set to 40.
unsigned long Width; //Width of bitmap in pixels.
unsigned long Height; // Height of bitmap in pixels.
unsigned int Planes; //Number of Planes. Set to 1.
unsigned int BitsPerPixel; //Number of Bits per pixels.
unsigned long Compression; //Compression. Usually set to 0.
unsigned long SizeImage; //Size in bytes of the bitmap.
unsigned long XPixelsPreMeter; //Horizontal pixels per meter.
unsigned long YPixelsPreMeter; //Vertical pixels per meter.
unsigned long ColorsUsed; //Number of colors used.
unsigned long ColorsImportant; //Number of "important" colors.
};

int ShowBMP(int x, int y, char* FileName)


{
int b,a;
BMP Obj;

unsigned char* Datas;


int in=0;
unsigned char c=0;
FILE * fp;

fp = fopen(FileName,"rb");
if(!fp){
printf("Error : Unable to open file ..");
exit(0);
}

fread(&Obj, sizeof(Obj), 1, fp);


if(Obj.BitsPerPixel!=4) // This isn't a 16 color bmp we can read;
{
fclose(fp);
printf("Error : File format not supported ..");
exit(0);
};
fseek(fp,Obj.OffSet,SEEK_SET);
Datas=(unsigned char*) calloc(Obj.Width/2+1, sizeof(unsigned char));
for(b=Obj.Height;b>=0;b--)
{
fread(Datas, sizeof(unsigned char), Obj.Width/2, fp);
c=0;
in=0;
for(a=0;a<=Obj.Width;a+=2)
{
c = (Datas[in] | 0x00) >>4;
putpixel(a+x,b+y,c);
c = (Datas[in] | 0xF0) & 0x0F;
putpixel(a+1+x,b+y,c);
in++;
}
}
free (Datas);
fclose(fp);
return 1;
}
void main()
{
int color;
int gd , gm ;
gd = VGA ; gm = VGAHI;

initgraph(&gd,&gm,"");
ShowBMP(0,0,"tune.bmp"); /* Enter File Name Here */
getch();
closegraph();
}

9. PROGRAM TO DRAW A HUT USING SIMPLE GRAPHIC FUNCTIONS

#include<conio.h>
#include<iostream.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
#include<process.h>
void main()
{
int graphdriver=DETECT,graphmode;
initgraph(&graphdriver,&graphmode,"...\\bgi");
line(100,100,150,50);
line(150,50,200,100);
line(100,100,200,100);
line(150,50,350,50);
line(200,100,350,100);
line(350,50,350,100);
circle(150,75,10);
rectangle(100,100,200,300);
rectangle(200,100,350,300);
rectangle(250,175,300,225);
line(250,175,300,225);
line(300,175,250,225);
line(125,300,125,225);
line(175,300,175,225);
arc(150,225,0,180,25);
getch();
closegraph();

}
OUTPUT
10.STUDY OF ADOBE FLASH.

Definition - What does Adobe Flash mean?


Adobe Flash is a proprietary application development platform developed by Adobe Systems.
The primary focus of the Flash platform is the creation of Rich Internet applications (RIA),
which combine graphics, animation, video and sound for an enhanced Web user experience.
The Adobe Flash platform is comprised of several different technologies, including:

 Flash Professional: A tool primarily used for animation design and creation
 Flash Builder: An integrated development environment (IDE) used to create RIAs
 Flex: The Flash development framework, including the software development kit (SDK)
 Flash Player: A client browser plug-in that provides the runtime environment for Flash
applications on the Web
 Adobe Integrated Runtime (AIR): A desktop runtime environment for Flash applications

Adobe Flash has enthusiastic supporters and critics. On the positive side, developers have used
the platform to produce amazing animations that enhance Web surfing. Detractors, however,
have noted negative Flash aspects, including the following:

 Frequently used to produce ads and banners that are annoying to users.
 Requires the Flash Player browser plug-in to display a Flash application in a Web page.
 Controlled by Adobe and not an open-source platform.
 Poses potential security risks.
 Can cause slow Web page display times.

Most browsers provide the option to disable the Flash Player plug-in.
Steve Jobs, co-founder and CEO of Apple, was famously averse to Flash and did not support it in
the iOS (mobile) version of the Apple Safari browser.

Tools[edit]
The Tools menu is probably the most used panel in Flash. In it are the tools needed to manipulate
items on the stage. It can also be used to do actions on the timeline.
Here are the main tools and their uses;

 Selection Tool (v) - Selecting whole objects and moving them.


 Subselection Tool - Selecting partial objects.
 Line Tool - Drawing straight lines.
 Lasso Tool - Selecting irregular shaped pieces of an object
 Pen Tool - Drawing non-straight and curved lines.
 Text Tool (t) - Inputting a textbox onto the stage.
 Oval Tool - Drawing oval and circle shapes. (Drag + Holding Shift = Circles)
 Shape Tool - By default draws rectangles. (Drag + Holding Shift = Squares)
 Pencil Tool - Free drawing thin lines.
 Brush Tool- Free drawing brush shapes.
 Free Transform - Scale, rotate, and skew an object.
 Fill Transform - Distort the gradient fill of an object.
 Stroke Tool - Change the color and size of a stroke.
 Fill Tool - Change the color of a fill.
 Eyedropper - Sample a color from the stage.
 Eraser - Use a brush to erase parts of an object.
 Hand Tool - Move the stage around if it is not all visible.
 Zoom - Zoom in or out.

You might also like