LAB MANUAL...LAB MANUAL Course : CS1544 Computer Graphics Lab Class : B.Sc Computer Science Semester...

34
LAB MANUAL Course : CS1544 Computer Graphics Lab Class : B.Sc Computer Science Semester : 5 Prepared By : Nidhi S COLLEGE OF APPLIED SCIENCE PERISSERY

Transcript of LAB MANUAL...LAB MANUAL Course : CS1544 Computer Graphics Lab Class : B.Sc Computer Science Semester...

Page 1: LAB MANUAL...LAB MANUAL Course : CS1544 Computer Graphics Lab Class : B.Sc Computer Science Semester : 5 Prepared By : Nidhi S COLLEGE OF APPLIED SCIENCE PERISSERY CS 1544: Computer

LAB MANUAL

Course : CS1544 Computer Graphics Lab

Class : B.Sc Computer Science

Semester : 5

Prepared By : Nidhi S

COLLEGE OF APPLIED SCIENCE

PERISSERY

Page 2: LAB MANUAL...LAB MANUAL Course : CS1544 Computer Graphics Lab Class : B.Sc Computer Science Semester : 5 Prepared By : Nidhi S COLLEGE OF APPLIED SCIENCE PERISSERY CS 1544: Computer

CS 1544: Computer Graphics Lab

P a g e 2 | 34

TABLE OF CONTENTS

1. Familiarisation of “graphics.h” ……………………………………………03

2. Drawing a car ……………………………………………………………...06

3. Simulation of a car …………………………………………………………08

4. Ball bouncing ……………………………………………………………...10

5. DDA line drawing algorithm ………………………………………………12

6. Bresenham’s line drawing algorithm ………………………………………14

7. Midpoint circle drawing algorithm ………………………………………...17

8. Flood fill algorithm ………………………………………………………..20

9. Boundary fill algorithm ……………………………………………………22

10. Setfillstyle.…………………………………………………………………24

11. Chessboard ………………………………………………………………...26

12. Two-dimensional translation ………………………………………………28

13. Two-dimensional rotation …………………………………………………30

14. Two-dimensional scaling ………………………………………………….32

15. Man movement …………………………………………………………….34

Page 3: LAB MANUAL...LAB MANUAL Course : CS1544 Computer Graphics Lab Class : B.Sc Computer Science Semester : 5 Prepared By : Nidhi S COLLEGE OF APPLIED SCIENCE PERISSERY CS 1544: Computer

CS 1544: Computer Graphics Lab

P a g e 3 | 34

1. FAMILIARISATION OF “GRAPHICS.H”

Computer graphic is an art of drawing pictures on computer screen with the help of

programming. The computer graphic is one of the most effective and commonly used way to

communicate the processed information to the user. It displays the information in the form of

graphical objects such as pictures ,charts ,graphs and diagrams instead of simple text.

Graphics mode Initialization

First of all we have to call the initgraph function that will intialize the graphics mode

on the computer. initigraph have the following prototype.

void initgraph(int far *graphdriver, int far *graphmode, char far *pathtodriver);

Initgraph initializes the graphics system by loading a graphics driver from disk (or

validating a registered driver) then putting the system into graphics mode. Initgraph also resets

all graphics settings (color, palette, current position, viewport, etc.) to their defaults, then resets

graphresult to 0.

*graphdriver - Integer that specifies the graphics driver to be used. You can give

graphdriver a value using a constant of the graphics_drivers enumeration type.

*graphmode - Integer that specifies the initial graphics mode (unless *graphdriver =

DETECT). If *graphdriver = DETECT, initgraph sets *graphmode to the highest resolution

available for the detected driver.

*pathtodriver Specifies the directory path where initgraph looks for graphics drivers

(*.BGI) first.

After a call to initgraph, *graphdriver is set to the current graphics driver, and

*graphmode is set to the current graphics mode.

Sample program

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

void main()

