Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <windows.h>
- #include <process.h>
- #include <stdlib.h>
- using namespace std;
- //A structure of a point, containing x,y
- struct Point
- {
- int xCoordinate = 0;
- int yCoordinate = 0;
- };
- //Abstract class
- class Shape
- {
- public:
- virtual void set() = 0;
- virtual void draw() const = 0;
- private:
- };
- class Triangle : public Shape
- {
- public:
- //For reading the input from the user and storing them into the private members
- void set();
- //For plotting Triangle shape
- void draw() const;
- Triangle();
- private:
- Point first;
- Point second;
- Point third;
- };
- class Square : public Shape
- {
- public:
- //For reading the input from the user and storing them into the private members
- void set();
- //For plotting Triangle shape
- void draw() const;
- Square();
- private:
- Point first;
- double length;
- };
- class Rectangles : public Shape
- {
- public:
- //For reading the input from the user and storing them into the private members
- void set();
- //For plotting Triangle shape
- void draw() const;
- Rectangles();
- private:
- Point first;
- Point second;
- };
- class Circle : public Shape
- {
- public:
- //For reading the input from the user and storing them into the private members
- void set();
- //For plotting Triangle shape
- void draw() const;
- Circle();
- private:
- Point first;
- double radius;
- };
- const double pi = 4 * atan(1.0);
- void gotoxy(double, double);
- void line(Point, Point);
- void circleShape(Point, double);
- int main()
- {
- int i;
- Shape* p[4];
- p[0] = new Triangle;
- p[1] = new Square;
- p[2] = new Rectangles;
- p[3] = new Circle;
- for (i = 0; i < 4; i++)
- p[i]->set();
- for (i = 0; i < 4; i++)
- {
- system("cls");
- p[i]->draw();
- Sleep(5000);
- }
- cout << endl << endl; cout << endl << endl; cout << endl << endl; cout << endl << endl; cout << endl << endl; cout << endl << endl; cout << endl << endl; cout << endl << endl;
- cout << endl << endl; cout << endl << endl; cout << endl << endl; cout << endl << endl; cout << endl << endl; cout << endl << endl; cout << endl << endl; cout << endl << endl;
- return 0;
- }
- void line(Point first, Point second)
- {
- int i, n = 10;
- double hx, hy, xPos, yPos, cXc, cYc, slope = 1;
- xPos = cXc = first.xCoordinate;
- yPos = cYc = first.yCoordinate;
- gotoxy(cXc, cYc);
- cout << '.';
- hx = second.xCoordinate - cXc;
- hy = second.yCoordinate - cYc;
- if (hx == 0)
- hy = hy / n;
- else
- {
- slope = hy / hx;
- hx /= n;
- }
- for (i = 0; i < n - 1; i++)
- {
- xPos += hx;
- if (hx == 0)
- yPos += hy;
- else
- yPos += slope * hx;
- gotoxy(xPos, yPos);
- cout << '.';
- }
- }
- void circleShape(Point center, double radius)
- {
- int i, n = 10;
- double hx, hy, xPos, yPos, cXc, cYc, diff;
- hy = 2 * radius / n;
- cXc = center.xCoordinate;
- cYc = center.yCoordinate;
- xPos = cXc;
- yPos = cYc - radius;
- gotoxy(cXc, yPos);
- cout << '.';
- for (i = 0; i < n - 1; i++)
- {
- yPos += hy;
- diff = yPos - cYc;
- hx = sqrt(radius * radius - diff * diff);
- xPos = cXc - hx;
- gotoxy(xPos, yPos);
- cout << '.';
- xPos = cXc + hx;
- gotoxy(xPos, yPos);
- cout << '.';
- }
- yPos += hy;
- gotoxy(cXc, yPos);
- cout << '.';
- }
- void gotoxy(double x, double y)
- {
- HANDLE hConsoleOutput;
- COORD dwCursorPosition;
- cout.flush();
- dwCursorPosition.X = x;
- dwCursorPosition.Y = y;
- hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
- SetConsoleCursorPosition(hConsoleOutput, dwCursorPosition);
- }
- void Triangle::set()
- {
- int x, y;
- cout << "Enter the coordinates of the triangle first point: ";
- cin >> x >> y;
- first.xCoordinate = x;
- first.yCoordinate = y;
- cout << "Enter the coordinates of the triangle second point: ";
- cin >> x >> y;
- second.xCoordinate = x;
- second.yCoordinate = y;
- cout << "Enter the coordinates of the triangle third point: ";
- cin >> x >> y;
- third.xCoordinate = x;
- third.yCoordinate = y;
- }
- void Triangle::draw() const
- {
- //line(first, second);
- //line(first, third);
- //line(second, third);
- line(first, second);
- line(second, third);
- line(third, first);
- }
- Triangle::Triangle()
- {
- first.xCoordinate = 0; first.yCoordinate = 0;
- second.xCoordinate = 0; second.yCoordinate = 0;
- third.xCoordinate = 0; third.yCoordinate = 0;
- }
- void Square::set()
- {
- int x, y, l;
- cout << "Enter the coordinates of the square first point: ";
- cin >> x >> y;
- first.xCoordinate = x;
- first.yCoordinate = y;
- cout << "Enter the side length: ";
- cin >> l;
- length = l;
- }
- void Square::draw() const
- {
- //First is given, we need to create second, third, forth
- Point second;
- second.xCoordinate = (length + first.xCoordinate);
- second.yCoordinate = first.yCoordinate;
- Point third;
- third.xCoordinate = first.xCoordinate + length;
- third.yCoordinate = first.yCoordinate + length;
- Point forth;
- forth.xCoordinate = first.xCoordinate;
- forth.yCoordinate = first.yCoordinate + length;
- line(first, second);
- line(second, third);
- line(third, forth);
- line(forth, first);
- }
- Square::Square()
- {
- first.xCoordinate = 0;
- first.yCoordinate = 0;
- length = 0;
- }
- void Rectangles::set()
- {
- int x, y;
- cout << "Enter the coordinates of the rectangle first point: ";
- cin >> x >> y;
- first.xCoordinate = x;
- first.yCoordinate = y;
- cout << "Enter the coordinates of the rectangle second point: ";
- cin >> x >> y;
- second.xCoordinate = x;
- second.yCoordinate = y;
- }
- void Rectangles::draw() const
- {
- Point third;
- third.xCoordinate = second.xCoordinate;
- third.yCoordinate = first.yCoordinate;
- Point forth;
- forth.xCoordinate = first.xCoordinate;
- forth.yCoordinate = second.yCoordinate;
- line(second, third);
- line(third, first);
- line(second, forth);
- line(forth, first);
- }
- Rectangles::Rectangles()
- {
- first.xCoordinate = 0; first.yCoordinate = 0;
- second.xCoordinate = 0; second.yCoordinate = 0;
- }
- void Circle::set()
- {
- int x, y, r;
- cout << "Enter the coordinates of the center: ";
- cin >> x >> y;
- first.xCoordinate = x;
- first.yCoordinate = y;
- cout << "Enter the radius: ";
- cin >> r;
- radius = r;
- }
- void Circle::draw() const
- {
- circleShape(first, radius);
- }
- Circle::Circle()
- {
- first.xCoordinate = 0;
- first.yCoordinate = 0;
- radius = 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement