Computer Graphics & Multimedia CSP- · PDF fileWAP to draw a line using DDA ... WAP to fill...

57
OM INSTITUTE OF TECHNOLOGY & MANAGEMENT JUGLAN, HISAR-125001 LAB MANUAL Computer Graphics & Multimedia CSP-641 DEPARTMENT OF COMPUTER APPLICATIONS

Transcript of Computer Graphics & Multimedia CSP- · PDF fileWAP to draw a line using DDA ... WAP to fill...

Page 1: Computer Graphics & Multimedia CSP- · PDF fileWAP to draw a line using DDA ... WAP to fill the triangle using Boundary Fill 4 connected Algorithm. 13. WAP to ... •Arc draws a circular

OM INSTITUTE OF TECHNOLOGY & MANAGEMENT

JUGLAN, HISAR-125001

LAB MANUAL

Computer Graphics & Multimedia

CSP-641

DEPARTMENT OF COMPUTER APPLICATIONS

Page 2: Computer Graphics & Multimedia CSP- · PDF fileWAP to draw a line using DDA ... WAP to fill the triangle using Boundary Fill 4 connected Algorithm. 13. WAP to ... •Arc draws a circular

S.NO PROGRAM’S TITLE1. Study of basic graphics functions defined in “graphics.h”.

2. WAP to use the inbuilt function of computer graphics.

3. WAP to draw a line using DDA function.

4. WAP to draw a line using Symmetric DDA function.

5. WAP to draw a line using Bresenham’s Algorithm (when slop<=1).

6. WAP to draw a line using Bresenham’s Algorithm (when slop>=1).

7. WAP to draw a circle using Polynomial function.

8. WAP to draw a circle using Bresenham’s function.

9. WAP to draw a circle using Trigonometric function.

10. WAP to draw an ellipse using Polynomial function.

11. WAP to draw an ellipse using Trigonometric function.

12. WAP to fill the triangle using Boundary Fill 4 connected Algorithm.

13. WAP to fill the circle using Flood Fill 4 connected Algorithm.

14. WAP to implement translation of a triangle.

15. WAP to implement translation in a circle.

16. WAP to implement rotation of line about origin.

17. WAP to perform line clipping.

18. WAP to implement reflection of a triangle.

19. WAP to perform shearing on a triangle.

Page 3: Computer Graphics & Multimedia CSP- · PDF fileWAP to draw a line using DDA ... WAP to fill the triangle using Boundary Fill 4 connected Algorithm. 13. WAP to ... •Arc draws a circular

Program:1

Study of basic graphics functions defined in “graphics.h”.

1) INITGRAPH• Initializes the graphics system.

Declaration• void initgraph (int * graphdriver, int * graphmode, char pathofdriver);

Remarks• To start the graphic system you must first call initgraph.• initgraph initializes the graphic system by loading a graphics driver from

disk(or validating a registered driver) and then putting the system intographics mode.

• initgraph also resets all graphic settings such as color palette, current position,view port, etc. to their defaults and then resets the graph.

Return Value• None.

2) GETPIXEL, PUTPIXEL• getpixel gets the color of a specific pixel.• putpixel places a pixel at a specified point.

Declaration• unsigned getpixel (int x, int y);• void putpixel (int x, int y, color);

Remarks• getpixel gets the color of the pixel located at (x,y).• putpixel plots a point in the color defined at (x,y).

Return Value• getpixel returns the color of the given pixel.• putpixel doesn’t return any value.

3) CLOSEGRAPH• Shuts down the graphics system.

Declaration• void closegraph (void);

Remarks• closegraph deallocates all memory allocated by the graphics system.

Page 4: Computer Graphics & Multimedia CSP- · PDF fileWAP to draw a line using DDA ... WAP to fill the triangle using Boundary Fill 4 connected Algorithm. 13. WAP to ... •Arc draws a circular

• It then restores the screen to the mode it was in before you called initgraph.

Return Value• None.

4) ARC, CIRCLE, PIESLICE• Arc draws a circular arc.• Circle draws a circle.• Pieslice draws a circular pieslice and fills it.

Declaration• void arc (int x, int y, int stangle, int endangle, int radius);• void circle (int x, int y, int radius);• void pieslice (int x, int y, int stangle, int endangle, int radius);

Remarks• Arc draws a circular arc in the current drawing color.• Circle draws a circle in the current drawing color.• Pieslice draws a pieslice in the current drawing color and then fills it using the

current fill pattern and fill color.

Return Value• None.

5) ELLIPSE, FILLELLIPSE, SECTOR• Ellipse draws an elliptical arc.• Fillellipse draws and fills the ellipse.• Sector draws and fills an elliptical pieslice.

Declaration• void ellipse (int x, int y, int stangle, int endangle, int xradius, int yradius);• void fillellipse (int x, int y, int xradius, int yradius);• void sector (int x, int y, int stangle, int endangle, int xradius, int yradius);

Remarks• Ellipse draws an elliptical arc in the current drawing color.• Fillellipse draws an elliptical arc in the current drawing color and then fills it

with fill color and fill pattern.• Sector draws an elliptical pieslice in the current drawing color and then fills it

using the pattern and color defined by ‘setfillstyle’ and ‘setfillpattern’.

Return Value• None.

Page 5: Computer Graphics & Multimedia CSP- · PDF fileWAP to draw a line using DDA ... WAP to fill the triangle using Boundary Fill 4 connected Algorithm. 13. WAP to ... •Arc draws a circular

6) FLOODFILL• It fills a bounded region.

Declaration• void floodfill (int x, int y, int border);

Remarks• floodfill fills an enclosed area on a bitmap device.• The area bounded by the color border is flooded with the current fill pattern

and fill color.• Here (x,y) is a “seed point”. If the seed is within an enclosed area the inside

will be filled; if the seed is outside the enclosed area, the exterior will befilled.

• Use fillpoly instead of floodfill wherever possible, so that you can maintaincode compatibility with future versions.

• floodfill doesn’t work within the IBM - 8514 driver.

Return Value• If an error occurs while flooding a region, graph result returns ‘1’.

7) GETCOLOR, SETCOLOR• Getcolor returns the current drawing color.• Setcolor returns the current or specific drawing color.

Declaration• int getcolor (void);• int setcolor (int color);

Remarks• Getcolor returns the current drawing color.• Setcolor sets the current drawing color to color, which can range from 0 to

getmaxcolor.• To set a drawing color with setcolor, you can pass either the color number or

the equivalent color name.

Return Value• Returns an integer.

8) LINE, LINEREL, LINETO

• Line draws a line between two specified points.• Linerel draws a line at a relative distance from the current position(cp).• Lineto draws a line from the current position to (x,y).

Declaration• void line (int x, int y, int x2, int y2);• void linerel (int dx, int dy);• void lineto (int x, int y);

Page 6: Computer Graphics & Multimedia CSP- · PDF fileWAP to draw a line using DDA ... WAP to fill the triangle using Boundary Fill 4 connected Algorithm. 13. WAP to ... •Arc draws a circular

Remarks• Linerel draws a line from the cp to the point that is a relative distance (dx, dy)

from the cp and then advances the cp by (dx, dy).• Lineto draws a line from cp to (x, y) and moves the cp to (x, y).• There is no return value for all the functions.

Return Value• None.

9) RECTANGLE• Draws a rectangle in graphics mode.

Declaration• void rectangle (int left, int top, int right, int bottom);

Remarks• Draws a rectangle in the current line style, thickness and drawing color.• (left, top) is the upper left corner of the rectangle and (right, bottom) is the

lower right corner.

Return Value• None.

Page 7: Computer Graphics & Multimedia CSP- · PDF fileWAP to draw a line using DDA ... WAP to fill the triangle using Boundary Fill 4 connected Algorithm. 13. WAP to ... •Arc draws a circular

Program:2

/*WAP to use the inbuilt function of computer graphics.*/

#include<conio.h>

#include<iostream.h>

#include<graphics.h>

#include<math.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(300,175,250,225);

line(125,300,125,225);

line(175,300,175,225);

arc(150,225,0,180,25);

getch();

closegraph();

}

Page 8: Computer Graphics & Multimedia CSP- · PDF fileWAP to draw a line using DDA ... WAP to fill the triangle using Boundary Fill 4 connected Algorithm. 13. WAP to ... •Arc draws a circular

Output-:Output-:Output-:

Page 9: Computer Graphics & Multimedia CSP- · PDF fileWAP to draw a line using DDA ... WAP to fill the triangle using Boundary Fill 4 connected Algorithm. 13. WAP to ... •Arc draws a circular

Program:3

/*WAP to draw a line using DDA function.*/

#include<stdio.h>

#include<conio.h>

#include<math.h>

#include<iostream.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,"...\\bgi:");

cout<<"enter first point";

cin>>x1>>y1;

cout<<"enter second point";

cin>>x2>>y2;

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,2);

for(k=0;k<s;k++)

{

x=x+xi;

y=y+yi;

Page 10: Computer Graphics & Multimedia CSP- · PDF fileWAP to draw a line using DDA ... WAP to fill the triangle using Boundary Fill 4 connected Algorithm. 13. WAP to ... •Arc draws a circular

putpixel(x,y,2);

}

getch();

closegraph();

}

Output-:

putpixel(x,y,2);

}

getch();

closegraph();

}

Output-:

putpixel(x,y,2);

}

getch();

closegraph();

}

Output-:

Page 11: Computer Graphics & Multimedia CSP- · PDF fileWAP to draw a line using DDA ... WAP to fill the triangle using Boundary Fill 4 connected Algorithm. 13. WAP to ... •Arc draws a circular

Program:4

/*WAP to draw a line using Symmetric DDA function.*/

#include<conio.h>

#include<math.h>

#include<iostream.h>

#include<graphics.h>

void main()

