Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Computer Graphics Lab, Lab Reports of Computer Graphics

Computer Graphics lab LABORATORY MANUAL CONTENTS This manual is intended for the students of Computer Science and Engineering in the subject of Computer Graphics. This manual typically contains practical/Lab Sessions related Programming. In C covering various aspects related the subject to enhanced understanding.

Typology: Lab Reports

2019/2020

Uploaded on 12/19/2020

anoop-rana-1
anoop-rana-1 🇮🇳

4.5

(2)

6 documents

1 / 38

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
UNITED COLLEGE OF EDUCATION,
GREATER NOIDA
DEPARTMENT OF COMPUTER APPLICATION
LAB MANUAL
SUBJECT NAME: PRACTICAL IX-CG LAB
PAPER CODE: BCA 351
BCA THIRD YEAR, SEMESTER-V
ODD SEMESTER, SESSION 2020-21
SUBJECT FACULTY
DR. SHILPI SINGH
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26

Partial preview of the text

Download Computer Graphics Lab and more Lab Reports Computer Graphics in PDF only on Docsity!

UNITED COLLEGE OF EDUCATION,

GREATER NOIDA

DEPARTMENT OF COMPUTER APPLICATION

LAB MANUAL

SUBJECT NAME: PRACTICAL IX-CG LAB

PAPER CODE: BCA 351

BCA THIRD YEAR, SEMESTER-V

ODD SEMESTER, SESSION 20 20 - 21

SUBJECT FACULTY

DR. SHILPI SINGH

LIST OF EXPERIMENTS/EXCERCISES PRACTICAL IX-COMPUTER GRAPHICS LAB BCA- 351

S. No. Experiment/Exercise
PART - 1 Basics/Inbuilt functions in computer graphics

1 To draw a line using inbuilt functions. 2 To draw a circle/Ellipse using inbuilt functions. 3 To draw a rectangle using inbuilt functions. 4 To change the background and foreground color using inbuilt functions. 5 To fill area using inbuilt functions. 6 To draw a triangle using inbuilt functions and a circle is centered at each corner of the triangle.

PART- 2 Line/circle/ellipse drawing algorithms

7 To draw a line using DDA Line drawing algorithm. 8 To draw a line using Bresenhem Line drawing algorithm. 9 To draw a circle using eight way symmetry. 10 To draw a circle using incremental circle drawing algorithm. 11 To draw a circle using mid point/Bresenhem circle drawing algorithm. 12 To draw an Ellipse using midpoint ellipse drawing algorithm.

PART- 3 Polygon Filling/Area Filling/Region filling

13 Polygon filling/area filling using boundary fill algorithm. 14 Polygon filling/area filling using flood fill algorithm. 15 Polygon filling/area filling using Scan line algorithm.

PART- 4 2D Clipping/2D Transformation /Window to View port
mapping

16 To clip a line using Midpoint Line clipping algorithm. 17 To clip a line using Cohen Sutherland /Four bit op-code line clipping algorithm. 18 To clip a line using Cyrus Back line clipping algorithm. 19 To implement a clock. 20 To implement a screen saver. 21 To transform a 2D object (Line, Rectangle, circle) using 1 Translation 2 Scaling 3 Rotation 4 Reflection 5 Shearing 22 To transform a 2D object (Line, Rectangle, circle) using Homogenous Coordinate system. 1 Translation 2 Scaling 3 Rotation 4 Reflection 5 Searing 23 To transform window to view port. 24 To zoom an object. 25 To perform reverse zooming.

Computer Graphics

Lab Record

INTRODUCTION:

Computer graphics basically includes approximately everything on computers system

which is not text or sound. Today almost every computer can do some graphics, and

people have even come to expect to control their computer through icons and pictures

rather than just by typing. Laboratory Experiments will be conducted using the currently

available computer graphics API such as OpenGL, C & C++. The students needs to

show to basic frame-buffer control, pixel processes, rasterization, 2D drawings, 3D

transformations, projections, scene hierarchy, modeling objects, color and interactive

animation.

OBJECTIVES:

The objective of this lab is to understand the following:

1) learn basic and fundamental computer graphics techniques;

2) 2D drawing ( Line, Circle, Ellipse & Curve Drawing)

3) Coloring / Filling the objects

4) 2D & 3D Transformation

5) Clipping Algorithms like ( Line, Polygon & Text)

6) How to create a realistic object ( Hidden surface removal)

7) Shading & Morphing Techniques

TARGET AUDIENCE:
The experiments are targeted at undergraduate students who have just finished a first course in
programming. Knowledge of writing programs in any programming language is expected.
SYSTEM REQUIREMENTS:
Hardware Requirement :
233 MHz processor or faster min. 64 MB of RAM min 1.5 GB space on your HDD
Software Requirement:
Microsoft Windows 9x or higher
Turbo C/Turbo C++/ Open GL

PART- 1

Basics/Inbuilt functions in computer graphics

Introduction & Theory

GRAPHICS.H

GRAPHICS.H is a header file in C that is used for drawing various shapes and other graphics. It is also used to color the objects drawn. We can also find out the coordinates of points on the screen using it. In order to be able to use graphics.h we need to have the drivers installed and recognized by the program. Also the graphics card inserted in the computer needs to be known.

INITGRAPH

Initializes the graphics system Void initgraph (int *graphdriver,int *graphmode, char *pathtodriver); To start the graphics system, you must first call initgraph. 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, and then resets graph result to 0.

LINE

Line is an inbuilt function present in graphics.h that draws a line in the current drawing color between two given coordinates. void line(int x1,int y1,int x2,int y2); Line draws a line from point (x1,y1) to point (x2,y2) using the current drawing color, line style and thickness. It does not update the current position (CP). In order to draw a line using this function, one just need to call the function line and pass the values of the x and y coordinates of the initial and final points of the line.

CIRCLE

Circle is an inbuilt function present in graphics.h that draws a circle in the current drawing color. void circle(int x, int y, int radius); x and y are the coordinates of the center of the circle and radius is the value of radius of circle. In order to draw a circle using this function, one just needs to call the function circle and pass the values of the center of the circle and its radius.

RECTANGLE

Rectangle is an inbuilt function present in graphics.h that draws a rectangle in the current drawing color, line style and thickness. void rectangle(int left,int top,int right,int bottom);

Putpixel plots a point in the color defined by color at (x, y).

GETCOLOR

getcolor returns the current drawing color. int getcolor(void); The drawing color is the value that pixels are set to when the program draws lines, etc.

SETCOLOR

setcolor sets the current drawing color void setcolor(int color); setcolor sets the current drawing color to color, which can range from 0 to getmaxcolor. To select a drawing color with setcolor, you can pass either the color number or the equivalent color name.

GETMAXX AND GETMAXY

Returns maximum x or y screen coordinate. The declaration is given as: int getmaxx(void); int getmaxy(void); getmaxx returns the maximum x value (screen-relative) for the current graphics driver and mode and getmaxy returns the maximum y value (screen-relative) for the current graphics driver and mode. On a CGA in 320 x 200 mode, getmaxx returns 319 and getmaxy returns 199.

OUTTEXTXY

outtextxy displays a string at the specified location (graphics mode). void outtextxy(int x, int y, char far *textstring); outtextxy displays textstring in the viewport at the position (x, y)

SETVIEWPORT

Sets the current viewport for graphics output. void setviewport(int left, int top, int right, int bottom, int clip); Setviewport establishes a new viewport for graphics output. The viewport's corners are given in absolute screen coordinates by (left, top) and (right, bottom).The current position (CP) is moved to (0, 0) in the new window. The clip argument determines whether drawings are clipped (truncated) at the current viewport boundaries. If clip is non-zero, all drawings will be clipped to the current viewport.

NOTE:

Experiments of this part is from S. No. 1 to S. No. 6 Mandatory assignment for this part is given below Q1.What is pixel. Q2. What do you mean by resolution? Q3.What is an image aspect ratio? Q4. Explain frame buffer? Q5.What is the function of the control electrode in a CRT? Q6. Explain the terms interactive and non interactive computer graphics. Q7. What are the three major adverse side effects of scan conversion?

