Graphics.h Programming with C/C++

Graphics.h Program

Let's type the program but while saving we need to use .cpp extension because graphics.h depends on so much c++ stuff. These are not available under plain C.

Graphics.h Program

#include <graphics.h>

int main(){
int gdrive = DETECT;
int gmode;

initgraph(&gdrive, &gmode, NULL);
// you can also pass NULL for third parameter if you did above setup successfully
// example: initgraph(&gd, &gm, NULL);

arc(200, 200, 0, 360, 100);
arc(150, 150, 0, 360, 20);
arc(250, 150, 0, 360, 20);

arc(200, 200, 0, 360, 5);
arc(200, 250, 180, 360, 30);

getch();
closegraph();
}

Graphics.h Program

Upon compilation and running, the Borland Graphics Interface window will start and the Drawings will be visible.

Bresenham's Line Drawing Algorithm

/*Bresnehem*/
#include <stdio.h>
#include <graphics.h>

void drawLine(int x0, int y0, int x1, int y1) {
int dx = abs(x1 - x0);
int dy = abs(y1 - y0);
int sx = (x0 < x1) ? 1 : -1;
int sy = (y0 < y1) ? 1 : -1;
int err = dx - dy;

while (x0 != x1 || y0 != y1) {
putpixel(x0, y0, WHITE);
int e2 = 2 * err;
if (e2 > -dy) {
err -= dy;
x0 += sx;
}
if (e2 < dx) {
err += dx;
y0 += sy;
}
}
}

int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, NULL);
int x0 = 100, y0 = 100, x1 = 400, y1 = 300;
drawLine(x0, y0, x1, y1);
delay(5000);
closegraph();
return 0;
}

Mid-Point Circle Drawing Algorithm

/*Mid-Point Circle Drawing Algorithm*/
#include <stdio.h>
#include <graphics.h>

void drawCircle(int xc, int yc, int r) {
int x = r, y = 0;
putpixel(xc + x, yc - y, WHITE);
if (r > 0) {
putpixel(xc - x, yc + y, WHITE);
putpixel(xc + y, yc + x, WHITE);
putpixel(xc - y, yc - x, WHITE);
}

int P = 1 - r;
while (x > y) {
y++;
if (P <= 0)
P = P + 2*y + 1;
else {
x--;
P = P + 2*y - 2*x + 1;
}
if (x < y)
break;
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 (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, NULL);
int xc = 200, yc = 200, r = 100;
drawCircle(xc, yc, r);
delay(5000);
closegraph();
return 0;
}

Cohen-Sutherland Line Clipping Algorithm

/*Cohen-Sutherland Line Clipping Algorithm*/
#include <stdio.h>
#include <graphics.h>

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

#define xmin 50
#define ymin 50
#define xmax 300
#define 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 cohenSutherland(int x0, int y0, int x1, int y1) {
int code0 = computeCode(x0, y0);
int code1 = computeCode(x1, y1);
int accept = 0;

while (1) {
if (!(code0 | code1)) {
accept = 1;
break;
} else if (code0 & code1)
break;
else {
int codeOut = code0 ? code0 : code1;
int x, y;
if (codeOut & TOP) {
x = x0 + (x1 - x0) * (ymax - y0) / (y1 - y0);
y = ymax;
} else if (codeOut & BOTTOM) {
x = x0 + (x1 - x0) * (ymin - y0) / (y1 - y0);
y = ymin;
} else if (codeOut & RIGHT) {
y = y0 + (y1 - y0) * (xmax - x0) / (x1 - x0);
x = xmax;
} else {
y = y0 + (y1 - y0) * (xmin - x0) / (x1 - x0);
x = xmin;
}

if (codeOut == code0) {
x0 = x;
y0 = y;
code0 = computeCode(x0, y0);
} else {
x1 = x;
y1 = y;
code1 = computeCode(x1, y1);
}
}
}

if (accept)
line(x0, y0, x1, y1);
}

int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, NULL);
rectangle(xmin, ymin, xmax, ymax);
int x0 = 10, y0 = 80, x1 = 400, y1 = 300;
cohenSutherland(x0, y0, x1, y1);
delay(5000);
closegraph();
return 0;
}