{

int x,y,x1,x2,y1,y2,k,dx,dy,s,xi,yi,n,n1,n2,i;

int gdriver=DETECT,gmode;

initgraph(&gdriver,&gmode,"...\\bgi");

cout<<"enter first point";

cin>>x1>>y1;

cout<<"enter second point";

cin>>x2>>y2;

dx=x2-x1;

dy=y2-y1;

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

s=abs(dx);

else

s=abs(dy);

n=log(s)/log(2);

xi=(x2-x1)/pow(n,2);

yi=(y2-y1)/pow(n,2);

x=x1;

y=y1;

putpixel(x,y,7);

for(k=0;k<s;k++)

{

x=x+xi;

y=y+yi;

Page 12: Computer Graphics & Multimedia CSP- · PDF fileWAP to draw a line using DDA ... WAP to fill the triangle using Boundary Fill 4 connected Algorithm. 13. WAP to ... •Arc draws a circular

putpixel(x,y,7);

}

getch();

closegraph();

}

Output-:

putpixel(x,y,7);

}

getch();

closegraph();

}

Output-:

putpixel(x,y,7);

}

getch();

closegraph();

}

Output-:

Page 13: Computer Graphics & Multimedia CSP- · PDF fileWAP to draw a line using DDA ... WAP to fill the triangle using Boundary Fill 4 connected Algorithm. 13. WAP to ... •Arc draws a circular

Program:5

/*WAP to draw a line using Bresenhman function (when slop<=1).*/

#include<conio.h>

#include<math.h>

#include<iostream.h>

#include<graphics.h>

void main()

{

int x,y,x1,x2,y1,y2,k,dx,dy,s,inc1,inc2,d,xend;

int gdriver=DETECT,gmode;

initgraph(&gdriver,&gmode,”…\\bgi”);

cout<<“enter first point”;

cin>>x1>>y1;

cout<<“enter second point”;

cin>>x2>>y2;

dx=x2-x1;

dy=y2-y1;

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

{

inc1=2*dy;

inc2=2*(dy-dx);

d=inc1-dx;

if(dx<0)

{

x=x2;

y=y2;

xend=x1;

}

else

{

x=x1;

y=y1;

Page 14: Computer Graphics & Multimedia CSP- · PDF fileWAP to draw a line using DDA ... WAP to fill the triangle using Boundary Fill 4 connected Algorithm. 13. WAP to ... •Arc draws a circular

xend=x2;

}

for(k=0;k<=xend;k++)

{

putpixel(x,y,6);

if(d>=0)

{

y=y+1;

d=d+inc2;

}

else

{

d=d+inc1;

}

x=x+1;

}

}

else

{

cout<<”enter slop less than 1”;

}

getch();

closegraph();

}

Page 15: Computer Graphics & Multimedia CSP- · PDF fileWAP to draw a line using DDA ... WAP to fill the triangle using Boundary Fill 4 connected Algorithm. 13. WAP to ... •Arc draws a circular

Output-:Output-:Output-:

Page 16: Computer Graphics & Multimedia CSP- · PDF fileWAP to draw a line using DDA ... WAP to fill the triangle using Boundary Fill 4 connected Algorithm. 13. WAP to ... •Arc draws a circular

Program:6

/*WAP to draw a line using bresenham function (when slop>=1).*/

#include<conio.h>

#include<math.h>

#include<iostream.h>

#include<graphics.h>

void main()

{

int x,y,x1,x2,y1,y2,k,dx,dy,s,inc1,inc2,d,yend;

int gdriver=DETECT,gmode;

initgraph(&gdriver,&gmode,”…\\bgi”);

cout<<”enter first point”;

cin>>x1>>y1;

cout<<”enter second point”;

cin>>x2>>y2;

dx=x2-x1;

dy=y2-y1;

if(abs(dx)<abs(dy))

{

inc1=2*dx;

inc2=2*(dx-dy);

d=inc1-dy;

if(dy<0)

{

x=x2;

y=y2;

yend=y1;

}

else

{

x=x1;

Page 17: Computer Graphics & Multimedia CSP- · PDF fileWAP to draw a line using DDA ... WAP to fill the triangle using Boundary Fill 4 connected Algorithm. 13. WAP to ... •Arc draws a circular

y=y1;

yend=y2;

}

for(k=0;k<=yend;k++)

{

putpixel(x,y,4);

if(d>=0)

{

x=x+1;

d=d+inc2;

}

else

{

d=d+inc1;

}

y=y+1;

}

}

else

{

cout<<”enter slop greater than 1”;

}

getch();

closegraph();}

Page 18: Computer Graphics & Multimedia CSP- · PDF fileWAP to draw a line using DDA ... WAP to fill the triangle using Boundary Fill 4 connected Algorithm. 13. WAP to ... •Arc draws a circular

Output-:Output-:Output-:

Page 19: Computer Graphics & Multimedia CSP- · PDF fileWAP to draw a line using DDA ... WAP to fill the triangle using Boundary Fill 4 connected Algorithm. 13. WAP to ... •Arc draws a circular

Program:7

/*WAP to draw a circle using Polynomial function.*/

#include<conio.h>

#include<math.h>

#include<iostream.h>

#include<graphics.h>

void main()

