0% found this document useful (0 votes)
9 views26 pages

Graphics File

The document outlines various practical implementations of graphics algorithms in C, including DDA and Bresenham's algorithms for line, circle, and ellipse drawing, as well as 2D transformations and Cohen-Sutherland clipping. Each section provides code snippets for the respective algorithms, demonstrating how to initialize graphics, draw shapes, and apply transformations. The document serves as a comprehensive guide for implementing basic graphics programming techniques.

Uploaded by

akarshrai0207
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)
9 views26 pages

Graphics File

The document outlines various practical implementations of graphics algorithms in C, including DDA and Bresenham's algorithms for line, circle, and ellipse drawing, as well as 2D transformations and Cohen-Sutherland clipping. Each section provides code snippets for the respective algorithms, demonstrating how to initialize graphics, draw shapes, and apply transformations. The document serves as a comprehensive guide for implementing basic graphics programming techniques.

Uploaded by

akarshrai0207
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/ 26

PRACTICAL -1

To Implement DDA Algorithms for Line

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

void drawDDALine(int x1, int y1, int x2, int y2)


{ int gd = DETECT, gm;
initgraph(&gd, &gm, "");

float dx = x2 - x1;
float dy = y2 - y1;
int steps = (abs(dx) > abs(dy)) ? abs(dx) : abs(dy);

float xInc = dx / steps;


float yInc = dy / steps;

float x = x1, y = y1;


for (int i = 0; i <= steps; i++) {
putpixel(round(x), round(y),
WHITE); x += xInc;
y += yInc;
delay(10);
}

getch();
closegraph();
}

int main() {
int x1 = 100, y1 = 100, x2 = 300, y2 = 300;
drawDDALine(x1, y1, x2,
y2); return 0;
}

OUTPUT:
PRACTICAL - 2

To implement DDA algorithms for circle

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

void drawDDACircle(int xc, int yc, int r)


{ int gd = DETECT, gm;
initgraph(&gd, &gm, "");

float x = 0, y = r;
float dTheta = 1.0 / r;
float cosTheta = cos(dTheta);
float sinTheta = sin(dTheta);

while (x <= y) {
putpixel(xc + x, yc + y, WHITE);
putpixel(xc - x, yc + y, 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 + y, yc - x, WHITE);
putpixel(xc - y, yc - x, WHITE);

float newX = x * cosTheta - y *


sinTheta; float newY = x * sinTheta + y
* cosTheta; x = newX;
y = newY;

delay(10);
}

getch();
closegraph();
}

int main() {
int xc = 250, yc = 250, r =
100; drawDDACircle(xc, yc,
r); return 0;
}
OUTPUT:
PRACTICAL - 3

To implement Bresenham's algorithms for line drawing

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

void drawBresenhamLine(int x1, int y1, int x2, int y2) {


int gd = DETECT, gm;
initgraph(&gd, &gm, "");

int dx = x2 - x1, dy = y2 - y1;


int p = 2 * dy - dx;
int x = x1, y = y1;

putpixel(x, y, WHITE);

while (x < x2) {


x++;
if (p < 0) {
p = p + 2 * dy;
} else
{
y++
;
p = p + 2 * (dy - dx);
}
putpixel(x, y,
WHITE); delay(10);
}

getch();
closegraph();
}

int main() {
int x1 = 100, y1 = 100, x2 = 300, y2 = 200;
drawBresenhamLine(x1, y1, x2, y2);
return 0;
}

OUTPUT:
PRACTICAL - 4

To implement Bresenham's algorithms for circle drawing

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