Void far rectangle(int left,int top,int right,int bottom);
(left,top) is the upper left corner of the rectangle and (right,bottom) is the bottom right corner.
In order to draw a rectangle using this function, one just need to call the function rectangle and pass
the values of the x and y coordinates of the top left and bottom right corners of the rectangle.

PROGRAM:

#include <stdio.h>
#include <graphics.h>
#include <conio.h>
void main()
int x0,x 1 ,y 0 ,y 1 ,rad;
int gd = DETECT, gm;
initgraph(&gd, &gm, "c:\tc\bgi");
printf("To draw a line...\n");
printf("Enter initial and final coordinates of line...\n");
scanf("%d%d%d%d",&x 0 ,&y 0 ,&x 1 ,&y 1 );
line(x0,y0,x1,y1);
printf("To draw a circle...\n");
printf("Enter coordinates of center and radius of circle...\n");
scanf("%d%d%d",&x 0 ,&y 0 ,&rad);
circle(x0,y0,rad);
printf("To draw a rectangle ...\n");
printf("Enter the 4 coordinates to draw the rectangle...\n");
scanf("%d%d%d%d",&x0,&y0,&x1,&y1);
rectangle(x0,y0,x1,y1);
getch();

OUTPUT:

To draw a line...
Enter initial and final coordinates of line...
To draw a circle...
Enter coordinates of center and radius of circle...
To draw a rectangle ...
Enter the 4 coordinates to draw the rectangle...

PART- 2

Line/circle/ellipse drawing algorithms

Scan Conversion: Drawing a Straight Line
General Theory

The general problem of scan conversion is which pixels to turn on. For example let us consider a line with positive slope in the first octant, i.e., 0.0 <= m <= 1.0 and drawn from left to right: (X1, Y1) to (X2, Y2):

The problem is when we increment xi to xi+1, do we turn on xi+1, yi or xi+1, yi+1?

The goal is to minimize the stair-step effect (jaggies), so that we have a uniform line density, and require the minimum drawing time. Remember, we must always round or truncate to integers since pixels are at integer positions.

  • Floating Point Algorithms
  • Digital Differential Analyzer (DDA) method
  • Bresenham (Decision Variable) method
  • Circle drawing using polar co-ordinate system
  • Eight way symmetry
  • Bresenham circle drawing algorithm
Floating Point Algorithms

The equation for a straight line: y = m * x + b, where m is the slope (m = dy/dx) and b is the y intercept. When we increment x by dx we can compute dy = m * dx, but this is a brute force real number computation and is very slow.

Alternative Method - Parametric Equations

For a line segment x1, y1 - > x2, y2: x = x1 + t*dx with dx = x2 - x1 and 0.0 <= t <= 1. So start with t = 0 and increment to t = 1.0, e.g. t= .05, .10,.15, etc. This is still too slow but parametric equations are used for clipping, curve fitting, and ray tracing.

Digital Differential Analyzer (DDA) Method

The basis of the DDA method is to take unit steps along one coordinate and compute the corresponding values along the other coordinate. The unit steps are always along the coordinate of greatest change, e.g. if dx = 10 and dy = 5, then we would take unit steps along x and compute the steps along y. Let us first consider a line of positive slope and draw from x1, y1 to x2, y2. {slope = m = dy/dx} if m <= 1.0 then let x_step = 1 {dx = 1, dy = 0 or 1} else {m > 1.0} let y_step = 1 {dy = 1, x_step = 0 or 1 } For a line with slope < 0.0 and drawing from x1, y1 to x2, y2, i.e., left to right then: if |m| < 1 then let dx = 1 and yi+1 = yi + m else {|m| ³ 1} let dy = - 1 and xi+1 = xi - 1/m if draw from x2, y2 to x1, y1 (right to left) then: if |m| < 1 then let dx = - 1 yi+1 = yi - m else {|m| ³ 1} dy = 1 xi+1 = xi + 1/m