{

int x,y,r,h,k,s,xend;

int gdriver=DETECT,gmode;

initgraph(&gdriver,&gmode,"...\\bgi:");

cout<<"enter the center of circle";

cin>>h>>k;

cout<<"enter the radius of circle";

cin>>r;

x=0;

y=r;

xend=r/1.414;

for(x=0;x<=xend;x++)

{

y=sqrt(r*r-x*x);

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

}

getch();

closegraph();

}

Page 20: Computer Graphics & Multimedia CSP- · PDF fileWAP to draw a line using DDA ... WAP to fill the triangle using Boundary Fill 4 connected Algorithm. 13. WAP to ... •Arc draws a circular

Output-:Output-:Output-:

Page 21: Computer Graphics & Multimedia CSP- · PDF fileWAP to draw a line using DDA ... WAP to fill the triangle using Boundary Fill 4 connected Algorithm. 13. WAP to ... •Arc draws a circular

Program:8

/*WAP to draw a circle using Bresenham’s function.*/

#include<conio.h>

#include<math.h>

#include<iostream.h>

#include<graphics.h>

void main()

{

int h,k,r,x,y,d;

int gdriver=DETECT,gmode;

initgraph(&gdriver,&gmode,"...\\bgi:");

cout<<"enter the center of circle";

cin>>h>>k;

cout<<"enter the radius of circle";

cin>>r;

x=0;

y=r;

d=(3-(2*r));

for(x=0;x<=y;x++)

{

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

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

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

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

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

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

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

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

if(d<0)

{

d=(d+(4*x)+6);

}

Page 22: Computer Graphics & Multimedia CSP- · PDF fileWAP to draw a line using DDA ... WAP to fill the triangle using Boundary Fill 4 connected Algorithm. 13. WAP to ... •Arc draws a circular

else

{

d=(d+(4*(x-y))+10);

y=y-1;

}

}

getch();

closegraph();

}

Output-:

else

{

d=(d+(4*(x-y))+10);

y=y-1;

}

}

getch();

closegraph();

}

Output-:

else

{

d=(d+(4*(x-y))+10);

y=y-1;

}

}

getch();

closegraph();

}

Output-:

Page 23: Computer Graphics & Multimedia CSP- · PDF fileWAP to draw a line using DDA ... WAP to fill the triangle using Boundary Fill 4 connected Algorithm. 13. WAP to ... •Arc draws a circular

Program:9

/*WAP to draw a circle using Trigonometric function.*/

#include<conio.h>

#include<math.h>

#include<iostream.h>

#include<graphics.h>

void main()

{

int r,h,k,theta,thetaend;

double angle,temp,thetainc,x,y;

int gdriver=DETECT,gmode;

initgraph(&gdriver,&gmode,"...\\bgi:");

cout<<"enter the center of circle";

cin>>h>>k;

cout<<"enter the radius of circle";

cin>>r;

temp=3.141/180;

thetainc=1/r;

theta=0;

thetaend=45;

for(theta=0;theta<=thetaend;theta++)

{

angle=temp*theta;

x=r*cos(angle);

y=r*sin(angle);

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

Page 24: Computer Graphics & Multimedia CSP- · PDF fileWAP to draw a line using DDA ... WAP to fill the triangle using Boundary Fill 4 connected Algorithm. 13. WAP to ... •Arc draws a circular

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

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

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

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

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

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

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

theta=theta+thetainc;

}

getch();

closegraph();

}

Output-:

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

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

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

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

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

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

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

theta=theta+thetainc;

}

getch();

closegraph();

}

Output-:

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

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

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

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

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

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

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

theta=theta+thetainc;

}

getch();

closegraph();

}

Output-:

Page 25: Computer Graphics & Multimedia CSP- · PDF fileWAP to draw a line using DDA ... WAP to fill the triangle using Boundary Fill 4 connected Algorithm. 13. WAP to ... •Arc draws a circular

Program:10

/*WAP to draw an ellipse using Polynomial function.*/

#include<conio.h>

#include<iostream.h>

#include<graphics.h>

#include<math.h>

void main()

{

int h,k;

double x,a,y,b,c,x1,x2,xend ;

int gdriver=DETECT,gmode;

initgraph(&gdriver,&gmode,"...\\bgi");

cout<<"enter center of ellipse";

cin>>h>>k;

cout<<"enter major and minor axis of ellipse";

cin>>x1>>x2;

a=x1/2;

b=x2/2;

x=0.0;

xend=a;

for(x=0;x<=xend;x=x+1)

{

y=b*sqrt(1-(x*x)/(a*a));

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

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

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

Page 26: Computer Graphics & Multimedia CSP- · PDF fileWAP to draw a line using DDA ... WAP to fill the triangle using Boundary Fill 4 connected Algorithm. 13. WAP to ... •Arc draws a circular

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

}

getch();

closegraph();

}

Output-:

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

}

getch();

closegraph();

}

Output-:

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

}

getch();

closegraph();

}

Output-:

Page 27: Computer Graphics & Multimedia CSP- · PDF fileWAP to draw a line using DDA ... WAP to fill the triangle using Boundary Fill 4 connected Algorithm. 13. WAP to ... •Arc draws a circular

Program:11

/*WAP to draw an ellipse using Trigonometric function.*/

#include<conio.h>

#include<iostream.h>

#include<graphics.h>

#include<math.h>

void main()

{

int h,k,xend,thetaend,a,b;

double x,y,c,x1,x2,temp,thetainc,theta,angle ;

int gdriver=DETECT,gmode;

initgraph(&gdriver,&gmode,"...\\bgi");

cout<<"enter center of ellipse";

cin>>h>>k;

cout<<"enter major and minor axis of ellipse";

cin>>x1>>x2;

a=x1/2;

b=x2/2;

x=0.0;

temp=3.141/180;

thetainc=1.571;

theta=0;

thetaend=90;

for(theta=0.0;theta<=thetaend;theta=theta+thetainc)

{

Page 28: Computer Graphics & Multimedia CSP- · PDF fileWAP to draw a line using DDA ... WAP to fill the triangle using Boundary Fill 4 connected Algorithm. 13. WAP to ... •Arc draws a circular

angle=temp*theta;

x=a*cos(angle);

y=b*sin(angle);

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

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

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

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

}

getch();

closegraph();

}

Page 29: Computer Graphics & Multimedia CSP- · PDF fileWAP to draw a line using DDA ... WAP to fill the triangle using Boundary Fill 4 connected Algorithm. 13. WAP to ... •Arc draws a circular

Output-:Output-:Output-:

Page 30: Computer Graphics & Multimedia CSP- · PDF fileWAP to draw a line using DDA ... WAP to fill the triangle using Boundary Fill 4 connected Algorithm. 13. WAP to ... •Arc draws a circular

PROGRAM – 12

/* WAP to fill the triangle with Boundary Fill using 4-connected Fill Method */

#include<iostream.h>

#include<conio.h>

#include<math.h>

#include<graphics.h>

void main()

{

void BoundaryFill(const int,const int,const int,const int);

int gdriver = DETECT, gmode;

initgraph(&gdriver,&gmode,"...//bgi");

int x,y;

cout<<"Enter the boundary color";

cin>>x;

setcolor(x);

line(100,150,200,150);

line(200,150,150,75);

line(150,75,100,150);

cout<<"Enter the interior color";

cin>>y;

if(x==y)

{

cout<<"enter different values for interior/boundary color";

}

Page 31: Computer Graphics & Multimedia CSP- · PDF fileWAP to draw a line using DDA ... WAP to fill the triangle using Boundary Fill 4 connected Algorithm. 13. WAP to ... •Arc draws a circular

else

{

BoundaryFill(110,140,y,x);

}

getch();

closegraph();

}

struct Pixel

{

int x;

int y;

Pixel *Next;

};

Pixel *Entry=NULL;

Pixel *Start=NULL;

Pixel *Last=NULL;

void insert_pixel(const int x,const int y)

{

Entry=new Pixel;

Entry->x=x;

Entry->y=y;

Entry->Next=NULL;

Last->Next=Entry;

Last=Entry;

}

void BoundaryFill(const int _x,const int _y,const int fill_color,const int boundary_color)

{

Page 32: Computer Graphics & Multimedia CSP- · PDF fileWAP to draw a line using DDA ... WAP to fill the triangle using Boundary Fill 4 connected Algorithm. 13. WAP to ... •Arc draws a circular

if(getpixel(_x,_y)==boundary_color || getpixel(_x,_y)==fill_color)

return;

int x=_x;

int y=_y;

Start=new Pixel;

Start->x=x;

Start->y=y;

Start->Next=NULL;

Last=Start;

while(Start!=NULL)

{

putpixel(x,y,fill_color);

if(getpixel((x-1),y)!=boundary_color &&getpixel((x-1),y)!=fill_color&& (x-1)>=0)

{

putpixel((x-1),y,fill_color);

insert_pixel((x-1),y);

}

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

getpixel(x,(y-1))!=fill_color&& (y-1)>=0)

{

putpixel(x,(y-1),fill_color);

insert_pixel(x,(y-1));

}

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

getpixel((x+1),y)!=fill_color&& (x+1)<=getmaxx( ))

{

Page 33: Computer Graphics & Multimedia CSP- · PDF fileWAP to draw a line using DDA ... WAP to fill the triangle using Boundary Fill 4 connected Algorithm. 13. WAP to ... •Arc draws a circular

putpixel((x+1),y,fill_color);

insert_pixel((x+1),y);

}

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

getpixel(x,(y+1))!=fill_color&& (y-1)<=getmaxy( ))

{

putpixel(x,(y+1),fill_color);

insert_pixel(x,(y+1));

}

Entry=Start;

Start=Start->Next;

x=Start->x;

y=Start->y;

delete Entry;

}

}

Page 34: Computer Graphics & Multimedia CSP- · PDF fileWAP to draw a line using DDA ... WAP to fill the triangle using Boundary Fill 4 connected Algorithm. 13. WAP to ... •Arc draws a circular

