Uploaded by Ghanshyam Jadon

kunal singh (1)

advertisement
COMPUTER GRAPHICS AND MULTIMEDIA
MADE BY AMAR TECH TEAM
(GHANSHYAM JADON,DEVANSHU YADAV,MAYANK
SUTHAR )
EXPERIMENT:-1(INTRODUCTION TO GRAPHICS)
Computer graphics is the field of computer science that deals with generating and
manipulating visual content on a computer. Graphics are an essential part of many
applications, including video games, virtual reality, digital art, and scientific
visualization.
The process of creating computer graphics involves several stages, including
modeling, rendering, and compositing. In modeling, the computer generates a 3D
representation of objects or scenes. The rendered images are then generated from
this model by calculating the way light interacts with the objects in the scene, which
gives the image its appearance. Finally, compositing combines multiple rendered
images and other visual elements to create the final image or animation.
There are several types of graphics used in computer graphics, including raster
graphics, vector graphics, and 3D graphics. Raster graphics are made up of pixels and
are commonly used for images such as photographs. Vector graphics use
mathematical equations to define lines and curves, and are used for creating images
such as logos and illustrations. 3D graphics are used for creating realistic and
immersive environments and are used in video games, films, and virtual reality
applications.
Overall, computer graphics has become an increasingly important field, as visual
content becomes more prevalent in our daily lives. The ability to create and
manipulate digital graphics has revolutionized many industries and has opened up
new possibilities for creativity and communication
COMPUTER GRAPHICS AND MULTIMEDIA
MADE BY AMAR TECH TEAM
(GHANSHYAM JADON,DEVANSHU YADAV,MAYANK
SUTHAR )
EXPERIMENT:-2(INTRODUCTION TO GRAPHICS
LIBRARY AND FUNCTIONS)
A graphic library is a collection of pre-written code and functions that help
programmers create graphical content. These libraries provide an interface between
the programmer and the graphics hardware, allowing them to create and manipulate
images and animations. There are several graphic libraries available for use, including
OpenGL, DirectX, and WebGL.
Functions are the building blocks of graphic libraries. They provide the programmer
with a set of instructions to create and manipulate graphical content. These functions
can be used to draw lines, shapes, and text, and to manipulate images in various
ways, such as rotating, scaling, and cropping. Some common graphic library
functions include:





