0% found this document useful (0 votes)
8 views

CG LAB(1-14)neha 1

Uploaded by

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

CG LAB(1-14)neha 1

Uploaded by

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

Neha Saini 220192(A2)

Introduction

Q: What is Computer Graphics?


Ans: It is the use of computers to create and manipulate pictures on a display device. It
comprises of software techniques to create, store, modify, and represent pictures.

Computer Graphics including digital images, animations, and interactive graphics used in various
sectors such as entertainment, education, scientific visualization, and virtual reality. Computer
Graphics can be used in UI design, rendering, geometric objects, animation, and many more. In
most areas, computer graphics is an abbreviation of CG.

Q: What are examples of computer graphics?


Ans: Computer graphics is the part of computer science that studies methods for manipulating
visual content although computer graphics deals with 3D graphics, 2D graphics, and image
processing. It also deals with the creation, manipulation, and storage of different types of images
and objects. There are some of the applications of computer graphics are described below.:
 Computer Art: Using computer graphics we can create fine and commercial art which
includes animation packages, and paint packages. These packages provide facilities for
designing object shapes and specifying object motion. Cartoon drawings, paintings, and
logo designs can also be done.

 Computer-Aided automobiles Drawing: Designing buildings, automobiles, and aircraft


is done with the help of computer-aided drawing, this helps in providing minute details to
the drawing and producing more accurate and sharp drawings with better specifications.

 Presentation Graphics: For the preparation of reports or summarising the financial,


statistical, mathematical, scientific, and economic data for research reports, and
managerial reports, moreover creation of bar graphs, pie charts, and time charts, can be
done using the tools present in computer graphics.

 Entertainment: Computer graphics find a major part of its utility in the movie industry
and game industry. Used for creating motion pictures, music videos, television shows,
and cartoon animation films. In the game industry where focus and interactivity are the
key players, computer graphics help in efficiently providing such features.

 Education: Computer-generated models are extremely useful for teaching huge number
of concepts and fundamentals in an easy-to-understand and learn manner. Using
computer graphics many educational models can be created through which more interest
can be generated among the students regarding the subject.

 Training: Specialised systems for training like simulators can be used for training the
candidates in a way that can be grasped in a short span of time with better understanding.
The creation of training modules using computer graphics is simple and very useful.

 Visualization: Today the need of visualize things have increased drastically, the need of
visualization can be seen in many advanced technologies, data visualization helps in
finding insights into the data, to check and study the behavior of processes around us we
need appropriate visualization which can be achieved through proper usage of computer
graphics.
Neha Saini 220192(A2)

 Image Processing: Various kinds of photographs or images require editing in order to be


used in different places. Processing of existing images into refined ones for better
interpretation is one of the many applications of computer graphics.

 Machine Drawing: Computer graphics are very frequently used for designing,
modifying, and creating various parts of a machine and the whole machine itself, the
main reason behind using computer graphics for this purpose is the precision and clarity
we get from such drawing is ultimate and extremely desired for the safe manufacturing of
machine using these drawings.

 Graphical User Interface: The use of pictures, images, icons, pop-up menus, and
graphical objects helps in creating a user-friendly environment where working is easy and
pleasant, using computer graphics we can create such an atmosphere where everything
can be automated and anyone can get the desired action performed in an easy fashion.

These are some of the applications of computer graphics due to which it’s popularity has
increased to a huge extend and will keep on increasing with the progress in technology

Q: Why computer graphics is used?

Ans: Suppose a shoe manufacturing company want to show the sale of shoes for five years. For
this vast amount of information is to store. So a lot of time and memory will be needed. This
method will be tough to understand by a common man. In this situation graphics is a better
alternative. Graphics tools are charts and graphs. Using graphs, data can be represented in
pictorial form. A picture can be understood easily just with a single look.
Interactive computer graphics work using the concept of two-way communication between
computer users. The computer will receive signals from the input device, and the picture is
modified accordingly. Picture will be changed quickly when we apply command.

Q: What are the features of Computer graphics and multimedia?

Ans: Some features of computer graphics and multimedia include:


 Scientific visualization- A collaborative field that combines science and computer
graphics to visually represent scientific data.

 Animation- A form of pictorial presentation that can enhance engagement and make
complex concepts easier to understand.
 Raster scan- A rectangular pattern of image capture in television that is one of the most
popular types of graphics monitors
Neha Saini 220192(A2)

 Image processing-A prominent discipline in computer graphics, computer vision,


artificial intelligence, and machine learning.

Q: Where it is used ?
Ans: Computer graphics and multimedia are used in many fields, including:
 Design: Computer graphics are used in the design process for engineering and architectural
systems. For example, objects are sometimes displayed as wireframes to show their internal
features and overall shape.
 Entertainment: Computer graphics and multimedia are used in the entertainment industry
for special effects in movies and animations, such as 3D animation and VFX. Multimedia
games are also popular.
 Education: Computer graphics and multimedia are used in education.
 Marketing: Computer graphics are used in marketing.
 Healthcare: Computer graphics are used in healthcare.
 Research: Computer graphics are used for simulations in research.
 Electronic devices: Computer graphics are used on electronic devices to display images
 Digital photography: Computer graphics are used in digital photography.
 Film and television: Computer graphics are used in film and television.
 Video games: Computer graphics are used in video games.

PROGRAM -1

AIM: Write a program to make a circle.

CODE:

#include <stdio.h>
#include <conio.h>
#include <graphics.h>
Neha Saini 220192(A2)

int main() {
int gd = DETECT, gm;

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


int x = 100,y = 100,radius = 50;
circle(x,y,radius);
getch();

closegraph();
return 0;
}

OUTPUT:
Neha Saini 220192(A2)

PROGRAM -2

AIM: Draw a hut using different shapes without color.

CODE:
#include <graphics.h>
#include <conio.h>

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

// Draw the base of the hut (rectangle)


rectangle(150, 300, 400, 450);

// Draw the roof of the hut (triangle)


line(150, 300, 275, 200); // Left side of the roof
line(275, 200, 400, 300); // Right side of the roof
line(150, 300, 400, 300); // Base of the roof

// Draw the door


rectangle(240, 350, 310, 450);

// Draw a window
rectangle(180, 330, 220, 370);

getch();
closegraph();
return 0;
}
OUTPUT:
Neha Saini 220192(A2)

PROGRAM -3

AIM: Draw a hut using different shapes with color.

CODE:

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

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

// Draw the base of the hut (rectangle)


rectangle(150, 300, 400, 450);
setfillstyle(SOLID_FILL, YELLOW); // Set the fill style and color for the base
floodfill(151, 301, WHITE); // Fill the base with yellow color

// Draw the roof of the hut (triangle)


line(150, 300, 275, 200); // Left side of the roof
line(275, 200, 400, 300); // Right side of the roof
line(150, 300, 400, 300); // Base of the roof
setfillstyle(SOLID_FILL, RED); // Set the fill style and color for the roof
floodfill(276, 299, WHITE); // Fill the roof with red color

// Draw the door


rectangle(240, 350, 310, 450);
setfillstyle(SOLID_FILL, BROWN); // Set the fill style and color for the door
floodfill(241, 351, WHITE); // Fill the door with brown color

// Draw a window
rectangle(180, 330, 220, 370);
setfillstyle(SOLID_FILL, LIGHTBLUE); // Set the fill style and color for the window
floodfill(181, 331, WHITE); // Fill the window with light blue color

getch();
closegraph();
return 0;
}
Neha Saini 220192(A2)

OUTPUT:
Neha Saini 220192(A2)

PROGRAM -4

AIM: Write a program to demonstrate lineto and linerel.

CODE:

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

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

moveto(100, 100); // Set the starting point (100, 100)


lineto(200, 200); // Draw a line to the point (200, 200)
linerel(50, 30); // Draw a line 50 pixels right and 30 pixels down from the current position
(200,200),ending at (250,230)
getch();
closegraph();

return 0;
}

OUTPUT:
Neha Saini 220192(A2)

PROGRAM -5

AIM: Write a program to demonstrate moveto and moverel.

CODE:

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

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

moveto(100, 100); // Starting point of line 1


lineto(200, 200); // Ending point of line 1
moverel(30, 50); // Moves the current point to (230, 250) for line 2
lineto(250, 310); // Ending point of line 2

getch();
closegraph();

return 0;
}

OUTPUT:
Neha Saini 220192(A2)

PROGRAM -6

AIM: Write a program to draw line in different styles.

CODE:

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

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

setlinestyle(0, 15, 1); // Set the line style: 0 for solid line, 15 for pattern, 1 for thickness
line(50, 50, 200, 50); // Draw a line from (50, 50) to (200, 50)

getch();
closegraph();

return 0;
}

OUTPUT:

setlinestyle(0,15,3)

setlinestyle(2,15,3)

setlinestyle(4,15,3)
Neha Saini 220192(A2)

PROGRAM -7

AIM: Write a program to fill the objects.

CODE:

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

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

setcolor(2); // Set the drawing color to green (2)


rectangle(50, 50, 200, 150); // Draw a rectangle with corners at (50, 50) and (200, 150)