void drawBresenhamCircle(int xc, int yc, int r) {


int gd = DETECT, gm;
initgraph(&gd, &gm, "");

int x = 0, y = r;
int d = 3 - 2 * r;

while (x <= y) {
putpixel(xc + x, yc + y, WHITE);
putpixel(xc - x, yc + y, 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 + y, yc - x, WHITE);
putpixel(xc - y, yc - x, WHITE);

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

getch();
closegraph();
}

int main() {
int xc = 250, yc = 250, r = 100;
drawBresenhamCircle(xc, yc, r);
return 0;
}
OUTPUT:
PRACTICAL - 5

To implement Bresenham's algorithms for ellipse drawing

#include
<graphics.h>
#include <stdio.h>
void drawBresenhamEllipse(int xc, int yc, int rx, int
ry) { int gd = DETECT, gm;
initgraph(&gd, &gm,
""); int x = 0, y = ry;
int rxSq = rx * rx, rySq = ry * ry;
int twoRxSq = 2 * rxSq, twoRySq = 2 *
rySq; int p;
p = rySq - (rxSq * ry) + (0.25 *
rxSq); while (twoRySq * x <=
twoRxSq * y) {
putpixel(xc + x, yc + y,
WHITE); putpixel(xc - x,
yc + y, WHITE);
putpixel(xc + x, yc - y,
WHITE); putpixel(xc - x,
yc - y, WHITE); x++;
if (p < 0) {
p += twoRySq * x + rySq;
} else
{ y-
-;
p += twoRySq * x - twoRxSq * y + rySq;
}
delay(10);
}
p = rySq * (x + 0.5) * (x + 0.5) + rxSq * (y - 1) * (y - 1) - rxSq
* rySq; while (y >= 0) {
putpixel(xc + x, yc + y,
WHITE); putpixel(xc - x,
yc + y, WHITE);
putpixel(xc + x, yc - y,
WHITE); putpixel(xc - x,
yc - y, WHITE); y--;
if (p > 0) {
p -= twoRxSq * y + rxSq;
} else
{
x++;
p += twoRySq * x - twoRxSq * y + rxSq;
}
delay(10);
}
getch();
closegraph();
}
int main() {
int xc = 250, yc = 250, rx = 150, ry =
100; drawBresenhamEllipse(xc, yc,
rx, ry); return 0;
}

OUTPUT:
PRACTICAL – 6

To implement Mid-Point Circle algorithm using C

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

void midpointCircle(int xc, int yc, int r) {


int x = 0, y = r;
int p = 1 - r; // Initial decision parameter

while (x <= y) {
// Plot symmetric points
putpixel(xc + x, yc + y, WHITE);
putpixel(xc - x, yc + y, 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 + y, yc - x, WHITE);
putpixel(xc - y, yc - x, WHITE);

x++;

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

int main() {
int gd = DETECT, gm;
char path[] = "C:\\MinGW\\lib\\libbgi.a"; // Path for graphics library

initgraph(&gd, &gm, path);

int xc = 250, yc = 250, radius = 100;


midpointCircle(xc, yc, radius);

getch();
closegraph();
return 0;
}
OUTPUT:
PRACTICAL – 7

To implement Mid-Point Ellipse algorithm using C

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

void drawEllipse(int xc, int yc, int rx, int ry) {


int gd = DETECT, gm;
initgraph(&gd, &gm, "");

float dx, dy, d1, d2;


int x = 0, y = ry;

// Initial decision parameter of region 1


d1 = (ry * ry) - (rx * rx * ry) + (0.25 * rx *
rx); dx = 2 * ry * ry * x;
dy = 2 * rx * rx * y;

// Region 1
while (dx < dy)
{
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 (d1 < 0)
{ x++;
dx = dx + (2 * ry * ry);
d1 = d1 + dx + (ry *
ry);
} else
{
x++
; y--
;
dx = dx + (2 * ry *
ry); dy = dy - (2 * rx
* rx);
d1 = d1 + dx - dy + (ry * ry);
}
delay(10);
}

// Initial decision parameter of region 2


d2 = ((ry * ry) * ((x + 0.5) * (x + 0.5)))
+
((rx * rx) * ((y - 1) * (y - 1))) -
(rx * rx * ry * ry);
// Region 2
while (y >= 0) {
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 (d2 > 0) {
y--;
dy = dy - (2 * rx * rx);
d2 = d2 + (rx * rx) - dy;
} else
{ y-
-;
x++
;
dx = dx + (2 * ry *
ry); dy = dy - (2 * rx
* rx);
d2 = d2 + dx - dy + (rx * rx);
}
delay(10);
}

getch();
closegraph();
}

int main() {
int xc = 250, yc = 250, rx = 150, ry = 100;
drawEllipse(xc, yc, rx, ry);
return 0;
}

OUTPUT:
PRACTICAL – 8

To perform 2D Transformations such as translation, rotation, scaling, reflection


and shearing.

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

void drawRectangle(int x1, int y1, int x2, int y2) {


rectangle(x1, y1, x2, y2);
}

void translate(int x1, int y1, int x2, int y2, int tx, int ty) {
rectangle(x1 + tx, y1 + ty, x2 + tx, y2 + ty);
}

void scale(int x1, int y1, int x2, int y2, float sx, float sy) {
rectangle(x1 * sx, y1 * sy, x2 * sx, y2 * sy);
}

void rotate(int x1, int y1, int x2, int y2, float angle)
{ float rad = angle * (M_PI / 180.0);
int x1r = x1 * cos(rad) - y1 *
sin(rad); int y1r = x1 * sin(rad) + y1
* cos(rad); int x2r = x2 * cos(rad) -
y2 * sin(rad); int y2r = x2 * sin(rad)
+ y2 * cos(rad);

rectangle(x1r, y1r, x2r, y2r);


}

void reflectX(int x1, int y1, int x2, int y2) {


rectangle(x1, -y1, x2, -y2);
}

void reflectY(int x1, int y1, int x2, int y2)


{ rectangle(-x1, y1, -x2, y2);
}

void shearX(int x1, int y1, int x2, int y2, int shx) {
rectangle(x1 + shx * y1, y1, x2 + shx * y2, y2);
}

void shearY(int x1, int y1, int x2, int y2, int shy) {
rectangle(x1, y1 + shy * x1, x2, y2 + shy * x2);
}

int main() {
int gd = DETECT, gm;
char path[] = "C:\\MinGW\\lib\\libbgi.a";

initgraph(&gd, &gm, path);

int x1 = 100, y1 = 100, x2 = 200, y2 = 200;

setcolor(WHITE);
drawRectangle(x1, y1, x2, y2);
outtextxy(50, 50, "Original Rectangle");

setcolor(RED);
translate(x1, y1, x2, y2, 50, 50);
outtextxy(200, 50, "Translated");

setcolor(GREEN);
scale(x1, y1, x2, y2, 1.5, 1.5);
outtextxy(50, 250, "Scaled");

setcolor(BLUE);
rotate(x1, y1, x2, y2, 30);
outtextxy(300, 50, "Rotated");

setcolor(CYAN);
reflectX(x1, y1, x2, y2);
outtextxy(300, 250, "Reflected X");

setcolor(MAGENTA);
reflectY(x1, y1, x2, y2);
outtextxy(100, 350, "Reflected Y");

setcolor(YELLOW);
shearX(x1, y1, x2, y2, 1);
outtextxy(400, 50, "Shear X");

setcolor(LIGHTGREEN);
shearY(x1, y1, x2, y2, 1);
outtextxy(400, 250, "Shear Y");

getch();
closegraph();
return 0;
}
OUTPUT:
PRACTICAL – 9

To implement Cohen-Sutherland 2D clipping and window-viewport mapping

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

#define INSIDE 0
#define LEFT 1
#define RIGHT 2
#define BOTTOM 4
#define TOP 8

int xmin = 100, ymin = 100, xmax = 300, ymax =

300; 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;
}

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 == 0) && (code2 == 0)) {
accept = 1;
break;
} else if (code1 & code2) {
break;
} else {
int code_out;
int x, y;
if (code1 != 0)
code_out = code1;
else
code_out = code2;

if (code_out & TOP) {


x = x1 + (x2 - x1) * (ymax - y1) / (y2 -
y1); y = ymax;
} else if (code_out & BOTTOM) {
x = x1 + (x2 - x1) * (ymin - y1) / (y2 -
y1); y = ymin;
} else if (code_out & RIGHT) {
y = y1 + (y2 - y1) * (xmax - x1) / (x2 -
x1); x = xmax;
} else if (code_out & LEFT) {
y = y1 + (y2 - y1) * (xmin - x1) / (x2 -
x1); x = xmin;
}

if (code_out == code1) {
x1 = x;
y1 = y;
code1 = computeCode(x1, y1);
} else {
x2 =
x; y2
= y;
code2 = computeCode(x2, y2);
}
}
}