Page 4: LAB MANUAL...LAB MANUAL Course : CS1544 Computer Graphics Lab Class : B.Sc Computer Science Semester : 5 Prepared By : Nidhi S COLLEGE OF APPLIED SCIENCE PERISSERY CS 1544: Computer

CS 1544: Computer Graphics Lab

P a g e 4 | 34

{

int gd = DETECT, gm;

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

line(100, 100, 200, 200);

getch();

closegraph();

}

Basic functions in “graphics.h”

cleardevice() - Clears all previous graphical outputs generated by the previous programs.

gotoxy() - Initialize the graphics cursor to the specified co-ordiante

putpixel() - It will colour the pixel specified by the co-ordinates.

Syntax: putpixel(x,y,WHITE)

outtextxy() This method is used to display a text in any position on the screen.

Syntax: outtextxy(x,y,"HELLO")

rectangle() - Draws a rectangle according to the given parameter x and y are the top-left

corner and right bottom corner co-ordinates.

Syntax : rectangle(int left, int top, int right, int bottom)

circle() - Draws a circle with x,y as the center

Syntax: circle(x,y,radius)

line() - Draws a line as per the given co-ordinates.

Syntax : line(int startx, int starty, int endx, int endy)

moveto() - Cursor is moved from the current location to the specified location dx,dy.These

parameters can also be used as incremental values.

Syntax : moveto(dx,dy)

lineto() - Draws a line from its current location to the co-ordinate(x,y)

Syntax : lineto(x,y)

ellipse() - Draws the ellipse with the specified angles and coordinates.

Syntax:ellipse(xcentre,ycenter,starting_angle,ending_angle,x_radius,y_radius)

drawpoly() - Draws a polygon with (num_of_points +1) edges.The array 'points', int points[

]=(x1,y1,x2,y2,x3,y3...)

Syntax : drawpoly(num_of_points + 1, points)

settextstyle()

Page 5: LAB MANUAL...LAB MANUAL Course : CS1544 Computer Graphics Lab Class : B.Sc Computer Science Semester : 5 Prepared By : Nidhi S COLLEGE OF APPLIED SCIENCE PERISSERY CS 1544: Computer

CS 1544: Computer Graphics Lab

P a g e 5 | 34

The fonts available are :TRIPLEX_FONT, SMALL_FONT,SANS_SERIE_FONT,

GOTHIC_FONT. The direction can be changed as HORIZ_DIR or VERT_DIR,

The charecter size increases from 1 to 10

Syntax : settextstyle(DEFAULT_FONT,HORIZ_DIR,1)

setfillstyle()

The fill styles avaliable are SOLID_FILL, LINE_FILL, HATCH_FILL,

SLASH_FILL etc.

Syntax : setfillstyle(SOLID_FILL,RED)

setcolor() Sets the color

Syntax : setcolor(color_name)

delay() - Cause a pause in execution of the program 1000ms= 1 second

Syntax : delay(100)

closegraph() - Terminates all graphics operations and revert the hardware back to the

normal mode.

Page 6: LAB MANUAL...LAB MANUAL Course : CS1544 Computer Graphics Lab Class : B.Sc Computer Science Semester : 5 Prepared By : Nidhi S COLLEGE OF APPLIED SCIENCE PERISSERY CS 1544: Computer

CS 1544: Computer Graphics Lab

P a g e 6 | 34

2. DRAWING A CAR

Aim:

To write a c program to draw a car using basic graphics functions.

Program:

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

void main()

{

int gd=DETECT, gm;

clrscr();

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

line(0,245,250,245);

line(0,200,210,200);

line(50,200,70,170);

line(70,170,140,170);

line(140,170,160,200);

line(85,170,85,200);

line(125,170,125,200);

line(0,200,0,230);

line(210,200,210,230);

line(0,230,50,230);

circle(65,230,15);

line(80,230,130,230);

circle(145,230,15);

line(210,230,160,230);

getch();

}

Page 7: LAB MANUAL...LAB MANUAL Course : CS1544 Computer Graphics Lab Class : B.Sc Computer Science Semester : 5 Prepared By : Nidhi S COLLEGE OF APPLIED SCIENCE PERISSERY CS 1544: Computer

CS 1544: Computer Graphics Lab

P a g e 7 | 34

Output:

Page 8: LAB MANUAL...LAB MANUAL Course : CS1544 Computer Graphics Lab Class : B.Sc Computer Science Semester : 5 Prepared By : Nidhi S COLLEGE OF APPLIED SCIENCE PERISSERY CS 1544: Computer

CS 1544: Computer Graphics Lab

P a g e 8 | 34

3. SIMULATION OF A CAR

Aim:

To write a c program to simulate car movement using basic graphics functions.

Program:

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

void main()

{

int i,gd=DETECT, gm;

clrscr();

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

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

{

line(0,245,650,245);

line(0+i,200,210+i,200);

line(50+i,200,70+i,170);

line(70+i,170,140+i,170);

line(140+i,170,160+i,200);

line(85+i,170,85+i,200);

line(125+i,170,125+i,200);

line(0+i,200,0+i,230);

line(210+i,200,210+i,230);

line(0+i,230,50+i,230);

circle(65+i,230,15);

line(80+i,230,130+i,230);

circle(145+i,230,15);

line(210+i,230,160+i,230);

cleardevice();

delay(5);

}

getch();

}

Page 9: LAB MANUAL...LAB MANUAL Course : CS1544 Computer Graphics Lab Class : B.Sc Computer Science Semester : 5 Prepared By : Nidhi S COLLEGE OF APPLIED SCIENCE PERISSERY CS 1544: Computer

CS 1544: Computer Graphics Lab

P a g e 9 | 34

Output:

Page 10: LAB MANUAL...LAB MANUAL Course : CS1544 Computer Graphics Lab Class : B.Sc Computer Science Semester : 5 Prepared By : Nidhi S COLLEGE OF APPLIED SCIENCE PERISSERY CS 1544: Computer

CS 1544: Computer Graphics Lab

P a g e 10 | 34

4. BALL BOUNCING

Aim:

To write a c program to illustrate ball bouncing using basic graphics functions.

Program:

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

void main()

{

int x,y=0,flag=1,t=400,gd=DETECT,gm;

clrscr();

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

for(x=40;x<600;x++)

{

cleardevice();

circle(x,y,30);

delay(40);

if(y>=400)

{

flag=0;

t=t-20;

}

if(y<=(400-t))

flag=1;

if(flag==1)

y=y+15;

else

y=y-15;

}

getch();

}

Page 11: LAB MANUAL...LAB MANUAL Course : CS1544 Computer Graphics Lab Class : B.Sc Computer Science Semester : 5 Prepared By : Nidhi S COLLEGE OF APPLIED SCIENCE PERISSERY CS 1544: Computer

CS 1544: Computer Graphics Lab

P a g e 11 | 34

Output:

Page 12: LAB MANUAL...LAB MANUAL Course : CS1544 Computer Graphics Lab Class : B.Sc Computer Science Semester : 5 Prepared By : Nidhi S COLLEGE OF APPLIED SCIENCE PERISSERY CS 1544: Computer

CS 1544: Computer Graphics Lab

P a g e 12 | 34

5. DDA LINE DRAWING ALGORITHM

Aim:

To write a c program to implement DDA line drawing algorithm.

Program:

#include<graphics.h>

#include<stdio.h>

#include<conio.h>

#include<math.h>

#define ROUND(a) ((int)(a+0.5))

void lineDDA(int,int,int,int);

int xa,ya,xb,yb;

void main()

{

int gd=DETECT,gm;

clrscr();

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

printf("Enter the co-ordinates of first endpoint:\n");

scanf("%d%d",&xa,&ya);

printf("Enter the co-ordinates of second endpoint:\n");

scanf("%d%d",&xb,&yb);

lineDDA(xa,ya,xb,yb);

getch();

}

void lineDDA(xa,ya,xb,yb)

{

int dx=xb-xa, dy=yb-ya, steps, k;

float xincr,yincr,x=xa,y=ya;

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

steps = abs(dx);

else

steps = abs(dy);

xincr = dx/(float)steps;

yincr = dy/(float)steps;

Page 13: LAB MANUAL...LAB MANUAL Course : CS1544 Computer Graphics Lab Class : B.Sc Computer Science Semester : 5 Prepared By : Nidhi S COLLEGE OF APPLIED SCIENCE PERISSERY CS 1544: Computer

CS 1544: Computer Graphics Lab

P a g e 13 | 34

putpixel(ROUND(x),ROUND(y),YELLOW);

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

{

x=x+xincr;

y=y+yincr;

putpixel(ROUND(x),ROUND(y),YELLOW);

}

}

Output:

Page 14: LAB MANUAL...LAB MANUAL Course : CS1544 Computer Graphics Lab Class : B.Sc Computer Science Semester : 5 Prepared By : Nidhi S COLLEGE OF APPLIED SCIENCE PERISSERY CS 1544: Computer

CS 1544: Computer Graphics Lab

P a g e 14 | 34

6. BRESENHAM’S LINE DRAWING ALGORITHM

Aim:

To write a c program to implement Bresenham’s line drawing algorithm.

Program:

#include<graphics.h>

#include<stdio.h>

#include<conio.h>

#include<math.h>

#define ROUND(a) ((int)(a+0.5))

void lineBres(int,int,int,int);

int xa,ya,xb,yb;

void main()

{

int gd=DETECT,gm;

clrscr();

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

printf("Enter the co-ordinates of first endpoint:\n");

scanf("%d%d",&xa,&ya);

printf("Enter the co-ordinates of second endpoint:\n");

scanf("%d%d",&xb,&yb);

lineBres(xa,ya,xb,yb);

getch();

}

void lineBres(xa,ya,xb,yb)

{

int dx=abs(xb-xa), dy=abs(yb-ya),p=2*dy-dx,Tdy=2*dy;

int Tdydx=2*(dy-dx),x,y,xend,yend;

if(xa>xb)

{

x=xb;

y=yb;

xend=xa;

yend=ya;

Page 15: LAB MANUAL...LAB MANUAL Course : CS1544 Computer Graphics Lab Class : B.Sc Computer Science Semester : 5 Prepared By : Nidhi S COLLEGE OF APPLIED SCIENCE PERISSERY CS 1544: Computer

CS 1544: Computer Graphics Lab

P a g e 15 | 34

}

else

{

x=xa;

y=ya;

xend=xb;

yend=yb;

}

putpixel(x,y,YELLOW);

while(x<xend)

{

x++;

if(p<0)

p=p+Tdy;

else

{

if(y>yend)

y--;

else

y++;

p=p+Tdydx;

}

putpixel(x,y,YELLOW);

}

}

Page 16: LAB MANUAL...LAB MANUAL Course : CS1544 Computer Graphics Lab Class : B.Sc Computer Science Semester : 5 Prepared By : Nidhi S COLLEGE OF APPLIED SCIENCE PERISSERY CS 1544: Computer

CS 1544: Computer Graphics Lab

P a g e 16 | 34

Output:

Page 17: LAB MANUAL...LAB MANUAL Course : CS1544 Computer Graphics Lab Class : B.Sc Computer Science Semester : 5 Prepared By : Nidhi S COLLEGE OF APPLIED SCIENCE PERISSERY CS 1544: Computer

CS 1544: Computer Graphics Lab

P a g e 17 | 34

7. MIDPOINT CIRCLE DRAWING ALGORITHM

Aim:

To write a c program to implement midpoint circle drawing algorithm.

Program:

#include<dos.h>

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

void draw_circle(int,int,int);

void symmetry(int,int,int,int);

void main()

{

int xc,yc,R,gd=DETECT,gm;

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

printf("Enter the center of the circle:\n");

printf("Xc =");

scanf("%d",&xc);

printf("Yc =");

scanf("%d",&yc);

printf("Enter the radius of the circle :");

scanf("%d",&R);

draw_circle(xc,yc,R);

getch();

closegraph();

}

void draw_circle(int xc,int yc,int rad)

{

int x=0,y=rad,p=1-rad;

symmetry(x,y,xc,yc);

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

{

if(p<0)

p=p+2*x+3;

Page 18: LAB MANUAL...LAB MANUAL Course : CS1544 Computer Graphics Lab Class : B.Sc Computer Science Semester : 5 Prepared By : Nidhi S COLLEGE OF APPLIED SCIENCE PERISSERY CS 1544: Computer

CS 1544: Computer Graphics Lab

P a g e 18 | 34

else

{

p=p+2*(x-y)+5;

y--;

}

symmetry(x,y,xc,yc);

delay(5);

}

}

void symmetry(int x,int y,int xc,int yc)

{

putpixel(xc+x,yc-y,GREEN);

delay(5);

putpixel(xc+y,yc-x,GREEN);

delay(5);

putpixel(xc+y,yc+x,GREEN);

delay(5);

putpixel(xc+x,yc+y,GREEN);

delay(5);

putpixel(xc-x,yc+y,GREEN);

delay(5);

putpixel(xc-y,yc+x,GREEN);

delay(5);

putpixel(xc-y,yc-x,GREEN);

delay(5);

putpixel(xc-x,yc-y,GREEN);

delay(5);

}

Page 19: LAB MANUAL...LAB MANUAL Course : CS1544 Computer Graphics Lab Class : B.Sc Computer Science Semester : 5 Prepared By : Nidhi S COLLEGE OF APPLIED SCIENCE PERISSERY CS 1544: Computer

CS 1544: Computer Graphics Lab

P a g e 19 | 34

Output:

Page 20: LAB MANUAL...LAB MANUAL Course : CS1544 Computer Graphics Lab Class : B.Sc Computer Science Semester : 5 Prepared By : Nidhi S COLLEGE OF APPLIED SCIENCE PERISSERY CS 1544: Computer

CS 1544: Computer Graphics Lab

P a g e 20 | 34

8. FLOOD FILL ALGORITHM

Aim:

To write a c program to implement flood fill algorithm.

Program:

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

void floodfill4(int,int,int,int);

void main()

{

int gd=DETECT,gm;

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

circle(100,100,20);

floodfill4(100,81,2,0);

getch();

}

void floodfill4(int x,int y,int fillcolor,int oldcolor)

{

if(getpixel(x,y)==oldcolor)

{

putpixel(x,y,fillcolor);

delay(5);

floodfill4(x+1,y,fillcolor,oldcolor);

floodfill4(x-1,y,fillcolor,oldcolor);

floodfill4(x,y+1,fillcolor,oldcolor);

floodfill4(x,y-1,fillcolor,oldcolor);

}

}

Page 21: LAB MANUAL...LAB MANUAL Course : CS1544 Computer Graphics Lab Class : B.Sc Computer Science Semester : 5 Prepared By : Nidhi S COLLEGE OF APPLIED SCIENCE PERISSERY CS 1544: Computer

CS 1544: Computer Graphics Lab

P a g e 21 | 34

Output:

Page 22: LAB MANUAL...LAB MANUAL Course : CS1544 Computer Graphics Lab Class : B.Sc Computer Science Semester : 5 Prepared By : Nidhi S COLLEGE OF APPLIED SCIENCE PERISSERY CS 1544: Computer

CS 1544: Computer Graphics Lab

P a g e 22 | 34

9. BOUNDARY FILL ALGORITHM

Aim:

To write a c program to implement boundary fill algorithm.

Program:

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

void boundaryfill4(int,int,int,int);

void main()

{

int gd=DETECT,gm;

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

setcolor(GREEN);

circle(100,100,20);

boundaryfill4(100,100,RED,GREEN);

getch();

}

void boundaryfill4(int x,int y,int fill,int boundary)

{

int current = getpixel(x,y);

if((current!=boundary)&&(current!=fill))

{

putpixel(x,y,fill);

delay(5);

boundaryfill4(x+1,y,fill,boundary);

boundaryfill4(x-1,y,fill,boundary);

boundaryfill4(x,y+1,fill,boundary);

boundaryfill4(x,y-1,fill,boundary);

}

}

Page 23: LAB MANUAL...LAB MANUAL Course : CS1544 Computer Graphics Lab Class : B.Sc Computer Science Semester : 5 Prepared By : Nidhi S COLLEGE OF APPLIED SCIENCE PERISSERY CS 1544: Computer

CS 1544: Computer Graphics Lab

P a g e 23 | 34

Output:

Page 24: LAB MANUAL...LAB MANUAL Course : CS1544 Computer Graphics Lab Class : B.Sc Computer Science Semester : 5 Prepared By : Nidhi S COLLEGE OF APPLIED SCIENCE PERISSERY CS 1544: Computer

CS 1544: Computer Graphics Lab

P a g e 24 | 34

10. SETFILLSTYLE

Aim:

To write a c program to demonstrate setfillstyle.

Program:

#include<graphics.h>

#include<conio.h>

void main()

{

int gd=DETECT, gm;

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

setfillstyle(XHATCH_FILL, RED);

circle(100,100,50);

floodfill(100,100,WHITE);

getch();

closegraph();

}

Output:

Page 25: LAB MANUAL...LAB MANUAL Course : CS1544 Computer Graphics Lab Class : B.Sc Computer Science Semester : 5 Prepared By : Nidhi S COLLEGE OF APPLIED SCIENCE PERISSERY CS 1544: Computer

CS 1544: Computer Graphics Lab

P a g e 25 | 34

The pattern value may be integer value or its corresponding pattern name in CAPS.

The patterns available are listed below:

o EMPTY_FILL 0

o SOLID_FILL 1

o LINE_FILL 2

o LTSLASH_FILL 3

o SLASH_FILL 4

o BKSLASH_FILL 5

o LTBKSLASH_FILL 6

o HATCH_FILL 7

o XHATCH_FILL 8

o INTERLEAVE_FILL 9

o WIDE_DOT_FILL 10

o CLOSE_DOT_FILL 11

o USER_FILL 12

The colour value may be integer value or its corresponding colour name in CAPS.

The colours available are listed below:

Page 26: LAB MANUAL...LAB MANUAL Course : CS1544 Computer Graphics Lab Class : B.Sc Computer Science Semester : 5 Prepared By : Nidhi S COLLEGE OF APPLIED SCIENCE PERISSERY CS 1544: Computer

CS 1544: Computer Graphics Lab

P a g e 26 | 34

11. CHESS BOARD

Aim:

To write a c program to draw the chess board.

Program:

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

void main()

{

int row,col,x=50,y=50,flag=0,gd=DETECT,gm;

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

printf("\n\t************* CHESS BOARD **************\n");

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

{

for(col=1;col<=8;col++)

{

if(flag==0)

{

setcolor(LIGHTGRAY);

setfillstyle(SOLID_FILL,BLACK);

rectangle(x,y,x+50,y+50);

floodfill(x+1,y+1,LIGHTGRAY);

flag=1;

}

else

{

setcolor(LIGHTGRAY);

setfillstyle(SOLID_FILL,WHITE);

rectangle(x,y,x+50,y+50);

floodfill(x+1,y+1,LIGHTGRAY);

flag=0;

}

x=x+50;

Page 27: LAB MANUAL...LAB MANUAL Course : CS1544 Computer Graphics Lab Class : B.Sc Computer Science Semester : 5 Prepared By : Nidhi S COLLEGE OF APPLIED SCIENCE PERISSERY CS 1544: Computer

CS 1544: Computer Graphics Lab

P a g e 27 | 34

}

if(flag==0)

flag=1;

else

flag=0;

delay(100);

x=50;

y=50+y;

}

getch();

closegraph();

}

Output:

Page 28: LAB MANUAL...LAB MANUAL Course : CS1544 Computer Graphics Lab Class : B.Sc Computer Science Semester : 5 Prepared By : Nidhi S COLLEGE OF APPLIED SCIENCE PERISSERY CS 1544: Computer