setfillstyle(1, 7); // Set the fill style to solid (1) and color to light gray (7)
floodfill(51, 51, 2); // Fill the rectangle starting from point (51, 51) with the fill color

getch();
closegraph();

return 0;
}

OUTPUT:

setfill(3,7);
Neha Saini 220192(A2)

PROGRAM -8

AIM: Write a program to implement scan conversion of a line using the Digital Differential
Analyzer (DDA) algorithm (slope <=1).

CODE:

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

// Function to draw a line using the DDA (Digital Differential Analyzer) algorithm
void DDA(int x1, int y1, int x2, int y2) {
int dx = x2 - x1; // Change in x
int dy = y2 - y1; // Change in y
int steps; // Number of steps needed to draw the line
float xIncrement; // Increment in x per step
float yIncrement; // Increment in y per step

// Determine the number of steps needed based on the greater difference


if (dx > dy) {
steps = dx;
} else {
steps = dy;
}

xIncrement = (float)dx / (float)steps;


yIncrement = (float)dy / (float)steps;

float x = x1;
float y = y1;

// Draw the line by incrementing x and y and plotting the pixel


for (int i = 0; i <= steps; i++) {
putpixel((int)(x + 0.5), (int)(y + 0.5), WHITE); // Plot the pixel
x = x + xIncrement; // Update x
y = y + yIncrement; // Update y
}
}