OUTPUT :

Page 35: Computer Graphics & Multimedia CSP- · PDF fileWAP to draw a line using DDA ... WAP to fill the triangle using Boundary Fill 4 connected Algorithm. 13. WAP to ... •Arc draws a circular

PROGRAM – 13

/* WAP to fill the triangle with FloodFill 4-connected Fill Method */

#include<iostream.h>

#include<conio.h>

#include<math.h>

#include<graphics.h>

void main()

{

void FloodFill(const int,const int,const int,const int);

int gdriver = DETECT, gmode;

initgraph(&gdriver,&gmode,"...//bgi");

int x,y;

setcolor(6);

line(100,150,200,150);

setcolor(4);

line(200,150,150,75);

line(150,75,100,150);

cout<<"Enter the interior color";

cin>>y;

FloodFill(110,140,y,10);

getch();

closegraph();

}

struct Pixel

{

Page 36: Computer Graphics & Multimedia CSP- · PDF fileWAP to draw a line using DDA ... WAP to fill the triangle using Boundary Fill 4 connected Algorithm. 13. WAP to ... •Arc draws a circular

int x;

int y;

Pixel *Next;

};

Pixel *Entry=NULL;

Pixel *Start=NULL;

Pixel *Last=NULL;

void insert_pixel(const int x,const int y)

{

Entry=new Pixel;

Entry->x=x;

Entry->y=y;

Entry->Next=NULL;

Last->Next=Entry;

Last=Entry;

}

void FloodFill(constint _x,constint _y, constint fill_color,constint old_color)

{

if(old_color==fill_color)

return;

int x=_x;

int y=_y;

Start=new Pixel;

Page 37: Computer Graphics & Multimedia CSP- · PDF fileWAP to draw a line using DDA ... WAP to fill the triangle using Boundary Fill 4 connected Algorithm. 13. WAP to ... •Arc draws a circular

Start->x=x;

Start->y=y;

Start->Next=NULL;

Last=Start;

while(Start!=NULL)

{

putpixel(x,y,fill_color);

if(getpixel((x-1),y)==old_color && (x-1)>=0)

{

putpixel((x-1),y,fill_color);

insert_pixel((x-1),y);

}

if(getpixel(x,(y-1))==old_color && (y-1)>=0)

{

putpixel(x,(y-1),fill_color);

insert_pixel(x,(y-1));

}

if(getpixel((x+1),y)==old_color && (x+1)<=getmaxx( ))

{

putpixel((x+1),y,fill_color);

insert_pixel((x+1),y);

Page 38: Computer Graphics & Multimedia CSP- · PDF fileWAP to draw a line using DDA ... WAP to fill the triangle using Boundary Fill 4 connected Algorithm. 13. WAP to ... •Arc draws a circular

}

if(getpixel(x,(y+1))==old_color && (y+1)<=getmaxy( ))

{

putpixel(x,(y+1),fill_color);

insert_pixel(x,(y+1));

}

Entry=Start;

Start=Start->Next;

x=Start->x;

y=Start->y;

delete Entry;

}

}

Page 39: Computer Graphics & Multimedia CSP- · PDF fileWAP to draw a line using DDA ... WAP to fill the triangle using Boundary Fill 4 connected Algorithm. 13. WAP to ... •Arc draws a circular

PROGRAM 14

/*Write a program to implement translation of a triangle.*/

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

void main()

{

int x1, y1, x2, y2, x3, y3, tx, ty;

int gdriver = DETECT, gmode;

initgraph(&gdriver, &gmode, "…\\BGI ");

cout<<"\nEnter the first coordinate: ";

cin>>x1>>y1;

cout<<"\nEnter the second coordinate: ";

cin>>x2>>y2;

cout<<"\nEnter the third coordinate: ";

cin>>x3>>y3;

cout<<"\nEnter the translation coordinates: ";

cin>>tx>>ty;

line(x1, y1, x2, y2);

line(x2, y2, x3, y3);

Page 40: Computer Graphics & Multimedia CSP- · PDF fileWAP to draw a line using DDA ... WAP to fill the triangle using Boundary Fill 4 connected Algorithm. 13. WAP to ... •Arc draws a circular

line(x3, y3, x1, y1);

line(x1 + tx, y1 + ty, x2 + tx, y2 + ty);

line(x2 + tx, y2 + ty, x3 + tx, y3 + ty);

line(x3 + tx, y3 + ty, x1 + tx, y1 + ty);

getch();

closegraph();

}

OUTPUT

Page 41: Computer Graphics & Multimedia CSP- · PDF fileWAP to draw a line using DDA ... WAP to fill the triangle using Boundary Fill 4 connected Algorithm. 13. WAP to ... •Arc draws a circular

PROGRAM 15

/*Write a program to implement translation of a circle.*/

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

void main()

{

int x, y, r, tx, ty;

int gdriver = DETECT, gmode;

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

cout<<"\nEnter the radius: ";

cin>>r;

cout<<"\nEnter the coordinates of the center: ";

cin>>x>>y;

circle(x, y, r);

cout<<"\nEnter the translation coordinates: ";

cin>>tx>>ty;

circle(x + tx, y + ty, r);

getch();

closegraph();

Page 42: Computer Graphics & Multimedia CSP- · PDF fileWAP to draw a line using DDA ... WAP to fill the triangle using Boundary Fill 4 connected Algorithm. 13. WAP to ... •Arc draws a circular

}

OUTPUT

Page 43: Computer Graphics & Multimedia CSP- · PDF fileWAP to draw a line using DDA ... WAP to fill the triangle using Boundary Fill 4 connected Algorithm. 13. WAP to ... •Arc draws a circular

Practical No. 16

/*Write a program to implement rotation of line about origin.*/

#include<iostream.h>

#include<conio.h>

#include<graphics.h>

#include<math.h>

void main()

{

clrscr();

int gdriver=DETECT,gmode,theta;

float x1,y1,x2,y2,x11,y11,x22,y22;

initgraph(&gdriver,&gmode," ");

cout<<"enter the co-ordinates of first point:";

cin>>x1>>y1;

cout<<"enter the co-ordinates of second point:";

cin>>x2>>y2;

line(x1,y1,x2,y2);

getch();

cout<<"enter the rotation angle:";

cin>>theta;

Page 44: Computer Graphics & Multimedia CSP- · PDF fileWAP to draw a line using DDA ... WAP to fill the triangle using Boundary Fill 4 connected Algorithm. 13. WAP to ... •Arc draws a circular

theta=theta*3.14/100;

x11=(x1*sin(theta))+(y1*cos(theta));

y11=(x1*cos(theta))-(y1*sin(theta));

x22=(x2*sin(theta))+(y2*cos(theta));

y22=(x2*cos(theta))-(y2*sin(theta));

line(x11,y11,x22,y22);

getch();

closegraph();

}

Page 45: Computer Graphics & Multimedia CSP- · PDF fileWAP to draw a line using DDA ... WAP to fill the triangle using Boundary Fill 4 connected Algorithm. 13. WAP to ... •Arc draws a circular

OUTPUT

Enter the coordinates of first point: 101 50

Enter the coordinates of second point: 367 78

Enter the rotation angle: 38

Page 46: Computer Graphics & Multimedia CSP- · PDF fileWAP to draw a line using DDA ... WAP to fill the triangle using Boundary Fill 4 connected Algorithm. 13. WAP to ... •Arc draws a circular

PROGRAM 17

/*WAP TO SHOW LINE CLIPPING*/

.

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<iostream.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;

}

Page 47: Computer Graphics & Multimedia CSP- · PDF fileWAP to draw a line using DDA ... WAP to fill the triangle using Boundary Fill 4 connected Algorithm. 13. WAP to ... •Arc draws a circular

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

Page 48: Computer Graphics & Multimedia CSP- · PDF fileWAP to draw a line using DDA ... WAP to fill the triangle using Boundary Fill 4 connected Algorithm. 13. WAP to ... •Arc draws a circular

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();

Page 49: Computer Graphics & Multimedia CSP- · PDF fileWAP to draw a line using DDA ... WAP to fill the triangle using Boundary Fill 4 connected Algorithm. 13. WAP to ... •Arc draws a circular

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;

}

Page 50: Computer Graphics & Multimedia CSP- · PDF fileWAP to draw a line using DDA ... WAP to fill the triangle using Boundary Fill 4 connected Algorithm. 13. WAP to ... •Arc draws a circular

OUTPUT

enter the value of x1,y1,x2,y2:__>1010100100enter the value of xmax,ymax,xmin,ymin505000

Page 51: Computer Graphics & Multimedia CSP- · PDF fileWAP to draw a line using DDA ... WAP to fill the triangle using Boundary Fill 4 connected Algorithm. 13. WAP to ... •Arc draws a circular

PROGRAM 18

/*PROGRAM TO REFLECT A TRIANGLE*/

#include<iostream.h>

#include<conio.h>

#include<graphics.h>

#include<process.h>

#include<math.h>

void main()

