0% found this document useful (0 votes)
9 views1 page

C++ 2D Transformations Program

Uploaded by

optimus prime
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 views1 page

C++ 2D Transformations Program

Uploaded by

optimus prime
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

/* 13.

Write a C++ program to implement translation, rotation, shear and scaling transformations
on a 2D object. */ #include #include #include #include using namespace std; class Tdimen { int
x1, y1, x2, y2, x3, y3; public: void translate(); void scale(); void rotate(); void shear(); void draw()
{ // Initial coordinates of triangle x1 = 150; y1 = 50; x2 = 100; y2 = 100; x3 = 200; y3 = 100; int
gdriver = DETECT, gmode; initgraph(&gdriver, &gmode, NULL); cleardevice(); // Draw original
triangle line(x1, y1, x2, y2); line(x2, y2, x3, y3); line(x3, y3, x1, y1); } void result() { // Draw
transformed triangle cleardevice(); line(x1, y1, x2, y2); line(x2, y2, x3, y3); line(x3, y3, x1, y1); }
}; // =============== Transformations ================== void Tdimen::translate() { float
tx, ty; cout << "Enter tx & ty: "; cin >> tx >> ty; x1 += tx; x2 += tx; x3 += tx; y1 += ty; y2 += ty; y3
+= ty; } void Tdimen::scale() { float sx, sy; cout << "Enter sx & sy: "; cin >> sx >> sy; x1 *= sx; x2
*= sx; x3 *= sx; y1 *= sy; y2 *= sy; y3 *= sy; } void Tdimen::rotate() { float deg; cout << "Enter
angle (in degrees): "; cin >> deg; deg = deg * 3.142 / 180.0; // convert to radians int x, y; x = x1;
y = y1; x1 = x * cos(deg) - y * sin(deg); y1 = x * sin(deg) + y * cos(deg); x = x2; y = y2; x2 = x *
cos(deg) - y * sin(deg); y2 = x * sin(deg) + y * cos(deg); x = x3; y = y3; x3 = x * cos(deg) - y *
sin(deg); y3 = x * sin(deg) + y * cos(deg); } void Tdimen::shear() { int choice; float shx, shy; cout
<< "Shear Options:\n1. X-Shear\n2. Y-Shear\nEnter choice: "; cin >> choice; if (choice == 1) {
cout << "Enter shear factor shx: "; cin >> shx; x1 = x1 + shx * y1; x2 = x2 + shx * y2; x3 = x3 +
shx * y3; } else if (choice == 2) { cout << "Enter shear factor shy: "; cin >> shy; y1 = y1 + shy *
x1; y2 = y2 + shy * x2; y3 = y3 + shy * x3; } else { cout << "Invalid choice!\n"; } } //
================== Main Program ================== int main() { int option; Tdimen T;
do { cout << "\n1. Translation 2. Scaling 3. Rotation 4. Shear 5. Exit\n"; cout << "Enter choice: ";
cin >> option; switch (option) { case 1: [Link](); [Link](); [Link](); break; case 2: [Link]();
[Link](); [Link](); break; case 3: [Link](); [Link](); [Link](); break; case 4: [Link]();
[Link](); [Link](); break; case 5: break; default: cout << "Invalid choice!"; } } while (option !=
5); closegraph(); return 0; }

You might also like