0% found this document useful (0 votes)
11 views18 pages

CG Draft

This document is a laboratory report submitted by Yash Modi for the Computer Graphics Lab at the National Institute of Technology Srinagar. It includes various experiments using the graphics.h library in C, detailing functions for drawing shapes and implementing algorithms like DDA and Bresenham's line generation. The report contains code examples for drawing a star, hut, concentric circles, moving car, and implementing line drawing algorithms.

Uploaded by

YASH MODI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views18 pages

CG Draft

This document is a laboratory report submitted by Yash Modi for the Computer Graphics Lab at the National Institute of Technology Srinagar. It includes various experiments using the graphics.h library in C, detailing functions for drawing shapes and implementing algorithms like DDA and Bresenham's line generation. The report contains code examples for drawing a star, hut, concentric circles, moving car, and implementing line drawing algorithms.

Uploaded by

YASH MODI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

2022BCSE051

YASH MODI

Department of Computer Science and Engineering


National Institute of Technology Srinagar
Hazratbal, Srinagar, Jammu and Kashmir - 190006, India.

LABORATORY REPORT
Computer Graphics Lab (CSL355)

Submitted by

YASH MODI
2022BCSE051
B.Tech. CSE
th
(6 Semester – Section ‘A’)

Submitted to

Dr. Pramod Kumar Yadav


Assistant Professor
Department of Computer Science and Engineering
National Institute of Technology Srinagar
Hazratbal, Srinagar, Jammu and Kashmir - 190006, India.

SPRING 2025
2022BCSE051
YASH MODI
2022BCSE051
YASH MODI

CONTENTS

Page Faculty
Week # Date Experiment
No. Sign

Self-Declaration

I have done all the above programs on my own to the best of my knowledge.

Student’s Signature
2022BCSE051
YASH MODI
2022BCSE051
YASH MODI

LAB: 01

Graphics.h Library

1. The graphics.h library in C is a classic graphics library used primarily in Turbo


C++ for simple 2D graphics programming.
2. It provides various functions to create graphical elements such as lines,
circles, rectangles, and text, making it a great tool for beginners learning
computer graphics.

Functions in graphics.h library:


▪ line(): Draws a straight line from (x1, y1) to
(x2, y2). Syntax: void line(int x1, int y1, int x2,
int y2);
▪ x1, y1: Starting point coordinates.
▪ x2, y2: Ending point coordinates.

▪ circle() : Draws a circle centred at (x, y) with the specified


radius. Syntax: void circle(int x, int y, int radius);
▪ x, y: centre of the circle.
▪ radius: Radius of the circle.

▪ arc() : Draws an arc (a portion of a circle) from start_angle to


end_angle. Syntax: void arc(int x, int y, int start_angle, int
end_angle, int radius);
▪ x, y: centre of the arc.
▪ start_angle: The angle (in degrees) where the arc
starts, measured counterclockwise from the
positive X-axis..
▪ end_angle: Ending angle (in degrees).
▪ radius: Radius of the arc.

▪ ellipse() : Draws an ellipse with specified horizontal and vertical


radii. Syntax: void ellipse(int x, int y, int start_angle, int end_angle,
2022BCSE051
YASH MODI

int xradius, int yradius );


▪ x, y: Center coordinates.
▪ start_angle, end_angle: Angle range for the arc of the ellipse.
▪ xradius, yradius: Horizontal and vertical radii.

▪ rectangle() : Draws a rectangle using the given coordinates for the


top-left and bottom-right corners.
▪ Syntax: void rectangle(int left, int top, int right, int bottom);
▪ left, top: Top-left corner coordinates.
▪ right, bottom: Bottom-right corner coordinates.

▪ initgraph() : Initializes the graphics mode by detecting the appropriate


graphics driver and setting the display mode.
▪ Syntax: void initgraph(int *graphdriver, int *graphmode, char
▪ *pathtodriver);
▪ graphdriver: A pointer to an integer that specifies the
graphics driver (use DETECT for automatic detection).
▪ graphmode: A pointer to an integer specifying the graphics mode.
▪ pathtodriver: The directory path where BGI files
are stored ("C:\\Turboc3\\BGI" for Turbo C++).

▪ closegraph() : Closes the graphics mode and frees memory


allocated for graphics operations.
Syntax: void closegraph();

▪ bar() : Draws a filled rectangle.


▪ Syntax: void bar(int left, int top, int right, int bottom);
▪ left, top: Top-left corner coordinates.
▪ right, bottom: Bottom-right corner coordinates.

▪ drawpoly() : Draws a polygon with num


vertices. Syntax: void drawpoly(int num, int
*points);
▪ num: Number of vertices.
2022BCSE051
YASH MODI

▪ points: Array of 2*num integers specifying the


(x, y) coordinates of each vertex.

▪ setcolor() : Sets the current drawing


color. Syntax: void setcolor(int color);
▪ color: The color to use (e.g., RED, BLUE).

▪ setbkcolor() : Sets the background color of the


screen. Syntax: void setbkcolor(int color);
▪ color: The background color.

▪ floodfill() : Fills an enclosed area with the current fill


color. Syntax: void floodfill(int x, int y, int
border_color);
▪ x, y: A point inside the region to fill.
▪ border_color: The border color that defines the enclosed region.

▪ getmaxx() : Returns the maximum X-coordinate for the current


graphics mode. Syntax: int getmaxx();

▪ getmaxy() : Returns the maximum Y-coordinate for the current


graphics mode. Syntax: int getmaxy();

▪ pieslice() : Draws and fills a pie slice (a sector of a circle).


▪ Syntax: void pieslice(int x, int y, int start_angle, int
end_angle, int radius);
▪ x, y: Center of the pie slice.
▪ start_angle, end_angle: Start and end angles (in degrees).
▪ radius: Radius of the pie slice.

▪ detectgraph() : Detects the best available graphics driver and


mode. Syntax : Detects the best available graphics driver and
2022BCSE051
YASH MODI

mode.
▪ graphdriver: Pointer to store detected driver.
▪ graphmode: Pointer to store detected mode.

▪ fillellipse() : The fillellipse() function is used to draw and fill an ellipse


with the current fill style and color.
▪ Syntax: void fillellipse(int x, int y, int xradius, int yradius);
▪ x, y → Coordinates of the center of the ellipse.
▪ xradius → Horizontal radius (width).
▪ yradius → Vertical radius (height).

▪ outtextxy() : Displays a string at position


(x, y). Syntax: void outtextxy(int x, int y,
char *text);
▪ x, y: Position to display the text.
▪ text: The string to display.

▪ settextstyle() : Sets the text style for outtextxy().


Syntax : void settextstyle(int font, int direction, int
size);
▪ font: Font type (e.g., DEFAULT_FONT).
▪ direction: Orientation (HORIZ_DIR for horizontal,
VERT_DIR for vertical).
▪ size: Text size.

▪ putpixel() : Draws a single pixel at (x, y) in the specified


color. Syntax : void putpixel(int x, int y, int color);
▪ x, y: Coordinates of the pixel.
▪ color: The color to set for the pixel.
2022BCSE051
YASH MODI

LAB 2

Q1- Write programs in C++ to draw the following shapes?

1.STAR

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

int main(void) {
int gdriver = DETECT, gmode, errorcode;
int xmax, ymax;

initgraph(&gdriver, &gmode, "c://turboc3/bgi");

errorcode = graphresult();
if (errorcode != grOk) {
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}

setcolor(getmaxcolor());
xmax = getmaxx();
ymax = getmaxy();

line(350, 300, 450, 300);


line(350, 300, 400, 250);
line(400, 250, 450, 300);

line(400, 320, 350, 265);


line(350, 265, 450, 265);
line(445, 265, 400, 320);

getch();
2022BCSE051
YASH MODI

closegraph();
return 0;
}
OUTPUT:

2.HUT:

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

int main(void) {
int gdriver = DETECT, gmode, errorcode;
int left, top, right, bottom;

initgraph(&gdriver, &gmode, "c://turboc3/bgi");

errorcode = graphresult();
if (errorcode != grOk) {
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}

left = getmaxx() / 2 - 50;


top = getmaxy() / 2 - 50;
right = getmaxx() / 2 + 50;
bottom = getmaxy() / 2 + 50;
2022BCSE051
YASH MODI

rectangle(150, 200, 300, 350);


line(150, 200, 225, 100);
line(225, 100, 300, 200);

getch();
closegraph();
return 0;
}
OUTPUT:

3.Concentric Circle:

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

int main(void) {
int gdriver = DETECT, gmode, errorcode;
int midx, midy;
int radius = 100;

initgraph(&gdriver, &gmode, "c://turboc3/bgi");

errorcode = graphresult();
if (errorcode != grOk) {
2022BCSE051
YASH MODI

printf("Graphics error: %s\n", grapherrormsg(errorcode));


printf("Press any key to halt:");
getch();
exit(1);
}

midx = getmaxx() / 2;
midy = getmaxy() / 2;
setcolor(getmaxcolor());

circle(midx, midy, radius);


circle(midx, midy, 150);

getch();
closegraph();
return 0;
}

OUTPUT:
2022BCSE051
YASH MODI

4.Moving Car:

#include <graphics.h>
#include <winbgim.h>
#include <conio.h> // For getch()

void drawCar(int x1, int y1) {


rectangle(x1 - 40, y1 - 40, x1 + 180, y1 + 30); // Body

int tire_centre = (x1 - 40 + x1 + 180) / 2;


int tire_centre_y = y1 + 30;

arc(tire_centre - 70, tire_centre_y, 180, 360, 30); // Wheels


arc(tire_centre + 40, tire_centre_y, 180, 360, 30);

circle(tire_centre - 40, tire_centre_y, 30);


circle(tire_centre + 70, tire_centre_y, 30);

line(0, tire_centre_y + 30, getmaxx(), tire_centre_y + 30); // Road


line(x1, y1 - 100, x1 + 140, y1 - 100); // Roof
line(x1, y1 - 100, x1 - 40, y1 - 40);
line(x1 + 140, y1 - 100, x1 + 180, y1 - 40);

int line_x = (x1 + x1 + 140) / 2;

line(line_x, y1 - 60, line_x, y1 - 50); // Windows


line(line_x, y1 - 60, line_x - 60, y1 - 60);
line(line_x, y1 - 60, line_x + 60, y1 - 60);
line(line_x - 85, y1 - 50, line_x + 85, y1 - 50);
2022BCSE051
YASH MODI

line(line_x + 85, y1 - 50, line_x + 60, y1 - 60);


line(line_x - 85, y1 - 50, line_x - 60, y1 - 60);
}

int main() {
initwindow(getmaxwidth(), getmaxheight(), "Car Animation");
int y1 = getmaxy() / 2;
int page = 0;

for (int x1 = 0; x1 < getmaxx(); x1 += 1) {


setactivepage(page);
cleardevice();
drawCar(x1, y1);
setvisualpage(page);
page = 1 - page;
delay(5);
}

getch();
closegraph();
return 0;
}
OUTPUT
2022BCSE051
YASH MODI

LAB 03

1.Implementation of DDA Algorithm

#include <graphics.h>
#include <conio.h>
#include <math.h>
#include <dos.h> // Required for delay()

// Function to implement DDA algorithm


void drawLineDDA(int x1, int y1, int x2, int y2) {
int dx = x2 - x1;
int dy = y2 - y1;

int steps = (abs(dx) > abs(dy)) ? abs(dx) : abs(dy);

float xIncrement = (float)dx / steps;


float yIncrement = (float)dy / steps;

float x = x1, y = y1;

for (int i = 0; i <= steps; i++) {


putpixel((int)x, (int)y, WHITE); // Plot pixel
x += xIncrement;
y += yIncrement;
delay(10); // Small delay to visualize drawing
}
}

int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");

// Four different line cases


drawLineDDA(100, 100, 300, 200); // Case 1: Left to Right (Positive Slope)
drawLineDDA(100, 300, 300, 100); // Case 2: Left to Right (Negative Slope)
drawLineDDA(300, 100, 100, 200); // Case 3: Right to Left (Positive Slope)
drawLineDDA(300, 300, 100, 100); // Case 4: Right to Left (Negative Slope)

getch();
closegraph();
return 0;
2022BCSE051
YASH MODI

}
2022BCSE051
YASH MODI

LAB 04

1. Bresenham’s Line Generation Algorithm


#include <graphics.h>
#include <conio.h>
#include <stdio.h>
#include <dos.h>
void drawLine(int x1, int y1, int x2, int y2) {
int dx = (x2 > x1) ? (x2 - x1) : (x1 - x2);
int dy = (y2 > y1) ? (y2 - y1) : (y1 - y2);
int sx = (x1 < x2) ? 1 : -1;
int sy = (y1 < y2) ? 1 : -1;
int err = dx - dy;
while (1) {
putpixel(x1, y1, WHITE);
printf("(%d, %d)\n", x1, y1);
delay(50); // Adding delay to visualize the drawing process
if (x1 == x2 && y1 == y2) break;
int e2 = 2 * err;
if (e2 > -dy) {
err -= dy;
x1 += sx;
}
if (e2 < dx) {
err += dx;
y1 += sy;
}
}
}
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");

int x1, y1, x2, y2;


printf("Enter x1, y1: ");
scanf("%d %d", &x1, &y1);
printf("Enter x2, y2: ");
scanf("%d %d", &x2, &y2);

drawLine(x1, y1, x2, y2);

getch();
closegraph();

return 0;
2022BCSE051
YASH MODI

Output:

You might also like