if (accept) {
setcolor(WHITE);
rectangle(xmin, ymin, xmax, ymax);
setcolor(YELLOW);
line(x1, y1, x2, y2);
outtextxy(50, 50, "Clipped Line");
}
}

void windowToViewport(int x, int y, int *xv, int *yv, int xwmin, int ywmin, int
xwmax, int ywmax, int xvmin, int yvmin, int xvmax, int yvmax) {
*xv = xvmin + (x - xwmin) * (xvmax - xvmin) / (xwmax - xwmin);
*yv = yvmin + (y - ywmin) * (yvmax - yvmin) / (ywmax - ywmin);
}

void displayWindowViewport(int x1, int y1, int x2, int y2) {


int xv1, yv1, xv2, yv2;
int xvmin = 350, yvmin = 100, xvmax = 550, yvmax = 300;

setcolor(WHITE);
rectangle(xmin, ymin, xmax, ymax);
setcolor(GREEN);
line(x1, y1, x2, y2);
outtextxy(50, 50, "Original Line in Window");

windowToViewport(x1, y1, &xv1, &yv1, xmin, ymin, xmax, ymax, xvmin, yvmin,
xvmax, yvmax);
windowToViewport(x2, y2, &xv2, &yv2, xmin, ymin, xmax, ymax, xvmin, yvmin,
xvmax, yvmax);

setcolor(WHITE);
rectangle(xvmin, yvmin, xvmax, yvmax);
setcolor(GREEN);
line(xv1, yv1, xv2, yv2);
outtextxy(400, 50, "Mapped Line in Viewport");
}

int main() {
int gd = DETECT, gm;
char path[] = "C:\\MinGW\\lib\\libbgi.a";

initgraph(&gd, &gm, path);

int x1 = 50, y1 = 150, x2 = 350, y2 = 250;

cohenSutherlandClip(x1, y1, x2, y2);


delay(2000);

cleardevice();

displayWindowViewport(x1, y1, x2,

y2);

getch();
closegraph();
return 0;
}
OUTPUT:
PRACTICAL – 10

To implement Liang Barksy Line Clipping Algorithm.

#include <stdio.h>
#include <graphics.h>
float xmin = 100, ymin = 100, xmax = 300, ymax = 300;
void liangBarsky(float x1, float y1, float x2, float y2) {
float dx = x2 - x1, dy = y2 -
y1; float p[4], q[4], t1 = 0, t2
= 1; int i;
p[0] = -dx; q[0] = x1 - xmin;
p[1] = dx; q[1] = xmax - x1;
p[2] = -dy; q[2] = y1 - ymin;
p[3] = dy; q[3] = ymax -
y1; for (i = 0; i < 4; i++) {
if (p[i] == 0) {
if (q[i] < 0) return;
} else {
float t = q[i] / p[i];
if (p[i] < 0) {
if (t > t1) t1 = t;
} else {
if (t < t2) t2 = t;
}
}
}
if (t1 > t2) return;
float nx1 = x1 + t1 * dx, ny1 = y1 + t1 *
dy; float nx2 = x1 + t2 * dx, ny2 = y1 + t2
* dy; setcolor(WHITE);
rectangle(xmin, ymin, xmax, ymax);
setcolor(YELLOW);
line(nx1, ny1, nx2, ny2);
outtextxy(50, 50, "Clipped Line");
}
int main() {
int gd = DETECT, gm;
char path[] = "C:\\MinGW\\lib\\libbgi.a";
initgraph(&gd, &gm, path);
float x1 = 50, y1 = 150, x2 = 350, y2 = 250;
liangBarsky(x1, y1, x2, y2);
getch();
closegraph();
return 0;
}
OUTPUT:
PRACTICAL - 11

To perform 3D Transformations such as translation, rotation and scaling.

#include <stdio.h>
#include
<graphics.h>
#include <math.h>
#define PI 3.14159
void project3D(int x, int y, int z, int *px, int
*py) { float factor = 0.5;
*px = x - factor * z;
*py = y - factor * z;
}

void rotateX(int *y, int *z, float angle)


{ float rad = angle * PI / 180;
int newY = *y * cos(rad) - *z *
sin(rad); int newZ = *y * sin(rad) + *z
* cos(rad);
*y = newY;
*z = newZ;
}

void rotateY(int *x, int *z, float angle)


{ float rad = angle * PI / 180;
int newX = *x * cos(rad) + *z *
sin(rad); int newZ = -(*x) * sin(rad) +
*z * cos(rad);
*x = newX;
*z = newZ;
}

void rotateZ(int *x, int *y, float angle)


{ float rad = angle * PI / 180;
int newX = *x * cos(rad) - *y *
sin(rad); int newY = *x * sin(rad) + *y
* cos(rad);
*x = newX;
*y = newY;
}

void transform3D(int points[8][3], int tx, int ty, int tz, float sx, float sy, float sz, float
angleX, float angleY, float angleZ) {
for (int i = 0; i < 8; i++)
{ points[i][0] *= sx;
points[i][1] *= sy;
points[i][2] *= sz;
rotateX(&points[i][1], &points[i][2],
angleX); rotateY(&points[i][0],
&points[i][2], angleY);
rotateZ(&points[i][0], &points[i][1],
angleZ); points[i][0] += tx;
points[i][1] +=
ty; points[i][2]
+= tz;
}
}

void drawCube(int points[8][3])


{ int px[8], py[8];
for (int i = 0; i < 8; i++) {
project3D(points[i][0], points[i][1], points[i][2], &px[i], &py[i]);
}
setcolor(WHITE);
line(px[0], py[0], px[1], py[1]);
line(px[1], py[1], px[2], py[2]);
line(px[2], py[2], px[3], py[3]);
line(px[3], py[3], px[0], py[0]);
line(px[4], py[4], px[5], py[5]);
line(px[5], py[5], px[6], py[6]);
line(px[6], py[6], px[7], py[7]);
line(px[7], py[7], px[4], py[4]);
line(px[0], py[0], px[4], py[4]);
line(px[1], py[1], px[5], py[5]);
line(px[2], py[2], px[6], py[6]);
line(px[3], py[3], px[7], py[7]);
}
int main() {
int gd = DETECT, gm;
char path[] = "C:\\MinGW\\lib\\libbgi.a";
initgraph(&gd, &gm, path);
int cube[8][3] = {
{100, 100, 100}, {200, 100, 100}, {200, 200, 100}, {100, 200, 100},
{100, 100, 200}, {200, 100, 200}, {200, 200, 200}, {100, 200, 200}
};
int tx = 0, ty = 0, tz = 0;
float scale = 1.0;
float angleX = 0, angleY = 0, angleZ =
0; for (int i = 0; i < 100; i++) {
cleardevice();
int tempCube[8][3];
for (int j = 0; j < 8; j++) {
tempCube[j][0] = cube[j][0];
tempCube[j][1] = cube[j][1];
tempCube[j][2] = cube[j][2];
}
transform3D(tempCube, tx, ty, tz, scale, scale, scale, angleX, angleY, angleZ);
drawCube(tempCube);
angleY += 5;
tx += 2;
delay(100);
}
delay(3000);
closegraph()
; return 0;
}

OUTPUT:
PRACTICAL - 12

To convert between color models

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

void RGBtoCMY(int r, int g, int b, int *c, int *m, int *y) {
*c = 255 - r;
*m = 255 - g;
*y = 255 - b;
}

void RGBtoHSV(int r, int g, int b, float *h, float *s, float *v) {
float rf = r / 255.0, gf = g / 255.0, bf = b / 255.0;
float max = fmax(fmax(rf, gf), bf);
float min = fmin(fmin(rf, gf), bf);
float delta = max - min;
*v = max;
if (max == 0) {
*s = 0;
} else {
*s = delta / max;
}

if (delta == 0) {
*h = 0;
} else if (max == rf) {
*h = 60 * fmod(((gf - bf) / delta), 6);
} else if (max == gf) {
*h = 60 * (((bf - rf) / delta) + 2);
} else {
*h = 60 * (((rf - gf) / delta) + 4);
}
if (*h < 0) *h += 360;

}
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\MinGW\lib\libbgi.a");
int r = 255, g = 0, b = 0;
int c, m, y;
float h, s, v;
RGBtoCMY(r, g, b, &c, &m, &y);
RGBtoHSV(r, g, b, &h, &s, &v);

setcolor(WHITE);
outtextxy(50, 20, "RGB to CMY and HSV Conversion");

setfillstyle(SOLID_FILL, COLOR(r, g,
b)); bar(50, 50, 150, 150);
outtextxy(50, 160, "RGB");

setfillstyle(SOLID_FILL, COLOR(c, m,
y)); bar(200, 50, 300, 150);
outtextxy(200, 160, "CMY");

int h_r = (h / 360.0) * 255;


int h_g = (s * 255);
int h_b = (v * 255);

setfillstyle(SOLID_FILL, COLOR(h_r, h_g,


h_b)); bar(350, 50, 450, 150);
outtextxy(350, 160, "HSV");

getch();
closegraph();
return 0;

OUTPUT:

You might also like