0% found this document useful (0 votes)
12 views6 pages

Computer Graphics Codes

The document contains various computer graphics programs demonstrating line generation using Slope's Method, DDA, and Bresenham's Algorithm, as well as circle generation using the Midpoint Method. It also covers 2-D transformations (translation, scaling, rotation, mirroring, and shearing) and line clipping using the Cohen-Sutherland algorithm. Additionally, it includes a text-based demo for 3D transformations (translation, scaling, and rotation).

Uploaded by

milindyadv1003
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)
12 views6 pages

Computer Graphics Codes

The document contains various computer graphics programs demonstrating line generation using Slope's Method, DDA, and Bresenham's Algorithm, as well as circle generation using the Midpoint Method. It also covers 2-D transformations (translation, scaling, rotation, mirroring, and shearing) and line clipping using the Cohen-Sutherland algorithm. Additionally, it includes a text-based demo for 3D transformations (translation, scaling, and rotation).

Uploaded by

milindyadv1003
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/ 6

// Computer Graphics Programs

// 1. Line Generation using Slope's Method, DDA and Bresenham's Algorithm


#include<iostream>
#include<graphics.h>
#include<cmath>
using namespace std;

// Slope's Method
void slopeMethod(int x1, int y1, int x2, int y2) {
float m = (float)(y2 - y1) / (x2 - x1);
for (int x = x1; x <= x2; x++) {
int y = y1 + m * (x - x1);
putpixel(x, y, WHITE);
}
}

// DDA Method
void DDA(int x1, int y1, int x2, int y2) {
int dx = x2 - x1;
int dy = y2 - y1;
int steps = max(abs(dx), abs(dy));
float Xinc = dx / (float)steps;
float Yinc = dy / (float)steps;
float x = x1;
float y = y1;
for (int i = 0; i <= steps; i++) {
putpixel(round(x), round(y), WHITE);
x += Xinc;
y += Yinc;
}
}

// Bresenham's Method
void bresenham(int x1, int y1, int x2, int y2) {
int dx = abs(x2 - x1);
int dy = abs(y2 - y1);
int p = 2 * dy - dx;
int x = x1, y = y1;
while (x <= x2) {
putpixel(x, y, WHITE);
x++;
if (p < 0)
p += 2 * dy;
else {
y++;
p += 2 * (dy - dx);
}
}
}

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

// Example usage
slopeMethod(50, 50, 200, 200);
DDA(100, 100, 300, 300);
bresenham(150, 150, 400, 400);

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

// 2. Circle Generation using Midpoint Method


#include<iostream>
#include<graphics.h>
using namespace std;

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


int x = 0, y = r;
int p = 1 - 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);
x++;
if (p < 0)
p += 2 * x + 1;
else {
y--;
p += 2 * (x - y) + 1;
}
}
}

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

drawCircle(250, 250, 100);

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

// 4. 2-D Transformations: Translation, Scaling, Rotation, Mirror, Shearing (Menu


Driven)
#include<iostream>
#include<graphics.h>
#include<cmath>
using namespace std;

void translate(int &x, int &y, int tx, int ty) {


x += tx;
y += ty;
}

void scale(int &x, int &y, int sx, int sy) {


x *= sx;
y *= sy;
}

void rotate(int &x, int &y, float angle) {


float rad = angle * 3.14159 / 180;
int x_new = x * cos(rad) - y * sin(rad);
int y_new = x * sin(rad) + y * cos(rad);
x = x_new;
y = y_new;
}

void mirror(int &x, int &y, bool x_axis, bool y_axis) {


if (x_axis) y = -y;
if (y_axis) x = -x;
}

void shear(int &x, int &y, int shx, int shy) {


int x_new = x + shx * y;
int y_new = y + shy * x;
x = x_new;
y = y_new;
}

int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, NULL);
int x = 100, y = 100;
int choice;
do {
cleardevice();
putpixel(x, y, WHITE);
cout << "1. Translate
2. Scale
3. Rotate
4. Mirror
5. Shear
6. Exit
";
cout << "Enter your choice: ";
cin >> choice;
switch(choice) {
case 1: int tx, ty;
cout << "Enter tx and ty: "; cin >> tx >> ty;
translate(x, y, tx, ty);
break;
case 2: int sx, sy;
cout << "Enter sx and sy: "; cin >> sx >> sy;
scale(x, y, sx, sy);
break;
case 3: float angle;
cout << "Enter angle: "; cin >> angle;
rotate(x, y, angle);
break;
case 4: bool x_axis, y_axis;
cout << "Mirror across X axis? (1/0) and Y axis? (1/0): "; cin >>
x_axis >> y_axis;
mirror(x, y, x_axis, y_axis);
break;
case 5: int shx, shy;
cout << "Enter shx and shy: "; cin >> shx >> shy;
shear(x, y, shx, shy);
break;
}
} while(choice != 6);
closegraph();
return 0;
}

// 5. Line Clipping using Cohen-Sutherland


#include<iostream>
#include<graphics.h>
using namespace std;

const int INSIDE = 0;


const int LEFT = 1;
const int RIGHT = 2;
const int BOTTOM = 4;
const int 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);
bool accept = false;
while (true) {
if ((code1 == 0) && (code2 == 0)) {
accept = true;
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) {
rectangle(xmin, ymin, xmax, ymax);
line(x1, y1, x2, y2);
}
}

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

cohenSutherlandClip(50, 50, 350, 350);

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

// 7. 3D Transformations: Translation, Scaling, Rotation


// (Note: Text-based demo, no 3D graphics in standard BGI graphics)

#include<iostream>
using namespace std;

void translate3D(int &x, int &y, int &z, int tx, int ty, int tz) {
x += tx;
y += ty;
z += tz;
}

void scale3D(int &x, int &y, int &z, int sx, int sy, int sz) {
x *= sx;
y *= sy;
z *= sz;
}

void rotate3D(int &x, int &y, int &z, float angleX, float angleY, float angleZ) {
float radX = angleX * 3.14159 / 180;
float radY = angleY * 3.14159 / 180;
float radZ = angleZ * 3.14159 / 180;

int y_new = y * cos(radX) - z * sin(radX);


int z_new = y * sin(radX) + z * cos(radX);
y = y_new; z = z_new;

int x_new = x * cos(radY) + z * sin(radY);


z_new = -x * sin(radY) + z * cos(radY);
x = x_new; z = z_new;

x_new = x * cos(radZ) - y * sin(radZ);


y_new = x * sin(radZ) + y * cos(radZ);
x = x_new; y = y_new;
}

int main() {
int x = 1, y = 1, z = 1;
translate3D(x, y, z, 2, 3, 4);
cout << "After Translation: " << x << " " << y << " " << z << endl;

scale3D(x, y, z, 2, 2, 2);
cout << "After Scaling: " << x << " " << y << " " << z << endl;

rotate3D(x, y, z, 30, 45, 60);


cout << "After Rotation: " << x << " " << y << " " << z << endl;

return 0;
}

You might also like