{

clrscr();

int graphdriver=DETECT,graphmode;

initgraph(&graphdriver,&graphmode,"...\\bgi");

int x,y,x1,a[3][3];

double b[3][3],c[3][3];

cout<<"\n Enter Ist coordinates of triangle:";

cin>>a[0][0]>>a[1][0];

cout<<"\n Enter 2nd coordinates of triangle:";

cin>>a[0][1]>>a[1][1];

cout<<"\n Enter 3rd coordinates of triangle:";

cin>>a[0][2]>>a[1][2];

cout<<"\n Enter 1. for reflection in x-axis:\n";

cout<<"\n Enter 2. for reflection in y-axis:\n";

cout<<"\n Enter 3. for reflection in both the axis:\n";

cin>>x;

cleardevice();

Page 52: Computer Graphics & Multimedia CSP- · PDF fileWAP to draw a line using DDA ... WAP to fill the triangle using Boundary Fill 4 connected Algorithm. 13. WAP to ... •Arc draws a circular

line(320,0,320,479);

line(0,240,639,240);

line(a[0][0],a[1][0],a[0][1],a[1][1]);

line(a[0][1],a[1][1],a[0][2],a[1][2]);

line(a[0][0],a[1][0],a[0][2],a[1][2]);

switch(x)

{

case 1:b[0][0]=640-a[0][0];

b[0][1]=640-a[0][1];

b[0][2]=640-a[0][2];

b[1][0]=a[1][0];

b[1][1]=a[1][1];

b[1][2]=a[1][2];

line(320,0,320,479);

line(0,240,639,240);

line(b[0][0],b[1][0],b[0][1],b[1][1]);

line(b[0][1],b[1][1],b[0][2],b[1][2]);

line(b[0][0],b[1][0],b[0][2],b[1][2]);

getch();

break;

case 2:b[1][0]=480-a[1][0];

b[1][1]=480-a[1][1];

b[1][2]=480-a[1][2];

b[0][0]=a[0][0];

b[0][1]=a[0][1];

b[0][2]=a[0][2];

line(320,0,320,479);

Page 53: Computer Graphics & Multimedia CSP- · PDF fileWAP to draw a line using DDA ... WAP to fill the triangle using Boundary Fill 4 connected Algorithm. 13. WAP to ... •Arc draws a circular

line(0,240,639,240);

line(b[0][0],b[1][0],b[0][1],b[1][1]);

line(b[0][1],b[1][1],b[0][2],b[1][2]);

line(b[0][0],b[1][0],b[0][2],b[1][2]);

getch();

break;

case 3: b[0][0]=640-a[0][0];

b[0][1]=640-a[0][1];

b[0][2]=640-a[0][2];

b[1][0]=a[1][0];

b[1][1]=a[1][1];

b[1][2]=a[1][2];

line(320,0,320,479);

line(0,240,639,240);

line(b[0][0],b[1][0],b[0][1],b[1][1]);

line(b[0][1],b[1][1],b[0][2],b[1][2]);

line(b[0][0],b[1][0],b[0][2],b[1][2]);

b[1][0]=480-a[1][0];

b[1][1]=480-a[1][1];

b[1][2]=480-a[1][2];

b[0][0]=a[0][0];

b[0][1]=a[0][1];

b[0][2]=a[0][2];

line(320,0,320,479);

line(0,240,639,240);

line(b[0][0],b[1][0],b[0][1],b[1][1]);

line(b[0][1],b[1][1],b[0][2],b[1][2]);

Page 54: Computer Graphics & Multimedia CSP- · PDF fileWAP to draw a line using DDA ... WAP to fill the triangle using Boundary Fill 4 connected Algorithm. 13. WAP to ... •Arc draws a circular

line(b[0][0],b[1][0],b[0][2],b[1][2]);

getch();

break;

}

getch();

closegraph();

}

Page 55: Computer Graphics & Multimedia CSP- · PDF fileWAP to draw a line using DDA ... WAP to fill the triangle using Boundary Fill 4 connected Algorithm. 13. WAP to ... •Arc draws a circular

Program:19

PROGRAM TO PERFORM SHEARING OF A RECTANGLE

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

void main()

{

int gdriver=DETECT,gmode;

int l,t,r,b,sx,sy;

int x1,y1,x2,y2,x3,y3,x4,y4;

initgraph(&gdriver,&gmode,"…\\bgi");

cout<<”Enter the coordinates of rectangle : ";

cin>>l>>t,>>r,>>b;

// cleardevice();

rectangle(l,t,r,b);

//Shear along x-axis

cout<<”Enter the x-direction shear factor : ";

cin>>sx;

x1=l+sx*t-sx*t;

y1=t;

x2=r+sx*t-sx*t;

y2=t;

Page 56: Computer Graphics & Multimedia CSP- · PDF fileWAP to draw a line using DDA ... WAP to fill the triangle using Boundary Fill 4 connected Algorithm. 13. WAP to ... •Arc draws a circular

x3=l+sx*b-sx*t;

y3=b;

x4=r+sx*b-sx*t;

y4=b;

line(x1,y1,x2,y2);

line(x2,y2,x4,y4);

line(x4,y4,x3,y3);

line(x1,y1,x3,y3);

x1=y1=x 2=y2=x3=y3=x 4=y4=0;

//Shear along y-axis

// cleardevice();

// rectangle(l,t,r,b);

cout<<”Enter the y-dir ection shear factor : ";

cin>>sy;

x1=l;

y1=t+sy*l-sy*l;

x2=r;

y2=t+sy*r-sy*l;

x3=r;

y3=b+sy*r-sy*l;

x4=l;

y4=b+sy*l-sy*l;

line(x1,y1,x2,y2);

Page 57: Computer Graphics & Multimedia CSP- · PDF fileWAP to draw a line using DDA ... WAP to fill the triangle using Boundary Fill 4 connected Algorithm. 13. WAP to ... •Arc draws a circular

line(x2,y2,x3,y3);

line(x4,y4,x3,y3);

line(x1,y1,x4,y4);

getch();

closegraph();

}