CS 1544: Computer Graphics Lab

P a g e 28 | 34

12. TWO DIMENSIONAL TRANSLATION

Aim:

To write a c program to demonstrate two dimensional translation.

Program:

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<math.h>

void main()

{

int x1,y1,x2,y2,tx,ty,x3,y3,x4,y4,gd=DETECT,gm;

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 translation distances tx,ty:\n");

scanf("%d%d",&tx,&ty);

setcolor(5);

line(x1,y1,x2,y2);

outtextxy(x2+2,y2+2,"Original line");

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

}

Page 29: LAB MANUAL...LAB MANUAL Course : CS1544 Computer Graphics Lab Class : B.Sc Computer Science Semester : 5 Prepared By : Nidhi S COLLEGE OF APPLIED SCIENCE PERISSERY CS 1544: Computer

CS 1544: Computer Graphics Lab

P a g e 29 | 34

Output:

Page 30: LAB MANUAL...LAB MANUAL Course : CS1544 Computer Graphics Lab Class : B.Sc Computer Science Semester : 5 Prepared By : Nidhi S COLLEGE OF APPLIED SCIENCE PERISSERY CS 1544: Computer

CS 1544: Computer Graphics Lab

P a g e 30 | 34

13. TWO DIMENSIONAL SCALING

Aim:

To write a c program to demonstrate two dimensional scaling.

Program:

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<math.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();

}

Page 31: LAB MANUAL...LAB MANUAL Course : CS1544 Computer Graphics Lab Class : B.Sc Computer Science Semester : 5 Prepared By : Nidhi S COLLEGE OF APPLIED SCIENCE PERISSERY CS 1544: Computer

CS 1544: Computer Graphics Lab

P a g e 31 | 34

Output:

Page 32: LAB MANUAL...LAB MANUAL Course : CS1544 Computer Graphics Lab Class : B.Sc Computer Science Semester : 5 Prepared By : Nidhi S COLLEGE OF APPLIED SCIENCE PERISSERY CS 1544: Computer

CS 1544: Computer Graphics Lab

P a g e 32 | 34

14. TWO DIMENSIONAL ROTATION

Aim:

To write a c program to demonstrate two dimensional rotation.

Program:

#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 point:\n");

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

printf("Enter coordinates of ending point\n");

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=abs((x1*cos(t))-(y1*sin(t)));

y3=abs((x1*sin(t))+(y1*cos(t)));

x4=abs((x2*cos(t))-(y2*sin(t)));

y4=abs((x2*sin(t))+(y2*cos(t)));

setcolor(7);

line(x3,y3,x4,y4);

outtextxy(x3+2,y3+2,"Line after rotation");

getch();

}

Page 33: LAB MANUAL...LAB MANUAL Course : CS1544 Computer Graphics Lab Class : B.Sc Computer Science Semester : 5 Prepared By : Nidhi S COLLEGE OF APPLIED SCIENCE PERISSERY CS 1544: Computer

CS 1544: Computer Graphics Lab

P a g e 33 | 34

Output:

Page 34: LAB MANUAL...LAB MANUAL Course : CS1544 Computer Graphics Lab Class : B.Sc Computer Science Semester : 5 Prepared By : Nidhi S COLLEGE OF APPLIED SCIENCE PERISSERY CS 1544: Computer

CS 1544: Computer Graphics Lab

P a g e 34 | 34

15. MAN MOVEMENT

Aim:

To write a c program to demonstrate movement of a man object.

Program:

#include<stdio.h>

#include<graphics.h>

#include<conio.h>

#include<dos.h>

void main()

{

int gd=DETECT,gm,i;

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

for(i=50;i<=getmaxx();i++)

{

circle(i,50,30);

line(i,80,i,200);

line(i,110,i-30,140);

line(i,110,i+30,140);

line(i,200,i-30,230);

line(i,200,i+30,230);

cleardevice();

delay(1000);

}

getch();

}

Output: