Computer Graphics Lab
Computer Graphics Lab
Computer Graphics Lab
School of Engineering
Computer Graphics
Lab Mannual
BCS3555
INDEX
Introduction to Computer graphics involves creating and manipulating visual images using
computers. In a computer graphics program, various functions are employed to perform tasks
such as drawing shapes, rendering 3D scenes, handling user input, and managing the display.
Below is an introduction to some common functions in computer graphics programming:
1. **Initiating Graphics:**
- **Function:** `initgraph()`
- **Description:** Initializes the graphics system and sets up the graphics mode for drawing.
2. **Closing Graphics:**
- **Function:** `closegraph()`
- **Description:** Closes the graphics system, releasing resources and ending the graphics
session.
- **Description:** These functions are used to draw basic shapes on the screen.
4. **Drawing Text:**
5. **Setting Color:**
- **Function:** `setcolor()`
- **Description:** These functions allow the program to interact with the user through
keyboard input.
8. **Mouse Input:**
- **Description:** Capture and process mouse input for interactive graphics programs.
9. **Coordinate Transformation:**
- **Description:** Manipulate the coordinate system to control the mapping between logical
and physical coordinates.
10. **Animation:**
- **Functions:** `delay()`, `clearviewport()`, `putpixel()`, etc.
- **Description:** Used to create animation effects by changing the display rapidly.
- **Functions:** Varies based on the library or API used (e.g., OpenCV for C++)
- **Description:** Functions for processing and manipulating images, such as filtering, edge
detection, etc.
These functions are often part of a graphics library or API, such as the graphics.h library in
C/C++ or more modern libraries like OpenGL, DirectX, or Vulkan for advanced graphics
programming. The specific functions and their usage may vary depending on the chosen
graphics framework.
1. Software Requirement:
Turbo C / C++ compiler that supports graphics.h package.
special DOSBOXed installer for Turbo C++ compiler
Hardware Requirements:
Intel® Pentium® 4, Intel Centrino®, Intel Xeon®, or Intel Core™ Duo (or compatible)
processor
Microsoft® Windows® 7 (64 bit) or Windows 8 (64 bit)
4GB of RAM
2.5GB of available hard-disk space for installation; additional free space required during
installation (cannot install on removable flash storage devices) 1024x768 display (1280x800
recommended)
QuickTime 10.x software recommended
#include <stdio.h>
#include <graphics.h>
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, ""); // Initialize graphics mode
return 0;
}
2-C program to draw a circle.
#include <stdio.h>
#include <graphics.h>
int main() {
return 0;
#include <stdio.h>
#include <graphics.h>
int main() {
return 0;
#include <stdio.h>
#include <graphics.h>
int main() {
int gd = DETECT, gm;
int x2 = x1 + sideLength;
int y2 = y1;
int x3 = x1;
int y3 = y1 + sideLength;
int x4 = x1 + sideLength;
int y4 = y1 + sideLength;
return 0;
#include <stdio.h>
#include <graphics.h>
int main() {
return 0;
#include <stdio.h>
#include <graphics.h>
int main() {
return 0;
}
1- Write a program in c to implement DDA algorithm.
#include <stdio.h>
#include <graphics.h>
int dx = x2 - x1;
int dy = y2 - y1;
float x = x1;
float y = y1;
putpixel(round(x), round(y), WHITE); // Assuming you have a function like putpixel to draw
a pixel
y += yIncrement;
}
int main() {
return 0;
#include <stdio.h>
#include <graphics.h>
int x, y, p, end;
xStart = x2;
yStart = y2;
xEnd = x1;
yEnd = y1;
} else {
xStart = x1;
yStart = y1;
xEnd = x2;
yEnd = y2;
x = xStart;
y = yStart;
p = twoDx - dy;
end = yEnd;
while (y <= end) {
putpixel(x, y, WHITE); // Assuming you have a function like putpixel to draw a pixel
y++;
if (p < 0) {
p += twoDx;
} else {
x++;
p += twodyminusdx;
} else {
x = xStart;
y = yStart;
p = twoDy - dx;
end = xEnd;
putpixel(x, y, WHITE); // Assuming you have a function like putpixel to draw a pixel
x++;
if (p < 0) {
p += twoDy;
} else {
y++;
p += twodxminusdy;
}
}
int main() {
return 0;
#include <stdio.h>
#include <graphics.h>
int x = radius;
int y = 0;
int decision = 1 - radius;
while (x >= y) {
putpixel(xc + x, yc - y, WHITE);
putpixel(xc + y, yc - x, WHITE);
putpixel(xc - y, yc - x, WHITE);
putpixel(xc - x, yc - y, WHITE);
putpixel(xc - x, yc + y, WHITE);
putpixel(xc - y, yc + x, WHITE);
putpixel(xc + y, yc + x, WHITE);
putpixel(xc + x, yc + y, WHITE);
y++;
if (decision <= 0) {
decision += 2 * y + 1;
} else {
x--;
decision += 2 * (y - x) + 1;
int main() {
return 0;
while (y >= x) {
x++;
// Printing the generated point and its reflection in the other octants putpixel(xc + x, yc
- y, WHITE);
putpixel(xc - x, yc - y, WHITE);
putpixel(xc + x, yc + y, WHITE);
putpixel(xc - x, yc + y, WHITE);
// If the generated point is on the line y = x, then the perimeter points have already been
printed
if (x != y) {
putpixel(xc + y, yc - x, WHITE);
putpixel(xc - y, yc - x, WHITE);
putpixel(xc + y, yc + x, WHITE);
putpixel(xc - y, yc + x, WHITE);
}
}
}
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, ""); // Initialize graphics mode
// When radius is zero only a single point will be printed at the center if (radius > 0) {
putpixel(xc - x, yc - y, WHITE);
putpixel(xc + y, yc + x, WHITE);
putpixel(xc - y, yc + x, WHITE);
}
while (x > y) {
y++;
// Printing the generated point and its reflection in the other octants putpixel(xc + x, yc
- y, WHITE);
putpixel(xc - x, yc - y, WHITE);
putpixel(xc + y, yc + x, WHITE);
putpixel(xc - y, yc + x, WHITE);
// If the generated point is on the line y = x, then the perimeter points have already been
printed
if (x != y) {
putpixel(xc + y, yc - x, WHITE);
putpixel(xc - y, yc - x, WHITE);
putpixel(xc + x, yc + y, WHITE);
putpixel(xc - x, yc + y, WHITE);
}
}
}
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, ""); // Initialize graphics mode
return 0;
}
6- C Program to implement Point Clipping
#include <stdio.h>
#include <graphics.h>
void clipPoint(int x, int y, int xMin, int yMin, int xMax, int yMax) {
if (x >= xMin && x <= xMax && y >= yMin && y <= yMax) {
printf("Point (%d, %d) is inside the clipping region.\n", x, y);
putpixel(x, y, WHITE); // Assuming you have a function like putpixel to draw a pixel
} else {
printf("Point (%d, %d) is outside the clipping region.\n", x, y);
}
}
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, ""); // Initialize graphics mode
// Function to translate a point by (dx, dy) void translatePoint(int *x, int *y, int dx, int dy) {
*x = *x + dx;
*y = *y + dy;
}
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, ""); // Initialize graphics mode
// Translation vector
int dx = 50, dy = 30;
#define true 1
#define false 0
// Function to perform Liang-Barsky Clipping int clipLine(int x1, int y1, int x2, int y2) {
int p[4], q[4];
float t1 = 0, t2 = 1;
q[0] = x1 - xmin;
q[1] = xmax - x1;
q[2] = y1 - ymin;
q[3] = ymax - y1;
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, ""); // Initialize graphics mode
return 0;
}
9- C Program to implement Line Clipping using Cohen Sutherland Algorithm
#include <stdio.h>
#include <graphics.h>
// Function to compute region code for a point (x, y) int computeCode(int x, int y) {
int code = INSIDE;
if (x < xmin)
code |= LEFT;
else if (x > xmax)
code |= RIGHT;
if (y < ymin)
code |= BOTTOM;
else if (y > ymax)
code |= TOP;
return code;
}
int accept = 0;
while (1) {
if (!(code1 | code2)) {
// Both endpoints inside the window, trivially accept the line accept = 1;
break;
} else if (code1 & code2) {
// Both endpoints outside the window, trivially reject the line break;
} else {
// Line needs clipping
int code;
int x, y;
// Replace the outside endpoint with the intersection point if (code == code1) {
x1 = x;
y1 = y;
code1 = computeCode(x1, y1);
} else {
x2 = x;
y2 = y;
code2 = computeCode(x2, y2);
}
}
}
if (accept) {
line(x1, y1, x2, y2);
printf("Line successfully clipped!\n");
} else {
printf("Line lies completely outside the clipping window.\n"); }
}
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, ""); // Initialize graphics mode
return 0;
}
10-Write a C-program for generating a curve for a given set of control points?
#include <graphics.h>
#include <stdio.h>
// Function to calculate the blending value for a point on the Bézier curve float blend(int n, int
k, float t) {
int i;
float res = 1.0;
for (i = 1; i <= k - 1; i++)
res *= (n - i + 1) / (float)i;
return res * pow(t, k) * pow(1 - t, n - k);
}
// Function to calculate the point on the Bézier curve using de Casteljau's algorithm void
bezier(int n, int *x, int *y, float t, int *px, int *py) {
int i, k;
float b;
*px = 0;
*py = 0;
for (k = 0; k <= n; k++) {
b = blend(n, k, t);
*px += b * x[k];
*py += b * y[k];
}
}
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, ""); // Initialize graphics mode
// Control points
int x[] = {100, 200, 300};
int y[] = {100, 400, 100};
- Answer: Computer Graphics is a field of study that involves the creation, manipulation, and
representation of visual images and animations using computers.
- Answer: Applications include computer-aided design (CAD), video games, virtual reality,
image processing, simulation, animation, and multimedia.
- Answer: The Graphics Pipeline is a sequence of stages that graphics data goes through
during rendering. It includes stages such as modeling, transformation, clipping, rasterization,
and display. It is crucial for transforming 3D models into 2D images for display.
- Answer: Raster Graphics uses a grid of pixels to represent images and is resolution-
dependent. Vector Graphics use mathematical equations to represent shapes, allowing for
scalability without loss of quality.
- Answer: Ray Tracing is a rendering technique that simulates the way light interacts with
objects to generate highly realistic images. It traces the path of rays of light as they travel
through a scene.
7. What are the primary color models used in Computer Graphics?
- Answer:The primary color models are RGB (Red, Green, Blue) for additive color mixing, and
CMY(K) (Cyan, Magenta, Yellow, Key/Black) for subtractive color mixing in printing.
Answer: Bresenham's Line Drawing Algorithm is an efficient method for drawing a line
between two points. It uses integer arithmetic and avoids floating-point calculations to
determine the pixels to be illuminated.
- Answer: Clipping is the process of removing parts of a graphical object that are outside the
viewing area or window. It ensures that only the visible portion of an object is displayed.
`graphics.h` is a header file in the C programming language that provides functions for simple
graphics operations. Note that `graphics.h` is mostly associated with the Turbo C compiler and
the Borland Graphics Interface (BGI). Here are some commonly used functions provided by
`graphics.h`:
1. initgraph:
2. closegraph:
3. getch:
- Description:Waits for a key press and returns the ASCII value of the key.
- Syntax:`int getch();`
4.circle:
5. line:
- Syntax: `void rectangle(int left, int top, int right, int bottom);`
7. ellipse:
- Syntax:`void ellipse(int x, int y, int startAngle, int endAngle, int xRadius, int yRadius);`
8. arc:
- Syntax: `void arc(int x, int y, int startAngle, int endAngle, int radius);`
9. putpixel:
10. setcolor:
11. setbkcolor:
- Syntax: `void getimage(int left, int top, int right, int bottom, void *bitmap);`
14. delay:
These functions are part of the Turbo C graphics library. If you are using a different compiler or
environment, these functions might not be available. Modern graphics programming is typically
done using more advanced libraries such as OpenGL, DirectX, or others, depending on the
platform and requirements.