0% found this document useful (0 votes)
7 views10 pages

CG 4

Computer graphics

Uploaded by

fuoco414
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)
7 views10 pages

CG 4

Computer graphics

Uploaded by

fuoco414
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/ 10

#include <iostream>

#include <graphics.h>

#include <cmath>

using namespace std;

class POLYGON {

private:

int p[10][3], Trans_result[10][3], Trans_matrix[3][3];

float Rotation_result[10][3], Rotation_matrix[3][3];

float Scaling_result[10][3], Scaling_matrix[3][3];

public:

int accept_poly(int p[][3]);

void draw_poly(int p[][3], int n);

void draw_polyfloat(float p[][3], int n);

void translation(int p[][3], int n);

void rotation(int p[][3], int n);

void scaling(int p[][3], int n);

};

int POLYGON::accept_poly(int p[][3]) {

int n;

cout << "\n\n\t\tEnter number of vertices: ";

cin >> n;
for (int i = 0; i < n; i++) {

cout << "\n\n\t\tEnter (x,y) coordinate of point P" << i << ": ";

cin >> p[i][0] >> p[i][1];

p[i][2] = 1; // Homogeneous coordinate

cout << "\nPolygon vertices:\n";

for (int i = 0; i < n; i++) {

cout << p[i][0] << "\t" << p[i][1] << "\t" << p[i][2] << endl;

return n;

void POLYGON::draw_poly(int p[][3], int n) {

int gd = DETECT, gm;

initgraph(&gd, &gm, NULL);

line(320, 0, 320, 480); // Y-axis

line(0, 240, 640, 240); // X-axis

for (int i = 0; i < n; i++) {

if (i < n - 1) {

line(p[i][0] + 320, -p[i][1] + 240, p[i + 1][0] + 320, -p[i + 1][1] + 240);

} else {

line(p[i][0] + 320, -p[i][1] + 240, p[0][0] + 320, -p[0][1] + 240);


}

delay(3000);

void POLYGON::draw_polyfloat(float p[][3], int n) {

int gd = DETECT, gm;

initgraph(&gd, &gm, NULL);

line(320, 0, 320, 480); // Y-axis

line(0, 240, 640, 240); // X-axis

for (int i = 0; i < n; i++) {

if (i < n - 1) {

line(p[i][0] + 320, -p[i][1] + 240, p[i + 1][0] + 320, -p[i + 1][1] + 240);

} else {

line(p[i][0] + 320, -p[i][1] + 240, p[0][0] + 320, -p[0][1] + 240);

void POLYGON::translation(int p[10][3], int n) {

int tx, ty;

cout << "\n\n\t\tEnter X-Translation tx: ";

cin >> tx;


cout << "\n\n\t\tEnter Y-Translation ty: ";

cin >> ty;

// Translation matrix

Trans_matrix[0][0] = 1; Trans_matrix[0][1] = 0; Trans_matrix[0][2] = tx;

Trans_matrix[1][0] = 0; Trans_matrix[1][1] = 1; Trans_matrix[1][2] = ty;

Trans_matrix[2][0] = 0; Trans_matrix[2][1] = 0; Trans_matrix[2][2] = 1;

for (int i = 0; i < n; i++) {

for (int j = 0; j < 3; j++) {

Trans_result[i][j] = 0;

for (int k = 0; k < 3; k++) {

Trans_result[i][j] += p[i][k] * Trans_matrix[k][j];

cout << "\n\n\t\tPolygon after Translation...";

draw_poly(Trans_result, n);

void POLYGON::rotation(int p[][3], int n) {

float angle, sinAngle, cosAngle;

cout << "\n\n\t\tEnter the angle of rotation in degrees: ";

cin >> angle;


cout << "\n\n **** Rotation Types ****";

cout << "\n\n\t\t1. Clockwise Rotation \n\n\t\t2. Anti-Clockwise Rotation ";

int type;

cout << "\n\n\t\tEnter your choice(1-2): ";

cin >> type;

angle = angle * M_PI / 180; // Convert to radians

sinAngle = sin(angle);

cosAngle = cos(angle);

// Rotation matrix

Rotation_matrix[0][0] = cosAngle; Rotation_matrix[0][1] = (type == 1) ? -sinAngle : sinAngle;


Rotation_matrix[0][2] = 0;

Rotation_matrix[1][0] = (type == 1) ? sinAngle : -sinAngle; Rotation_matrix[1][1] = cosAngle;


Rotation_matrix[1][2] = 0;

Rotation_matrix[2][0] = 0; Rotation_matrix[2][1] = 0; Rotation_matrix[2][2] = 1;

for (int i = 0; i < n; i++) {

for (int j = 0; j < 3; j++) {

Rotation_result[i][j] = 0;

for (int k = 0; k < 3; k++) {

Rotation_result[i][j] += p[i][k] * Rotation_matrix[k][j];

}
cout << "\n\n\t\tPolygon after Rotation...";

draw_polyfloat(Rotation_result, n);

void POLYGON::scaling(int p[][3], int n) {

float Sx, Sy;

cout << "\n\n\t\tEnter X-Scaling Sx: ";

cin >> Sx;

cout << "\n\n\t\tEnter Y-Scaling Sy: ";

cin >> Sy;

// Scaling matrix

Scaling_matrix[0][0] = Sx; Scaling_matrix[0][1] = 0; Scaling_matrix[0][2] = 0;

Scaling_matrix[1][0] = 0; Scaling_matrix[1][1] = Sy; Scaling_matrix[1][2] = 0;

Scaling_matrix[2][0] = 0; Scaling_matrix[2][1] = 0; Scaling_matrix[2][2] = 1;

for (int i = 0; i < n; i++) {

for (int j = 0; j < 3; j++) {

Scaling_result[i][j] = 0;

for (int k = 0; k < 3; k++) {

Scaling_result[i][j] += p[i][k] * Scaling_matrix[k][j];

cout << "\n\n\t\tPolygon after Scaling...";


draw_polyfloat(Scaling_result, n);

int main() {

int n, p[10][3];

POLYGON p1;

cout << "\n\n **** 2-D TRANSFORMATION ****";

n = p1.accept_poly(p);

cout << "\n\n\t\tOriginal Polygon...";

p1.draw_poly(p, n);

int ch;

do {

cout << "\n\n **** 2-D TRANSFORMATION ****";

cout << "\n\n\t\t1. Translation \n\n\t\t2. Scaling \n\n\t\t3. Rotation \n\n\t\t4. Exit";

cout << "\n\n\tEnter your choice(1-4): ";

cin >> ch;

switch (ch) {

case 1:

p1.translation(p, n);

break;

case 2:

p1.scaling(p, n);
break;

case 3:

p1.rotation(p, n);

break;

case 4:

exit(0);

default:

cout << "Invalid choice! Please try again.";

} while (1);

return 0;

}
OUTPUT :

You might also like