Babu Banarasi Das University,Lucknow
School of Engineering
Department of Computer Science & Engineering
Computer Graphics
Lab Mannual
BCS3555
INDEX
Sr. Name of Program
No.
1. Introduction to Computer Graphics
2. C Program to draw a Line
3. C Program to draw a Circle
4. C Program to draw a Rectangle
5. C Program to draw a Square
6. C Program to draw an Ellipse
7. C Program to draw an Arc
8. C Program to implement DDA Line
Drawing Algorithm
9. C Program to implement Bresenham’s
Line Drawing Algorithm
10. C Program to implement DDA Circle
Drawing Algorithm
11. C Program to implement Bresenham’s
Circle Drawing Algorithm
12. C Program to implement Mid-point
Circle Drawing Algorithm
13. C Program to implement Point Clipping
14. C Program to Translate any Object
15. C Program to implement Liang Barskey
Clipping Algorithm
16. C Program to implement Line Clipping using Cohen Sutherland Algorithm
17. Write a C-program for generating a curve for a given set of control
points?
18. Viva Questions
19. Graphics.h Functions
Introduction to Computer Graphics
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.
3. **Drawing Basic Shapes:**
- **Functions:** `line()`, `rectangle()`, `circle()`, `ellipse()`, `arc()`, etc.
- **Description:** These functions are used to draw basic shapes on the screen.
4. **Drawing Text:**
- **Function:** `outtext()`, `outtextxy()`
- **Description:** Displays text on the screen at specified coordinates.
5. **Setting Color:**
- **Function:** `setcolor()`
- **Description:** Sets the drawing color for subsequent graphics operations.
6. **Setting Line Style and Thickness:**
- **Functions:** `setlinestyle()`, `setlinewidth()`
- **Description:** Specifies the style and thickness of lines to be drawn.
7. **Handling User Input:**
- **Functions:** `getch()`, `getche()`, `kbhit()`, etc.
- **Description:** These functions allow the program to interact with the user through
keyboard input.
8. **Mouse Input:**
- **Functions:** `getmouseclick()`, `getmousex()`, `getmousey()`, etc.
- **Description:** Capture and process mouse input for interactive graphics programs.
9. **Coordinate Transformation:**
- **Functions:** `setviewport()`, `setwindow()`, `setaspectratio()`, etc.
- **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.
11. **3D Graphics:**
- **Functions:** `drawpoly3d()`, `drawpoly()`, `drawpoly3d()` etc.
- **Description:** Functions for rendering and manipulating 3D graphics.
12. **Clipping Algorithms:**
- **Functions:** `cohenSutherlandClip()`, `liangBarskyClip()`, etc.
- **Description:** Algorithms for clipping lines or polygons against a specified window.
13. **Image Processing:**
- **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.
Hardware and Software Requirements
For implementing graphics concepts in C-Language:
1. Software Requirement:
Turbo C / C++ compiler that supports graphics.h package.
special DOSBOXed installer for Turbo C++ compiler
2. Minimum hardware requirements:
Intel Pentium III800 MHz Processor or higher version
Intel chipset 810 mother board or higher versi
14’’ color monitor or greater than that
Mouse
Keyboard
2GB HDD or greater
256 MB RAM or greater
For doing key frame animation:
Software Requirements: Adobe Flash Professional version 6 .
Download Adobe Flash CS6 which contains Flash Professional Also and install
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
Basic Structure of a C-graphics program:
1-C program to draw a line
#include <stdio.h>
#include <graphics.h>
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, ""); // Initialize graphics mode
// Coordinates of the line
int x1 = 100, y1 = 100, x2 = 300, y2 = 200;
// Draw a line using the line() function
line(x1, y1, x2, y2);
getch(); // Wait for a key press
closegraph(); // Close the graphics window
return 0;
}
2-C program to draw a circle.
#include <stdio.h>
#include <graphics.h>
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, ""); // Initialize graphics mode
// Coordinates of the circle's center
int xc = 200, yc = 200;
int radius = 50;
// Draw a circle using the circle() function
circle(xc, yc, radius);
getch(); // Wait for a key press
closegraph(); // Close the graphics window
return 0;
3-C program to draw a Rectangle
#include <stdio.h>
#include <graphics.h>
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, ""); // Initialize graphics mode
// Coordinates of the top-left and bottom-right corners of the rectangle
int x1 = 100, y1 = 100, x2 = 300, y2 = 200;
// Draw a rectangle using the rectangle() function
rectangle(x1, y1, x2, y2);
getch(); // Wait for a key press
closegraph(); // Close the graphics window
return 0;
4- C Program to draw a square.
#include <stdio.h>
#include <graphics.h>
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, ""); // Initialize graphics mode
// Coordinates of the top-left corner of the square
int x1 = 100, y1 = 100;
// Side length of the square
int sideLength = 50;
// Calculate the coordinates of the other three corners
int x2 = x1 + sideLength;
int y2 = y1;
int x3 = x1;
int y3 = y1 + sideLength;
int x4 = x1 + sideLength;
int y4 = y1 + sideLength;
// Draw a square using the rectangle() function
rectangle(x1, y1, x4, y4);
getch(); // Wait for a key press
closegraph(); // Close the graphics window
return 0;
5- C program to draw an Ellipse
#include <stdio.h>
#include <graphics.h>
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, ""); // Initialize graphics mode
// Coordinates of the center of the ellipse
int xc = 200, yc = 200;
// Semi-major and semi-minor axes lengths
int a = 80, b = 50;
// Draw an ellipse using the ellipse() function
ellipse(xc, yc, 0, 360, a, b);
getch(); // Wait for a key press
closegraph(); // Close the graphics window
return 0;
6- C program to draw an arc
#include <stdio.h>
#include <graphics.h>
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, ""); // Initialize graphics mode
// Coordinates of the center of the arc
int xc = 200, yc = 200;
// Radius of the arc
int radius = 80;
// Start and end angles of the arc
int start_angle = 45;
int end_angle = 135;
// Draw an arc using the arc() function
arc(xc, yc, start_angle, end_angle, radius);
getch(); // Wait for a key press
closegraph(); // Close the graphics window
return 0;
}
1- Write a program in c to implement DDA algorithm.
#include <stdio.h>
#include <graphics.h>
void drawLineDDA(int x1, int y1, int x2, int y2) {
int dx = x2 - x1;
int dy = y2 - y1;
// Calculate steps required for generating pixels
int steps = abs(dx) > abs(dy) ? abs(dx) : abs(dy);
// Calculate increments for x and y
float xIncrement = (float)dx / (float)steps;
float yIncrement = (float)dy / (float)steps;
// Initialize starting point
float x = x1;
float y = y1;
// Loop to draw each pixel
for (int i = 0; i <= steps; i++) {
putpixel(round(x), round(y), WHITE); // Assuming you have a function like putpixel to draw
a pixel
// Increment in x and y for the next pixel
x += xIncrement;
y += yIncrement;
}
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, ""); // Initialize graphics mode
// Coordinates of the line (change as needed)
int x1 = 100, y1 = 100, x2 = 300, y2 = 200;
drawLineDDA(x1, y1, x2, y2);
getch(); // Wait for a key press
closegraph(); // Close the graphics window
return 0;
2-Write a Program in Bresenham’s line drawing.
#include <stdio.h>
#include <graphics.h>
void drawLineBresenham(int x1, int y1, int x2, int y2) {
int dx = abs(x2 - x1);
int dy = abs(y2 - y1);
int twoDy = 2 * dy;
int twoDx = 2 * dx;
int twodyminusdx = 2 * (dy - dx);
int twodxminusdy = 2 * (dx - dy);
int x, y, p, end;
// Determine which endpoint to use as a starting point
int xStart, yStart, xEnd, yEnd;
if (x1 > x2) {
xStart = x2;
yStart = y2;
xEnd = x1;
yEnd = y1;
} else {
xStart = x1;
yStart = y1;
xEnd = x2;
yEnd = y2;
// Determine the slope direction
if (dy > dx) {
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;
while (x <= end) {
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() {
int gd = DETECT, gm;
initgraph(&gd, &gm, ""); // Initialize graphics mode
// Coordinates of the line (change as needed)
int x1 = 100, y1 = 100, x2 = 300, y2 = 200;
drawLineBresenham(x1, y1, x2, y2);
getch(); // Wait for a key press
closegraph(); // Close the graphics window
return 0;
3-Write a program in C to implement DDA circle
#include <stdio.h>
#include <graphics.h>
void drawCircleDDA(int xc, int yc, int radius) {
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() {
int gd = DETECT, gm;
initgraph(&gd, &gm, ""); // Initialize graphics mode
// Circle parameters (change as needed)
int xc = 200, yc = 200, radius = 50;
drawCircleDDA(xc, yc, radius);
getch(); // Wait for a key press
closegraph(); // Close the graphics window
return 0;
4- C Program to implement Bresenhem’s Circle Drawing Algorithm #include <stdio.h>
#include <graphics.h>
void drawCircleBresenham(int xc, int yc, int radius) {
int x = 0, y = radius;
int p = 3 - 2 * radius;
// Initial points on the two axes
putpixel(xc + x, yc - y, WHITE);
while (y >= x) {
x++;
// Mid-point is inside or on the perimeter
if (p > 0) {
y--;
p = p + 4 * (x - y) + 10;
} else
p = p + 4 * x + 6;
// All the perimeter points have already been printed
if (x > y)
break;
// 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
// Circle parameters (change as needed)
int xc = 200, yc = 200, radius = 50;
drawCircleBresenham(xc, yc, radius);
getch(); // Wait for a key press
closegraph(); // Close the graphics window
return 0;
}
5- C Program to implement Mid-point Circle Drawing Algorithm #include <stdio.h>
#include <graphics.h>
void drawCircleMidpoint(int xc, int yc, int radius) {
int x = radius;
int y = 0;
int p = 1 - radius;
// Initial point on the mid-point
putpixel(xc + x, yc - y, WHITE);
// 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++;
// Mid-point is inside or on the perimeter
if (p <= 0)
p = p + 2 * y + 1;
else {
x--;
p = p + 2 * y - 2 * x + 1;
}
// All the perimeter points have already been printed
if (x < y)
break;
// 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
// Circle parameters (change as needed)
int xc = 200, yc = 200, radius = 50;
drawCircleMidpoint(xc, yc, radius);
getch(); // Wait for a key press
closegraph(); // Close the graphics window
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
// Clipping region parameters (change as needed)
int xMin = 100, yMin = 100, xMax = 300, yMax = 200;
// Test points (change as needed)
int testX1 = 150, testY1 = 150;
int testX2 = 250, testY2 = 250;
// Clip and draw the points
clipPoint(testX1, testY1, xMin, yMin, xMax, yMax);
clipPoint(testX2, testY2, xMin, yMin, xMax, yMax);
getch(); // Wait for a key press
closegraph(); // Close the graphics window
return 0;
}
7- C Program to Translate any Object
#include <stdio.h>
#include <graphics.h>
// 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
// Original point coordinates
int x = 100, y = 100;
// Translation vector
int dx = 50, dy = 30;
// Draw the original point
putpixel(x, y, WHITE); // Assuming you have a function like putpixel to draw a pixel
// Translate the point
translatePoint(&x, &y, dx, dy);
// Draw the translated point
putpixel(x, y, WHITE);
getch(); // Wait for a key press
closegraph(); // Close the graphics window
return 0;
}
8- C Program to implement Liang Barskey Clipping Algorithm #include <stdio.h>
#include <graphics.h>
#define true 1
#define false 0
int xmin, ymin, xmax, ymax; // Clipping window coordinates
// 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;
p[0] = -(x2 - x1);
p[1] = x2 - x1;
p[2] = -(y2 - y1);
p[3] = y2 - y1;
q[0] = x1 - xmin;
q[1] = xmax - x1;
q[2] = y1 - ymin;
q[3] = ymax - y1;
for (int i = 0; i < 4; i++) {
if (p[i] == 0) {
if (q[i] < 0)
return false;
} else {
float r = (float)q[i] / p[i];
if (p[i] < 0) {
if (r > t2)
return false;
else if (r > t1)
t1 = r;
} else if (p[i] > 0) {
if (r < t1)
return false;
else if (r < t2)
t2 = r;
}
}
}
int x1_clip = x1 + t1 * (x2 - x1);
int y1_clip = y1 + t1 * (y2 - y1);
int x2_clip = x1 + t2 * (x2 - x1);
int y2_clip = y1 + t2 * (y2 - y1);
// Draw the clipped line
line(x1_clip, y1_clip, x2_clip, y2_clip);
return true;
}
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, ""); // Initialize graphics mode
// Clipping window coordinates (change as needed) xmin = 50;
ymin = 50;
xmax = 300;
ymax = 200;
// Line coordinates (change as needed)
int x1 = 30, y1 = 80, x2 = 200, y2 = 120;
// Draw the clipping window
rectangle(xmin, ymin, xmax, ymax);
// Draw the original line
line(x1, y1, x2, y2);
// Clip and draw the line
if (clipLine(x1, y1, x2, y2)) {
printf("Line successfully clipped!\n");
} else {
printf("Line lies completely outside the clipping window.\n"); }
getch(); // Wait for a key press
closegraph(); // Close the graphics window
return 0;
}
9- C Program to implement Line Clipping using Cohen Sutherland Algorithm
#include <stdio.h>
#include <graphics.h>
// Define region codes
#define INSIDE 0
#define LEFT 1
#define RIGHT 2
#define BOTTOM 4
#define TOP 8
// Define window coordinates
int xmin, ymin, xmax, ymax;
// 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;
}
// Function to clip a line using Cohen-Sutherland Algorithm void cohenSutherlandClip(int x1,
int y1, int x2, int y2) { int code1 = computeCode(x1, y1);
int code2 = computeCode(x2, y2);
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;
// Choose an endpoint outside the window
if (code1 != 0)
code = code1;
else
code = code2;
// Find intersection point
if (code & TOP) {
x = x1 + (x2 - x1) * (ymax - y1) / (y2 - y1);
y = ymax;
} else if (code & BOTTOM) {
x = x1 + (x2 - x1) * (ymin - y1) / (y2 - y1);
y = ymin;
} else if (code & RIGHT) {
y = y1 + (y2 - y1) * (xmax - x1) / (x2 - x1);
x = xmax;
} else if (code & LEFT) {
y = y1 + (y2 - y1) * (xmin - x1) / (x2 - x1);
x = xmin;
}
// 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
// Clipping window coordinates (change as needed) xmin = 50;
ymin = 50;
xmax = 300;
ymax = 200;
// Line coordinates (change as needed)
int x1 = 30, y1 = 80, x2 = 200, y2 = 120;
// Draw the clipping window
rectangle(xmin, ymin, xmax, ymax);
// Draw the original line
line(x1, y1, x2, y2);
// Clip and draw the line
cohenSutherlandClip(x1, y1, x2, y2);
getch(); // Wait for a key press
closegraph(); // Close the graphics window
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
// Number of control points
int n = 3;
// Control points
int x[] = {100, 200, 300};
int y[] = {100, 400, 100};
// Draw control polygon
setcolor(WHITE);
for (int i = 0; i < n - 1; i++)
line(x[i], y[i], x[i + 1], y[i + 1]);
// Draw control points
setcolor(YELLOW);
for (int i = 0; i < n; i++)
circle(x[i], y[i], 3);
// Draw Bézier curve
setcolor(GREEN);
float t;
int px, py;
for (t = 0; t <= 1; t += 0.001) {
bezier(n - 1, x, y, t, &px, &py);
putpixel(px, py, GREEN);
}
getch(); // Wait for a key press
closegraph(); // Close the graphics window
return 0; }
Viva Questions
1. What is Computer Graphics?
- Answer: Computer Graphics is a field of study that involves the creation, manipulation, and
representation of visual images and animations using computers.
2. Name some applications of Computer Graphics.
- Answer: Applications include computer-aided design (CAD), video games, virtual reality,
image processing, simulation, animation, and multimedia.
3. What is the importance of the Graphics Pipeline in computer graphics?
- 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.
4. Explain the difference between Raster Graphics and Vector Graphics.
- 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.
5. What is Anti-aliasing in Computer Graphics?
- Answer:Anti-aliasing is a technique to reduce visual artifacts (jagged edges) in images. It
works by smoothing or blending the colors along the edges.
6. Define Ray Tracing.
- 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.
8. Explain the Bresenham's Line Drawing Algorithm.
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.
9. What is the purpose of Clipping in Computer Graphics?
- 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.
10. Describe the concept of Shading in Computer Graphics.
Answer: Shading is the process of determining the colors of pixels in a rendered
image based on factors like lighting, material properties, and the angle of the surface with
respect to the viewer.
Graphics.h Functions
`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:
- Description: Initializes the graphics system.
- Syntax:`void initgraph(int *graphicsDriver, int *graphicsMode, char *driverDirectory);`
2. closegraph:
- Description:Closes the graphics system.
- Syntax: `void closegraph();`
3. getch:
- Description:Waits for a key press and returns the ASCII value of the key.
- Syntax:`int getch();`
4.circle:
- Description: Draws a circle with a specified center and radius.
- Syntax: `void circle(int x, int y, int radius);`
5. line:
- Description:Draws a line between two specified points.
- Syntax:`void line(int x1, int y1, int x2, int y2);`
6. rectangle:
- Description:Draws a rectangle with specified coordinates.
- Syntax: `void rectangle(int left, int top, int right, int bottom);`
7. ellipse:
- Description: Draws an ellipse with a specified center and radii.
- Syntax:`void ellipse(int x, int y, int startAngle, int endAngle, int xRadius, int yRadius);`
8. arc:
- Description: Draws an arc or a portion of a circle.
- Syntax: `void arc(int x, int y, int startAngle, int endAngle, int radius);`
9. putpixel:
- Description: Sets the color of a pixel at the specified coordinates.
- Syntax:void putpixel(int x, int y, int color);`
10. setcolor:
- Description:Sets the drawing color for subsequent graphics operations.
- Syntax: `void setcolor(int color);`
11. setbkcolor:
- Description:Sets the background color.
- Syntax:`void setbkcolor(int color);`
12. setlinestyle:
- Description: Sets the line style for drawing.
- Syntax:`void setlinestyle(int linestyle, unsigned upattern, int thickness);`
13. getimage and putimage:
- Description: Captures and restores an image of a specified area.
- Syntax: `void getimage(int left, int top, int right, int bottom, void *bitmap);`
`void putimage(int left, int top, void *bitmap, int op);`
14. delay:
- Description: Delays the program execution.
- Syntax:`void delay(int milliseconds);`
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.