Write C++ Program To Implement Translation, Rotation and Scaling Transformations On Equilateral Triangle and Rhombus. Apply The Concept of Operator Overloading
Write C++ Program To Implement Translation, Rotation and Scaling Transformations On Equilateral Triangle and Rhombus. Apply The Concept of Operator Overloading
#include <cmath>
using namespace std;
class Point {
public:
float x, y;
Point(float x = 0, float y = 0) : x(x), y(y) {}
Point operator+(const Point &t) const { return Point(x + t.x, y + t.y); }
Point operator*(float s) const { return Point(x * s, y * s); }
Point operator*(const float a) const {
float r = a * (3.14159 / 180);
return Point(x * cos(r) - y * sin(r), x * sin(r) + y * cos(r));
}
void display() const { cout << "(" << x << ", " << y << ")"; }
};
class Triangle {
public:
Point p1, p2, p3;
Triangle(Point a, Point b, Point c) : p1(a), p2(b), p3(c) {}
void translate(Point t) { p1 = p1 + t, p2 = p2 + t, p3 = p3 + t; }
void scale(float s) { p1 = p1 * s, p2 = p2 * s, p3 = p3 * s; }
void rotate(float a) { p1 = p1 * a, p2 = p2 * a, p3 = p3 * a; }
void display() const { p1.display(), cout << ", ", p2.display(), cout << ", ",
p3.display(), cout << endl; }
};
class Rhombus {
public:
Point p1, p2, p3, p4;
Rhombus(Point a, Point b, Point c, Point d) : p1(a), p2(b), p3(c), p4(d) {}
void translate(Point t) { p1 = p1 + t, p2 = p2 + t, p3 = p3 + t, p4 = p4 + t; }
void scale(float s) { p1 = p1 * s, p2 = p2 * s, p3 = p3 * s, p4 = p4 * s; }
void rotate(float a) { p1 = p1 * a, p2 = p2 * a, p3 = p3 * a, p4 = p4 * a; }
void display() const { p1.display(), cout << ", ", p2.display(), cout << ", ",
p3.display(), cout << ", ", p4.display(), cout << endl; }
};
int main() {
Triangle t(Point(0, 1), Point(-0.866, -0.5), Point(0.866, -0.5));
Rhombus r(Point(-1, 0), Point(0, 1), Point(1, 0), Point(0, -1));
Point tr(2, 2);