Complete DDA Algorithm

procedure DDA( x1, y1, x2, y2: integer); var dx, dy, steps: integer; x_inc, y_inc, x, y: real; begin dx := x2 - x1; dy := y2 - y1; if abs(dx) > abs(dy) then steps := abs(dx); {steps is larger of dx, dy} else

Now look at the relation of p's for successive x terms. pi+1 = 2dy * xi+1 - 2 * dx * y i+1 + C pi+1 - pi = 2 * dy * (xi+1 - xi) - 2 * dx * ( yi+1 - yi) with xi+1 = xi + 1 and yi+1= yi + 1 or yi (2) pi+1 = pi + 2 * dy - 2 * dx(yi+1 - yi) Note: b = y - dy / dx * x Now compute p1 (x1,y1) from (1) p1 = 2dy * x1 - 2dx * y1 + 2dy + dx(2y1 - 2dy / dx * x1 - 1) = 2dy * x1 - 2dx * y1 + 2dy + 2dx * y1 - 2dyx1 - dx = 2dy - dx if pi < 0 choose yi and pi+1 = pi + 2dy else {pi ³ 0} and choose yi+1 so pi+1 = pi + 2dy - 2dx Bresenham Algorithm for 1st octant : 1 Enter endpoints ( x1, y1 ) and ( x2, y2 ). 2 Display x1, y. 3 Compute dx = x2 - x1 ; dy = y2 - y1 ; p1 = 2dy - dx. 4 If p1 < 0.0 , display x1 + 1, y1, else display x1+1, y1 + 1 5 if p1 < 0.0, p2 = p1 + 2dy else p2 = p1 + 2dy - 2dx

Example. 1

AIM: To draw a line using DDA algorithm

#include <stdio.h>
#include <graphics.h>
#include <conio.h>
#include <math.h>
void DDA();
void main()
int x1,x2,y1,y2,colorval,dx,dy;
int gd = DETECT, gm;
initgraph(&gd, &gm, "c:\tc\bgi");
printf("enter the values of x1 & y1 : \n");
scanf("%d%d",&x1,&y1);
printf("enter the values of x2 & y2 : \n");
scanf("%d%d",&x2,&y2);
printf("enter color value : \n");
scanf("%d",&colorval);
DDA(x1, x2, y1, y2, colorval);
getch();
void DDA(int x1,int x2,int y1,int y2,int colorval)
int dy, dx;
float m;
int i,j;
dy = y2 - y1;
dx = x2 - x1;
m = dy/dx;
j = y1;
cleardevice();
for (i=x1; i<=x2; i++)
{putpixel(i,floor(j),colorval);
j=j+m;}

EXAMPLE. 2

AIM: To draw a line using Bresenhem algorithm

#include <stdio.h>
#include <graphics.h>
#include <conio.h>
#include <math.h>
void bresenhem();
void main()
int x1,x2,y1,y2,x3,y3,colorval,dx,dy;
int gd = DETECT, gm;
initgraph(&gd, &gm, "c:\tc\bgi");
printf("Enter the values of x1 & y1 : \n");
scanf("%d%d",&x1,&y1);
printf("Enter the values of x2 & y2 : \n");
scanf("%d%d",&x2,&y2);
printf("Enter color value : \n");
scanf("%d",&colorval);
bresenhem(x1, x2, y1, y2, colorval);
getch();
void bresenhem(int x1,int x2,int y1,int y2,int colorval)
int dy, dx;
int i,j,D;
float incrE, incrNE;
dy = y2 - y1;
dx = x2 - x1;
D = 2*dy-dx;
incrE = 2*dy;
incrNE = 2dy-2dx;
i = x1;
j = y1;
putpixel(i, j, colorval);
while(i<=x2)
if(D<=0)
D+=incrE;
i++;
else
D+=incrNE;
i++;
j++;
putpixel(i,j,colorval);

OUTPUT:

Enter the values of x1 & y1 :
Enter the values of x2 & y2 :