glBegin(): This function is used to begin drawing a new object in OpenGL. It is
typically followed by a set of glVertex() function calls to define the vertices of
the object.
glClear(): This function is used to clear the screen and prepare it for a new
frame.
glColor(): This function is used to set the color for the upcoming drawing
operations.
glPushMatrix() and glPopMatrix(): These functions are used to create and
restore matrices that can be used to transform objects in 3D space.
glViewport(): This function is used to set the dimensions of the viewport,
which is the area of the screen where the graphics will be displayed.
Graphic libraries and functions are an essential part of computer graphics, allowing
programmers to create complex and visually appealing content quickly and
efficiently. Understanding these libraries and functions is critical for anyone
interested in creating graphical content for applications such as video games, virtual
reality, and digital art.
COMPUTER GRAPHICS AND MULTIMEDIA
MADE BY AMAR TECH TEAM
(GHANSHYAM JADON,DEVANSHU YADAV,MAYANK
SUTHAR )
EXPERIMEN:-3(IMPLEMENTATION OF LINE
ATTRIBUTES)
In computer graphics, line attributes are properties that can be assigned to
lines to change their appearance. There are several line attributes that can
be adjusted, including:
1. Line Width: This attribute changes the thickness of the line. A thicker
line can make it more visible and can be used to create a bold or
heavy appearance.
2. Line Style: This attribute changes the pattern of the line. For example,
a dashed line or a dotted line can be used to create a different visual
effect.
3. Line Color: This attribute changes the color of the line. A different
color can be used to make the line stand out or to blend in with the
background.
4. Line Cap Style: This attribute changes the appearance of the ends of
the line. There are several options available, including square, round,
and butt ends.
5. Line Join Style: This attribute changes the appearance of the corners
of the line. There are several options available, including round,
beveled, and mitered corners.
Line attributes are commonly used in a variety of applications, including
graphic design, CAD software, and video games. Understanding how to
adjust line attributes is important for anyone interested in creating visually
appealing graphics.
COMPUTER GRAPHICS AND MULTIMEDIA
MADE BY AMAR TECH TEAM
(GHANSHYAM JADON,DEVANSHU YADAV,MAYANK
SUTHAR )
EXPERIMENT:-4(IMPLEMENTATION OF CIRCLE
ATTRIBUTES)
In computer graphics, circle attributes refer to the properties that can be assigned to
circles to change their appearance. There are several circle attributes that can be
adjusted, including:
1. Radius: This attribute changes the size of the circle. A larger radius makes the
circle bigger, while a smaller radius makes it smaller.
2. Color: This attribute changes the color of the circle. Different colors can be
used to make the circle stand out or to blend in with the background.
3. Fill Color: This attribute changes the color used to fill the circle. By default,
circles are filled with a solid color, but this can be changed to a pattern or
gradient fill.
4. Line Width: This attribute changes the thickness of the line used to draw the
circle. A thicker line can make the circle more visible and can be used to create
a bold or heavy appearance.
5. Line Style: This attribute changes the pattern of the line used to draw the
circle. For example, a dashed line or a dotted line can be used to create a
different visual effect.
6. Line Color: This attribute changes the color of the line used to draw the circle.
A different color can be used to make the circle stand out or to blend in with
the background.
Circle attributes are commonly used in a variety of applications, including graphic
design, CAD software, and video games. Understanding how to adjust circle
attributes is important for anyone interested in creating visually appealing graphics.
The implementation of circle attributes can be done using various programming
languages such as C++, Java, and Python, by utilizing graphics libraries and their
functions to create and manipulate circles with desired attributes.
COMPUTER GRAPHICS AND MULTIMEDIA
MADE BY AMAR TECH TEAM
(GHANSHYAM JADON,DEVANSHU YADAV,MAYANK
SUTHAR )
EXPERIMENT:-5(IMPLIMENTATION OF ELLIPSE
ATTRIBUTES)
In computer graphics, ellipse attributes refer to the properties that can be assigned
to ellipses to change their appearance. Ellipses are similar to circles, but they have
two different radii, one for the x-axis and one for the y-axis. There are several ellipse
attributes that can be adjusted, including:
1. X and Y Position: This attribute changes the position of the ellipse on the x
and y axis. This can be used to move the ellipse to a different location on the
screen.
2. X and Y Radius: These attributes change the size of the ellipse along the x and
y axis. By changing the radius, the ellipse can be stretched in one direction or
compressed in another.
3. Color: This attribute changes the color of the ellipse. Different colors can be
used to make the ellipse stand out or to blend in with the background.
4. Fill Color: This attribute changes the color used to fill the ellipse. By default,
ellipses are filled with a solid color, but this can be changed to a pattern or
gradient fill.
5. Line Width: This attribute changes the thickness of the line used to draw the
ellipse. A thicker line can make the ellipse more visible and can be used to
create a bold or heavy appearance.
6. Line Style: This attribute changes the pattern of the line used to draw the
ellipse. For example, a dashed line or a dotted line can be used to create a
different visual effect.
7. Line Color: This attribute changes the color of the line used to draw the ellipse.
A different color can be used to make the ellipse stand out or to blend in with
the background.
Ellipses attributes can be implemented using graphics libraries and their functions to
create and manipulate ellipses with desired attributes. Some popular graphics
libraries for implementing ellipse attributes are OpenGL, DirectX, and Java2D.
COMPUTER GRAPHICS AND MULTIMEDIA
MADE BY AMAR TECH TEAM
(GHANSHYAM JADON,DEVANSHU YADAV,MAYANK
SUTHAR )
Understanding how to adjust ellipse attributes is important for anyone interested in
creating visually appealing graphics.
EXPERIMENT:-6(TO DRAW AND MOVE A CAR USING
GRAPHICS FUNCTION)
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600
#define CAR_WIDTH 80
#define CAR_HEIGHT 40
#define CAR_SPEED 5
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
int x = 350, y = 500;
char ch;
while (1) {
if (kbhit()) {
ch = getch();
COMPUTER GRAPHICS AND MULTIMEDIA
MADE BY AMAR TECH TEAM
(GHANSHYAM JADON,DEVANSHU YADAV,MAYANK
SUTHAR )
if (ch == 'a' || ch == 'A') {
x -= CAR_SPEED;
} else if (ch == 'd' || ch == 'D') {
x += CAR_SPEED;
}
}
cleardevice();
drawCar(x, y);
delay(10);
}
getch();
closegraph();
return 0;
}
void drawCar(int x, int y) {
setfillstyle(SOLID_FILL, RED);
rectangle(x, y, x + CAR_WIDTH, y + CAR_HEIGHT);
floodfill(x + CAR_WIDTH / 2, y + CAR_HEIGHT / 2, WHITE);
setfillstyle(SOLID_FILL, BLACK);
circle(x + 20, y + 40, 5);
floodfill(x + 20, y + 40, WHITE);
circle(x + 60, y + 40, 5);
floodfill(x + 60, y + 40, WHITE);
rectangle(x + 10, y, x + 70, y + 30);
floodfill(x + 40, y + 15, WHITE);
}
COMPUTER GRAPHICS AND MULTIMEDIA
MADE BY AMAR TECH TEAM
(GHANSHYAM JADON,DEVANSHU YADAV,MAYANK
SUTHAR )
EXPERIMENT:-7(WRITE A PROGRAM TO DRAW A LINE
USING DDA ALGORITHM)
#include <graphics.h>
#include <math.h>
void drawLineDDA(int x1, int y1, int x2, int y2);
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
drawLineDDA(100, 100, 500, 400);
getch();
closegraph();
return 0;
}
void drawLineDDA(int x1, int y1, int x2, int y2) {
COMPUTER GRAPHICS AND MULTIMEDIA
MADE BY AMAR TECH TEAM
(GHANSHYAM JADON,DEVANSHU YADAV,MAYANK
SUTHAR )
float dx = x2 - x1;
float dy = y2 - y1;
float m = dy / dx;
float y = y1;
for (int x = x1; x <= x2; x++) {
putpixel(x, round(y), WHITE);
y += m;
}
}
EXPERIMENT:-8(WRITE A PROGRAM TO DRAW A LINE
USING BRESENHAN'S ALGORITHM)
#include <graphics.h>
void drawLineBresenham(int x1, int y1, int x2, int y2);
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
drawLineBresenham(100, 100, 500, 400);
getch();
closegraph();
return 0;
}
void drawLineBresenham(int x1, int y1, int x2, int y2) {
COMPUTER GRAPHICS AND MULTIMEDIA
MADE BY AMAR TECH TEAM
(GHANSHYAM JADON,DEVANSHU YADAV,MAYANK
SUTHAR )
int dx = abs(x2 - x1);
int dy = abs(y2 - y1);
int sx = x1 < x2 ? 1 : -1;
int sy = y1 < y2 ? 1 : -1;
int err = dx - dy;
int x = x1;
int y = y1;
while (x != x2 || y != y2) {
putpixel(x, y, WHITE);
int e2 = 2 * err;
if (e2 > -dy) {
err -= dy;
x += sx;
}
if (e2 < dx) {
err += dx;
y += sy;
}
}
putpixel(x, y, WHITE);
}
COMPUTER GRAPHICS AND MULTIMEDIA
MADE BY AMAR TECH TEAM
(GHANSHYAM JADON,DEVANSHU YADAV,MAYANK
SUTHAR )
EXPERIMENT:-9(WRITE A PROGRAM TO DRAW A
CIRCLE USING MID POINT ALGORITHM)
#include <graphics.h>
void drawCircleMidpoint(int x0, int y0, int r);
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
drawCircleMidpoint(320, 240, 100);
getch();
closegraph();
return 0;
COMPUTER GRAPHICS AND MULTIMEDIA
MADE BY AMAR TECH TEAM
(GHANSHYAM JADON,DEVANSHU YADAV,MAYANK
SUTHAR )
}
void drawCircleMidpoint(int x0, int y0, int r) {
int x = 0, y = r;
int p = 1 - r;
while (x <= y) {
putpixel(x0 + x, y0 + y, WHITE);
putpixel(x0 - x, y0 + y, WHITE);
putpixel(x0 + x, y0 - y, WHITE);
putpixel(x0 - x, y0 - y, WHITE);
putpixel(x0 + y, y0 + x, WHITE);
putpixel(x0 - y, y0 + x, WHITE);
putpixel(x0 + y, y0 - x, WHITE);
putpixel(x0 - y, y0 - x, WHITE);
if (p <= 0) {
x++;
p += 2 * x + 1;
}
else {
y--;
x++;
p += 2 * (x - y) + 1;
}
}
}
COMPUTER GRAPHICS AND MULTIMEDIA
MADE BY AMAR TECH TEAM
(GHANSHYAM JADON,DEVANSHU YADAV,MAYANK
SUTHAR )
EXPERIMENT:-10(WRITE A PROGRAM TO DRAW A
CIRCLE USING BRESENHAN'S ALGORITHM)
#include <graphics.h>
void drawCircleBresenham(int x0, int y0, int r);
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
drawCircleBresenham(320, 240, 100);
getch();
COMPUTER GRAPHICS AND MULTIMEDIA
MADE BY AMAR TECH TEAM
(GHANSHYAM JADON,DEVANSHU YADAV,MAYANK
SUTHAR )
closegraph();
return 0;
}
void drawCircleBresenham(int x0, int y0, int r) {
int x = 0, y = r;
int d = 3 - 2 * r;
while (x <= y) {
putpixel(x0 + x, y0 + y, WHITE);
putpixel(x0 - x, y0 + y, WHITE);
putpixel(x0 + x, y0 - y, WHITE);
putpixel(x0 - x, y0 - y, WHITE);
putpixel(x0 + y, y0 + x, WHITE);
putpixel(x0 - y, y0 + x, WHITE);
putpixel(x0 + y, y0 - x, WHITE);
putpixel(x0 - y, y0 - x, WHITE);
if (d < 0) {
d = d + 4 * x + 6;
}
else {
d = d + 4 * (x - y) + 10;
y--;
}
x++;
}
}
COMPUTER GRAPHICS AND MULTIMEDIA
MADE BY AMAR TECH TEAM
(GHANSHYAM JADON,DEVANSHU YADAV,MAYANK
SUTHAR )
EXPERIMENT:-11(WRITE A PROGRAM TO DRAW A
ELLIPSE USING MID POINT ALGORITHM)
#include <graphics.h>
void drawEllipse(int xCenter, int yCenter, int rx, int ry)
{
int x = 0;
int y = ry;
// Initial decision parameter
int decisionParam = ry * ry - rx * rx * ry + 0.25 * rx * rx;
COMPUTER GRAPHICS AND MULTIMEDIA
MADE BY AMAR TECH TEAM
(GHANSHYAM JADON,DEVANSHU YADAV,MAYANK
SUTHAR )
int dx = 2 * ry * ry * x;
int dy = 2 * rx * rx * y;
while (dx < dy)
{
// Plot the points in all four quadrants
putpixel(xCenter + x, yCenter + y, WHITE);
putpixel(xCenter - x, yCenter + y, WHITE);
putpixel(xCenter + x, yCenter - y, WHITE);
putpixel(xCenter - x, yCenter - y, WHITE);
// Update the x-coordinate and decision parameter
x++;
dx += 2 * ry * ry;
if (decisionParam < 0)
{
decisionParam += dx + ry * ry;
}
else
{
y--;
dy -= 2 * rx * rx;
decisionParam += dx - dy + ry * ry;
}
}
decisionParam = ry * ry * (x + 0.5) * (x + 0.5) + rx * rx * (y - 1) * (y - 1) - rx * rx * ry * ry;
while (y >= 0)
{
COMPUTER GRAPHICS AND MULTIMEDIA
MADE BY AMAR TECH TEAM
(GHANSHYAM JADON,DEVANSHU YADAV,MAYANK
SUTHAR )
// Plot the points in all four quadrants
putpixel(xCenter + x, yCenter + y, WHITE);
putpixel(xCenter - x, yCenter + y, WHITE);
putpixel(xCenter + x, yCenter - y, WHITE);
putpixel(xCenter - x, yCenter - y, WHITE);
// Update the y-coordinate and decision parameter
y--;
dy -= 2 * rx * rx;
if (decisionParam > 0)
{
decisionParam += rx * rx - dy;
}
else
{
x++;
dx += 2 * ry * ry;
decisionParam += dx - dy + rx * rx;
}
}
}
int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
int xCenter = 250;
int yCenter = 250;
int rx = 150;
COMPUTER GRAPHICS AND MULTIMEDIA
MADE BY AMAR TECH TEAM
(GHANSHYAM JADON,DEVANSHU YADAV,MAYANK
SUTHAR )
int ry = 100;
drawEllipse(xCenter, yCenter, rx, ry);
getch();
closegraph();
return 0;
}
EXPERIMENT:-12(WRITE A PROGRAM TO PERFORM 2D
TRANSFORMATION-TRANSLATION)
#include <graphics.h>
COMPUTER GRAPHICS AND MULTIMEDIA
MADE BY AMAR TECH TEAM
(GHANSHYAM JADON,DEVANSHU YADAV,MAYANK
SUTHAR )
void translate(int x[], int y[], int n, int tx, int ty)
{
int i;
for(i = 0; i < n; i++)
{
x[i] += tx;
y[i] += ty;
}
}
int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
int x[] = {100, 200, 300};
int y[] = {100, 200, 150};
int n = 3;
// Original triangle
setcolor(WHITE);
drawpoly(n, x, y);
// Translated triangle
translate(x, y, n, 50, 100);
setcolor(RED);
drawpoly(n, x, y);
COMPUTER GRAPHICS AND MULTIMEDIA
MADE BY AMAR TECH TEAM
(GHANSHYAM JADON,DEVANSHU YADAV,MAYANK
SUTHAR )
getch();
closegraph();
return 0;
}
COMPUTER GRAPHICS AND MULTIMEDIA
MADE BY AMAR TECH TEAM
(GHANSHYAM JADON,DEVANSHU YADAV,MAYANK
SUTHAR )
EXPERIMENT:-13(WRITE A PROGRAM TO PERFORM 2D
TRANSFORMATION-ROTATION)
#include <graphics.h>
#include <math.h>
#define PI 3.14159265
void rotate(int x[], int y[], int n, double angle)
{
int i;
double rad = angle * PI / 180.0;
double cosAngle = cos(rad);
double sinAngle = sin(rad);
for(i = 0; i < n; i++)
{
double newX = x[i] * cosAngle - y[i] * sinAngle;
double newY = x[i] * sinAngle + y[i] * cosAngle;
x[i] = round(newX);
y[i] = round(newY);
}
}
int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
COMPUTER GRAPHICS AND MULTIMEDIA
MADE BY AMAR TECH TEAM
(GHANSHYAM JADON,DEVANSHU YADAV,MAYANK
SUTHAR )
int x[] = {100, 200, 300};
int y[] = {100, 200, 150};
int n = 3;
// Original triangle
setcolor(WHITE);
drawpoly(n, x, y);
// Rotated triangle
rotate(x, y, n, 30);
setcolor(RED);
drawpoly(n, x, y);
getch();
closegraph();
return 0;
}
EXPERIMENT:-14(WRITE A PROGRAM TO PERFORM 2D
TRANSFORMATION SCALING)
#include <graphics.h>
void scale(int x[], int y[], int n, float sx, float sy)
{int i;
for(i = 0; i < n; i++)
{
x[i] = x[i] * sx;
COMPUTER GRAPHICS AND MULTIMEDIA
MADE BY AMAR TECH TEAM
(GHANSHYAM JADON,DEVANSHU YADAV,MAYANK
SUTHAR )
y[i] = y[i] * sy;
}
}
int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
int x[] = {100, 200, 300};
int y[] = {100, 200, 150};
int n = 3;
// Original triangle
setcolor(WHITE);
drawpoly(n, x, y);
// Scaled triangle
scale(x, y, n, 1.5, 0.5);
setcolor(RED);
drawpoly(n, x, y);
getch();
closegraph();
return 0;
}
COMPUTER GRAPHICS AND MULTIMEDIA
MADE BY AMAR TECH TEAM
(GHANSHYAM JADON,DEVANSHU YADAV,MAYANK
SUTHAR )
EXPERIMENT:-15(WRITE A PROGRAM TO PERFORM 2D
TRANSFORMATION REFLECTION)
#include <stdio.h>
#include <graphics.h>
void main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
int x1 = 100, y1 = 100, x2 = 200, y2 = 200;
int x1_new = x1, y1_new = getmaxy() - y1;
int x2_new = x2, y2_new = getmaxy() - y2;
line(x1, y1, x2, y2); // Original line
delay(2000);
setcolor(RED);
line(x1_new, y1_new, x2_new, y2_new); // Reflected line
delay(2000);
closegraph();
}
COMPUTER GRAPHICS AND MULTIMEDIA
MADE BY AMAR TECH TEAM
(GHANSHYAM JADON,DEVANSHU YADAV,MAYANK
SUTHAR )
EXPERIMENT:-16(WRITE A PROGRAM TO PERFORM TO
2D TRANSFORMATION SHEARING)
#include <stdio.h>
#include <graphics.h>
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
int x1 = 100, y1 = 100, x2 = 200, y2 = 200;
// Initial coordinates
line(x1, y1, x2, y2);
// Shearing transformation
int shx = 1, shy = 0;
int newX1 = x1 + shx * y1;
int newY1 = y1 + shy * x1;
int newX2 = x2 + shx * y2;
int newY2 = y2 + shy * x2;
// Transformed coordinates
line(newX1, newY1, newX2, newY2);
COMPUTER GRAPHICS AND MULTIMEDIA
MADE BY AMAR TECH TEAM
(GHANSHYAM JADON,DEVANSHU YADAV,MAYANK
SUTHAR )
getch();
closegraph();
return 0;
}
EXPERIMENT:-17(WRITE A PPROGRAM TO PERFORM
2D COMPOSITE TRANSFORMATION)
#include <stdio.h>
#include <graphics.h>
#include <math.h>
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
int x1 = 100, y1 = 100, x2 = 200, y2 = 200;
// Initial coordinates
line(x1, y1, x2, y2);
// Translation transformation
int tx = 50, ty = 50;
int newX1 = x1 + tx;
int newY1 = y1 + ty;
int newX2 = x2 + tx;
int newY2 = y2 + ty;
COMPUTER GRAPHICS AND MULTIMEDIA
MADE BY AMAR TECH TEAM
(GHANSHYAM JADON,DEVANSHU YADAV,MAYANK
SUTHAR )
// Rotation transformation
int angle = 45;
float rad = angle * M_PI / 180.0;
int rotX1 = round(newX1 * cos(rad) - newY1 * sin(rad));
int rotY1 = round(newX1 * sin(rad) + newY1 * cos(rad));
int rotX2 = round(newX2 * cos(rad) - newY2 * sin(rad));
int rotY2 = round(newX2 * sin(rad) + newY2 * cos(rad));
// Scaling transformation
float sx = 1.5, sy = 0.5;
int scaleX1 = round(rotX1 * sx);
int scaleY1 = round(rotY1 * sy);
int scaleX2 = round(rotX2 * sx);
int scaleY2 = round(rotY2 * sy);
// Transformed coordinates
line(scaleX1, scaleY1, scaleX2, scaleY2);
getch();
closegraph();
return 0;
}
COMPUTER GRAPHICS AND MULTIMEDIA
MADE BY AMAR TECH TEAM
(GHANSHYAM JADON,DEVANSHU YADAV,MAYANK
SUTHAR )
EXPERIMENT:-18(WRITE A PROGRAM TO IMPLEMENT
COHEN SUTHERLAND 2D LINE CLIPPING AND
WINDOWING)
#include <stdio.h>
#include <graphics.h>
#define INSIDE 0
#define LEFT 1
#define RIGHT 2
#define BOTTOM 4
#define TOP 8
int computeOutCode(int x, int y, int xmin, int ymin, int xmax, int ymax) {
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;
}
COMPUTER GRAPHICS AND MULTIMEDIA
MADE BY AMAR TECH TEAM
(GHANSHYAM JADON,DEVANSHU YADAV,MAYANK
SUTHAR )
void cohenSutherland(int x1, int y1, int x2, int y2, int xmin, int ymin, int xmax, int ymax) {
int outcode1 = computeOutCode(x1, y1, xmin, ymin, xmax, ymax);
int outcode2 = computeOutCode(x2, y2, xmin, ymin, xmax, ymax);
int accept = 0;
while (1) {
if ((outcode1 | outcode2) == 0) {
// Line segment is completely inside the window
accept = 1;
break;
} else if (outcode1 & outcode2) {
// Line segment is completely outside the window
break;
} else {
// Line segment intersects the window
int x, y;
int outcode = outcode1 ? outcode1 : outcode2;
if (outcode & TOP) {
// Clip the line to the top edge of the window
x = x1 + (x2 - x1) * (ymax - y1) / (y2 - y1);
y = ymax;
} else if (outcode & BOTTOM) {
// Clip the line to the bottom edge of the window
x = x1 + (x2 - x1) * (ymin - y1) / (y2 - y1);
y = ymin;
} else if (outcode & RIGHT) {
// Clip the line to the right edge of the window
COMPUTER GRAPHICS AND MULTIMEDIA
MADE BY AMAR TECH TEAM
(GHANSHYAM JADON,DEVANSHU YADAV,MAYANK
SUTHAR )
y = y1 + (y2 - y1) * (xmax - x1) / (x2 - x1);
x = xmax;
} else if (outcode & LEFT) {
// Clip the line to the left edge of the window
y = y1 + (y2 - y1) * (xmin - x1) / (x2 - x1);
x = xmin;
}
if (outcode == outcode1) {
x1 = x;
y1 = y;
outcode1 = computeOutCode(x1, y1, xmin, ymin, xmax, ymax);
} else {
x2 = x;
y2 = y;
outcode2 = computeOutCode(x2, y2, xmin, ymin, xmax, ymax);
}
}
}
if (accept) {
// Draw the clipped line
line(x1, y1, x2, y2);
}
}
int main() {
int gd = DETECT, gm;
COMPUTER GRAPHICS AND MULTIMEDIA
MADE BY AMAR TECH TEAM
(GHANSHYAM JADON,DEVANSHU YADAV,MAYANK
SUTHAR )
initgraph(&gd, &gm, "");
// Set the window size and draw the window boundaries
int xmin = 100, ymin = 100, xmax = 400, ymax = 400;
rectangle(xmin, ymin, xmax, ymax);
}
}
EXPERIMENT:-20(WRITE A PROGRAM TO 3D
TRANSFORMATION-TRANSLATION)
#include <stdio.h>
// A structure to represent a 3D point
typedef struct {
double x;
double y;
double z;
} Point3D;
// A function to translate a 3D point by a given vector
void translatePoint(Point3D* point, double tx, double ty, double tz) {
point->x += tx;
point->y += ty;
point->z += tz;
}
int main() {
COMPUTER GRAPHICS AND MULTIMEDIA
MADE BY AMAR TECH TEAM
(GHANSHYAM JADON,DEVANSHU YADAV,MAYANK
SUTHAR )
// Define a 3D point
Point3D point = { 1.0, 2.0, 3.0 };
// Print the original point
printf("Original point: (%f, %f, %f)\n", point.x, point.y, point.z);
// Translate the point by (1, 2, 3)
translatePoint(&point, 1.0, 2.0, 3.0);
// Print the translated point
printf("Translated point: (%f, %f, %f)\n", point.x, point.y, point.z);
return 0;
}
EXPERIMENT:-21(WRITE A PROGRAM TO PERFORM 3D
TRANSFORMATION-ROTATION)
#include <stdio.h>
#include <math.h>
COMPUTER GRAPHICS AND MULTIMEDIA
MADE BY AMAR TECH TEAM
(GHANSHYAM JADON,DEVANSHU YADAV,MAYANK
SUTHAR )
// A structure to represent a 3D point
typedef struct {
double x;
double y;
double z;
} Point3D;
// A function to perform a 3D rotation around the X axis
void rotateX(Point3D* point, double angle) {
double sinTheta = sin(angle);
double cosTheta = cos(angle);
double newY = point->y * cosTheta - point->z * sinTheta;
double newZ = point->y * sinTheta + point->z * cosTheta;
point->y = newY;
point->z = newZ;
}
// A function to perform a 3D rotation around the Y axis
void rotateY(Point3D* point, double angle) {
double sinTheta = sin(angle);
double cosTheta = cos(angle);
double newX = point->x * cosTheta + point->z * sinTheta;
double newZ = -point->x * sinTheta + point->z * cosTheta;
point->x = newX;
point->z = newZ;
}
COMPUTER GRAPHICS AND MULTIMEDIA
MADE BY AMAR TECH TEAM
(GHANSHYAM JADON,DEVANSHU YADAV,MAYANK
SUTHAR )
// A function to perform a 3D rotation around the Z axis
void rotateZ(Point3D* point, double angle) {
double sinTheta = sin(angle);
double cosTheta = cos(angle);
double newX = point->x * cosTheta - point->y * sinTheta;
double newY = point->x * sinTheta + point->y * cosTheta;
point->x = newX;
point->y = newY;
}
int main() {
// Define a 3D point
Point3D point = { 1.0, 2.0, 3.0 };
// Print the original point
printf("Original point: (%f, %f, %f)\n", point.x, point.y, point.z);
// Rotate the point around the X axis by 90 degrees
rotateX(&point, M_PI / 2.0);
// Print the rotated point
printf("Rotated point: (%f, %f, %f)\n", point.x, point.y, point.z);
return 0;
}
COMPUTER GRAPHICS AND MULTIMEDIA
MADE BY AMAR TECH TEAM
(GHANSHYAM JADON,DEVANSHU YADAV,MAYANK
SUTHAR )
EXPERIMENT:-22(WRITE A PROGRAM TO PERFORM 3D
TRANSFORMATION-SCALING)
#include <stdio.h>
#include <stdlib.h>
typedef struct {
float x, y, z;
} Point3D;
void scale(Point3D *p, float sx, float sy, float sz) {
p->x *= sx;
p->y *= sy;
p->z *= sz;
}
int main() {
Point3D point = {1, 2, 3};
float sx = 2, sy = 3, sz = 4;
printf("Before scaling: (%.2f, %.2f, %.2f)\n", point.x, point.y, point.z);
scale(&point, sx, sy, sz);
printf("After scaling: (%.2f, %.2f, %.2f)\n", point.x, point.y, point.z);
return 0;
}
COMPUTER GRAPHICS AND MULTIMEDIA
MADE BY AMAR TECH TEAM
(GHANSHYAM JADON,DEVANSHU YADAV,MAYANK
SUTHAR )
EXPERIMENT:-23(WRITE A PROGRAM TO PERFORM 3D
COMPOSITE TRANSFORMATION)
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef struct {
float x, y, z;
} Point3D;
void translate(Point3D *p, float tx, float ty, float tz) {
p->x += tx;
p->y += ty;
p->z += tz;
}
void rotateX(Point3D *p, float angle) {
float rad = angle * M_PI / 180;
float cosA = cos(rad);
float sinA = sin(rad);
float y = p->y * cosA - p->z * sinA;
float z = p->y * sinA + p->z * cosA;
p->y = y;
p->z = z;
}
void rotateY(Point3D *p, float angle) {
COMPUTER GRAPHICS AND MULTIMEDIA
MADE BY AMAR TECH TEAM
(GHANSHYAM JADON,DEVANSHU YADAV,MAYANK
SUTHAR )
float rad = angle * M_PI / 180;
float cosA = cos(rad);
float sinA = sin(rad);
float x = p->x * cosA + p->z * sinA;
float z = -p->x * sinA + p->z * cosA;
p->x = x;
p->z = z;
}
void rotateZ(Point3D *p, float angle) {
float rad = angle * M_PI / 180;
float cosA = cos(rad);
float sinA = sin(rad);
float x = p->x * cosA - p->y * sinA;
float y = p->x * sinA + p->y * cosA;
p->x = x;
p->y = y;
}
void scale(Point3D *p, float sx, float sy, float sz) {
p->x *= sx;
p->y *= sy;
p->z *= sz;
}
void transform(Point3D *p, float tx, float ty, float tz, float rx, float ry, float rz, float sx, float sy, float sz)
{
translate(p, tx, ty, tz);
rotateX(p, rx);
COMPUTER GRAPHICS AND MULTIMEDIA
MADE BY AMAR TECH TEAM
(GHANSHYAM JADON,DEVANSHU YADAV,MAYANK
SUTHAR )
rotateY(p, ry);
rotateZ(p, rz);
scale(p, sx, sy, sz);
}
int main() {
Point3D point = {1, 2, 3};
float tx = 1, ty = 2, tz = 3;
float rx = 45, ry = 30, rz = 60;
float sx = 2, sy = 3, sz = 4;
printf("Before transformation: (%.2f, %.2f, %.2f)\n", point.x, point.y, point.z);
transform(&point, tx, ty, tz, rx, ry, rz, sx, sy, sz);
printf("After transformation: (%.2f, %.2f, %.2f)\n", point.x, point.y, point.z);
return 0;
}
EXPERIMENT:-24(WRITE A PROGRAM TO 3D OBJECTS
AND SCENES)
#include <GL/glut.h>
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
COMPUTER GRAPHICS AND MULTIMEDIA
MADE BY AMAR TECH TEAM
(GHANSHYAM JADON,DEVANSHU YADAV,MAYANK
SUTHAR )
glLoadIdentity();
// Set camera position
gluLookAt(0, 0, 5, 0, 0, 0, 0, 1, 0);
// Draw cube
glColor3f(1.0f, 0.0f, 0.0f);
glutSolidCube(1.0);
glFlush();
}
void reshape(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, (GLfloat)w / (GLfloat)h, 1.0, 100.0);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutCreateWindow("3D Object and Scene");
// Enable depth testing
glEnable(GL_DEPTH_TEST);
COMPUTER GRAPHICS AND MULTIMEDIA
MADE BY AMAR TECH TEAM
(GHANSHYAM JADON,DEVANSHU YADAV,MAYANK
SUTHAR )
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
EXPERIMENT:-25(WRITE A PROGRAM FOR
GENERATING FRACTAL IMAGES)
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <GL/glut.h>
#define WIDTH 800
#define HEIGHT 600
#define MAX_ITERATIONS 100
// Function to compute the Mandelbrot set
int mandelbrot(double x0, double y0) {
double x = 0.0, y = 0.0;
int i;
for (i = 0; i < MAX_ITERATIONS; i++) {
double x2 = x * x, y2 = y * y;
COMPUTER GRAPHICS AND MULTIMEDIA
MADE BY AMAR TECH TEAM
(GHANSHYAM JADON,DEVANSHU YADAV,MAYANK
SUTHAR )
if (x2 + y2 > 4.0) {
return i;
}
y = 2 * x * y + y0;
x = x2 - y2 + x0;
}
return MAX_ITERATIONS;
}
// Function to display the fractal image
void display() {
int x, y;
double scale = 3.0 / WIDTH;
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
for (y = 0; y < HEIGHT; y++) {
for (x = 0; x < WIDTH; x++) {
double x0 = (x - WIDTH / 2) * scale;
double y0 = (y - HEIGHT / 2) * scale;
int iterations = mandelbrot(x0, y0);
double hue = iterations / (double)MAX_ITERATIONS;
glColor3f(hue, hue, hue);
glVertex2i(x, y);
COMPUTER GRAPHICS AND MULTIMEDIA
MADE BY AMAR TECH TEAM
(GHANSHYAM JADON,DEVANSHU YADAV,MAYANK
SUTHAR )
}
}
glEnd();
glFlush();
}
// Function to handle window resizing
void reshape(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, w, 0, h);
}
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(WIDTH, HEIGHT);
glutCreateWindow("Fractal Image");
glClearColor(1.0, 1.0, 1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
COMPUTER GRAPHICS AND MULTIMEDIA
MADE BY AMAR TECH TEAM
(GHANSHYAM JADON,DEVANSHU YADAV,MAYANK
SUTHAR )
glutMainLoop();
return 0;
}
Download