Various 3D Transform

/*Various 3D Transform*/
#include <stdio.h>
#include <graphics.h>
#include <math.h>

#define PI 3.14159265

typedef struct {
float x;
float y;
float z;
} Point3D;

void draw3DObject(Point3D vertices[], int numVertices, int edges[][2], int numEdges) {
for (int i = 0; i < numEdges; i++) {
int v1 = edges[i][0];
int v2 = edges[i][1];
line(vertices[v1].x, vertices[v1].y, vertices[v2].x, vertices[v2].y);
}
}

void translate3D(Point3D vertices[], int numVertices, float tx, float ty, float tz) {
for (int i = 0; i < numVertices; i++) {
vertices[i].x += tx;
vertices[i].y += ty;
vertices[i].z += tz;
}
}

void rotate3D(Point3D vertices[], int numVertices, float angleX, float angleY, float angleZ) {
float sinX = sin(angleX * PI / 180);
float cosX = cos(angleX * PI / 180);
float sinY = sin(angleY * PI / 180);
float cosY = cos(angleY * PI / 180);
float sinZ = sin(angleZ * PI / 180);
float cosZ = cos(angleZ * PI / 180);

for (int i = 0; i < numVertices; i++) {
float x = vertices[i].x;
float y = vertices[i].y;
float z = vertices[i].z;

// Rotation around X-axis
vertices[i].y = y * cosX - z * sinX;
vertices[i].z = y * sinX + z * cosX;

// Rotation around Y-axis
float oldX = x;
x = vertices[i].x = oldX * cosY + vertices[i].z * sinY;
vertices[i].z = -oldX * sinY + vertices[i].z * cosY;

// Rotation around Z-axis
oldX = x;
float oldY = y;
vertices[i].x = oldX * cosZ - oldY * sinZ;
vertices[i].y = oldX * sinZ + oldY * cosZ;
}
}

void scale3D(Point3D vertices[], int numVertices, float sx, float sy, float sz) {
for (int i = 0; i < numVertices; i++) {
vertices[i].x *= sx;
vertices[i].y *= sy;
vertices[i].z *= sz;
}
}

void parallelProjection(Point3D vertices[], int numVertices, float d) {
for (int i = 0; i < numVertices; i++) {
vertices[i].x /= d;
vertices[i].y /= d;
}
}

void perspectiveProjection(Point3D vertices[], int numVertices, float d) {
for (int i = 0; i < numVertices; i++) {
vertices[i].x /= (1 + vertices[i].z / d);
vertices[i].y /= (1 + vertices[i].z / d);
}
}

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

// Define the vertices of a cube
Point3D vertices[] = {{-50, -50, -50}, {50, -50, -50}, {50, 50, -50}, {-50, 50, -50},
{-50, -50, 50}, {50, -50, 50}, {50, 50, 50}, {-50, 50, 50}};

// Define the edges of the cube
int edges[][2] = {{0, 1}, {1, 2}, {2, 3}, {3, 0}, {4, 5}, {5, 6}, {6, 7}, {7, 4},
{0, 4}, {1, 5}, {2, 6}, {3, 7}};

int numVertices = sizeof(vertices) / sizeof(vertices[0]);
int numEdges = sizeof(edges) / sizeof(edges[0]);

// Draw the original 3D object
draw3DObject(vertices, numVertices, edges, numEdges);
delay(2000);
cleardevice();

// Apply transformations
translate3D(vertices, numVertices, 100, 100, 100);
rotate3D(vertices, numVertices, 45, 45, 45);
scale3D(vertices, numVertices, 1.5, 1.5, 1.5);

// Draw the transformed object
draw3DObject(vertices, numVertices, edges, numEdges);
delay(2000);
cleardevice();

// Apply parallel projection
parallelProjection(vertices, numVertices, 2);

// Draw the parallel projection
draw3DObject(vertices, numVertices, edges, numEdges);
delay(2000);
cleardevice();

// Apply perspective projection
perspectiveProjection(vertices, numVertices, 200);

// Draw the perspective projection
draw3DObject(vertices, numVertices, edges, numEdges);
delay(2000);
closegraph();

return 0;
}