int main() {
int gd = DETECT;
int gm;
Neha Saini 220192(A2)

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

int x1, y1, x2, y2;

// Get the coordinates of the first point from the user


printf("Enter the coordinates of the first point (x1, y1): ");
scanf("%d %d", &x1, &y1);

// Get the coordinates of the second point from the user


printf("Enter the coordinates of the second point (x2, y2): ");
scanf("%d %d", &x2, &y2);

// Call the DDA function to draw the line


DDA(x1, y1, x2, y2);

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

OUTPUT:
Neha Saini 220192(A2)

PROGRAM -9

AIM: Write a program to implement scan conversion of a line using the Bresenham’s method
(slope <=1).

CODE:

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

// Function to draw a line using the Bresenham's line algorithm


void bresenhamLine(int x0, int y0, int x1, int y1) {
int dx, dy, p, x, y;
int xEnd;

dx = x1 - x0;
dy = y1 - y0;
p = 2 * dy - dx; // Initial decision parameter

x = x0;
y = y0;
xEnd = x1;

// Plot the initial point


putpixel(x, y, WHITE);

// Bresenham's algorithm main loop


while (x < xEnd) {
x++;

if (p < 0) {
p += 2 * dy;
} else {
y++;
p += 2 * (dy - dx);
}

// Plot the new point


putpixel(x, y, WHITE);
}
}

int main() {
int gd = DETECT;
Neha Saini 220192(A2)

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

int x0 = 100, y0 = 100;


int x1 = 300, y1 = 300;

bresenhamLine(x0, y0, x1, y1);

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

OUTPUT:
Neha Saini 220192(A2)

PROGRAM -10

AIM: Write a program to implement scan conversion of a line using the Bresenham’s method
(slope >1).

CODE:

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

void bresenhamLine(int x0, int y0, int x1, int y1) {


int dx, dy, p, x, y;
int xEnd;

dx = x1 - x0;
dy = y1 - y0;
p = 2 * dx - dy;

x = x0;
y = y0;
xEnd = x1;

// Plot the initial point


putpixel(x, y, WHITE);

// Bresenham's algorithm main loop


while (y < y1) {
y++;

if (p > 0) {
x++;
p += 2 * (dx - dy);
}
else {
p += 2 * dx;
}

putpixel(x, y, WHITE);
}
}

int main() {
int gd = DETECT;
int gm;
Neha Saini 220192(A2)

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

int x0 = 100, y0 = 100;


int x1 = 300, y1 = 300;

// Draw the line using Bresenham's algorithm


bresenhamLine(x0, y0, x1, y1);

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

OUTPUT:
Neha Saini 220192(A2)

PROGRAM -11

AIM: Write a program to implement scan conversion of a circle using the Polynomial method.

CODE:

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

void drawCircle(int h, int k, int r) {


int x = 0;
int y = r;
int p = 3 - 2 * r;

// Draw the initial points


putpixel(h + x, k + y, WHITE);
putpixel(h - x, k + y, WHITE);
putpixel(h + x, k - y, WHITE);
putpixel(h - x, k - y, WHITE);
putpixel(h + y, k + x, WHITE);
putpixel(h - y, k + x, WHITE);
putpixel(h + y, k - x, WHITE);
putpixel(h - y, k - x, WHITE);

// Midpoint circle algorithm


while (x < y) {
x++;
if (p < 0) {
p += 4 * x + 6;
} else {
y--;
p += 4 * (x - y) + 10;
}

// Draw points in all octants


putpixel(h + x, k + y, WHITE);
putpixel(h - x, k + y, WHITE);
putpixel(h + x, k - y, WHITE);
putpixel(h - x, k - y, WHITE);
putpixel(h + y, k + x, WHITE);
putpixel(h - y, k + x, WHITE);
putpixel(h + y, k - x, WHITE);
putpixel(h - y, k - x, WHITE);
}
}
Neha Saini 220192(A2)

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

int h = 200; // x-coordinate of the center


int k = 200; // y-coordinate of the center
int r = 100; // Radius of the circle

drawCircle(h, k, r);

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

OUTPUT:
Neha Saini 220192(A2)

PROGRAM -12

AIM: Write a program to implement scan conversion of a circle using the Trigonometric
method.

CODE:

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

// Function to draw a circle using trigonometric method


void drawCircle(int h, int k, int r) {
int x, y;
float angle;

for (angle = 0; angle <= 2 * M_PI; angle += 0.01) {


x = h + (int)(r * cos(angle));
y = k + (int)(r * sin(angle));
putpixel(x, y, WHITE);
}
}

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

int h = 200; // x-coordinate of the center


int k = 200; // y-coordinate of the center
int r = 100; // Radius of the circle

// Draw the circle


drawCircle(h, k, r);

getch( );
closegraph( );
return 0;
}
Neha Saini 220192(A2)

OUTPUT:
Neha Saini 220192(A2)

PROGRAM -13

AIM: Write a program to implement scan conversion of a circle using the Bresenhams method.

CODE:

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

// Function to draw a circle using Bresenham's circle algorithm


void bresenhamCircle(int h, int k, int r) {
int x = 0;
int y = r;
int p = 3 - 2 * r; // Initial decision parameter

// Draw the initial points


putpixel(h + x, k + y,
WHITE); putpixel(h - x, k + y,
WHITE); putpixel(h + x, k - y,
WHITE); putpixel(h - x, k - y,
WHITE); putpixel(h + y, k +
x, WHITE); putpixel(h - y, k +
x, WHITE); putpixel(h + y, k -
x, WHITE); putpixel(h - y, k -
x, WHITE);

while (x < y) {
x++;

if (p < 0) {
p += 4 * x + 6;
} else {
y--;
p += 4 * (x - y) + 10;
}

// Draw points in all octants


putpixel(h + x, k + y,
WHITE); putpixel(h - x, k + y,
WHITE); putpixel(h + x, k - y,
WHITE); putpixel(h - x, k - y,
WHITE); putpixel(h + y, k +
x, WHITE); putpixel(h - y, k +
x, WHITE); putpixel(h + y, k -
x, WHITE); putpixel(h - y, k -
x, WHITE);
}
Neha Saini 220192(A2)

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

int h = 200; // x-coordinate of the center


int k = 200; // y-coordinate of the center
int r = 100; // Radius of the circle

bresenhamCircle(h, k, r);

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

OUTPUT:
Neha Saini 220192(A2)

PROGRAM -14

AIM: Write a program to implement scan conversion of a circle using the Mid Point method.

CODE:

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

// Function to draw a circle using the Midpoint Circle Algorithm


void midpointCircle(int h, int k, int r) {
int x = 0;
int y = r;
int p = 1 - r;

// Draw the initial points


putpixel(h + x, k + y,
WHITE); putpixel(h - x, k + y,
WHITE); putpixel(h + x, k - y,
WHITE); putpixel(h - x, k - y,
WHITE); putpixel(h + y, k +
x, WHITE); putpixel(h - y, k +
x, WHITE); putpixel(h + y, k -
x, WHITE); putpixel(h - y, k -
x, WHITE);

while (x < y) {
x++;

if (p < 0) {
p += 2 * x + 1;
} else {
y--;
p += 2 * (x - y) + 1;
}

// Draw points in all octants


putpixel(h + x, k + y,
WHITE); putpixel(h - x, k + y,
WHITE); putpixel(h + x, k - y,
WHITE); putpixel(h - x, k - y,
WHITE); putpixel(h + y, k +
x, WHITE); putpixel(h - y, k +
x, WHITE); putpixel(h + y, k -
x, WHITE); putpixel(h - y, k -
x, WHITE);
}
Neha Saini 220192(A2)

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

int h = 200; // x-coordinate of the center


int k = 200; // y-coordinate of the center
int r = 100; // Radius of the circle

// Draw the circle


midpointCircle(h, k, r);

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

OUTPUT:

You might also like