Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // graphred.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <iostream>
- using namespace std;
- enum Color
- {
- transparent,
- black,
- white,
- red,
- unknown
- };
- struct Coords
- {
- double x, y;
- Coords(double x, double y)
- {
- this->x = x;
- this->y = y;
- }
- };
- class Shape
- {
- protected:
- Color color;
- Coords centre;
- public:
- Shape(Color color = transparent, double x = 0.0, double y = 0.0) : centre(x, y)
- {
- this->color = color;
- }
- void SetColor(Color newColor)
- {
- this->color = newColor;
- }
- virtual void Move(Coords newCentre)
- {
- centre = newCentre;
- }
- void Resize(float ratio);
- void Rotate(float angle);
- virtual void Draw()//чтобы можно было переопределять далее
- {
- cout << "(" << centre.x << ", " << centre.y << "): " <<
- (color == transparent ? "transparent" :
- (color == red ? "red" :
- (color == black ? "black" :
- (color == white ? "white" : "unknown")))) << endl;
- }
- };
- class Point : Shape
- {
- public:
- Point(Color color = black, double x = 0.0, double y = 0.0)
- : Shape(color, x, y)
- { }
- };
- class Line : Shape
- {
- Coords A, B;
- public:
- Line(Color color, double xA, double yA, double xB, double yB)
- : Shape(color, (xB + xA) / 2, (yB + yA) / 2), A(xA, yA), B(xB, yB)
- { }
- void Draw() override
- {
- cout << "Line: (" << A.x << ", " << A.y << ") - (" << B.x << ", " << B.y << "); centre in " << "(" << centre.x << ", " << centre.y << "): " <<
- (color == transparent ? "transparent" :
- (color == red ? "red" :
- (color == black ? "black" :
- (color == white ? "white" : "unknown")))) << endl;
- }
- void Move(Coords newCentre) override
- {
- A.x = A.x + (newCentre.x - centre.x);
- A.y = A.y + (newCentre.y - centre.y);
- B.x = B.x + (newCentre.x - centre.x);
- B.y = B.y + (newCentre.y - centre.y);
- centre = newCentre;
- }
- };
- class Triangle : Shape
- {
- Coords A, B, C;
- public:
- Triangle(Color color, double xA, double yA, double xB, double yB, double xC, double yC)
- : Shape(color, (xC + xB + xA) / 3, (yC + yB + yA) / 3), A(xA, yA), B(xB, yB), C(xC, yC)
- { }
- void Draw() override
- {
- cout << "Triangle: (" << A.x << ", " << A.y << ") - (" << B.x << ", " << B.y << ") - (" << C.x << ", " << C.y << "); centre in " << "(" << centre.x << ", " << centre.y << "): " <<
- (color == transparent ? "transparent" :
- (color == red ? "red" :
- (color == black ? "black" :
- (color == white ? "white" : "unknown")))) << endl;
- }
- void Move(Coords newCentre) override
- {
- A.x = A.x + (newCentre.x - centre.x);
- A.y = A.y + (newCentre.y - centre.y);
- B.x = B.x + (newCentre.x - centre.x);
- B.y = B.y + (newCentre.y - centre.y);
- C.x = C.x + (newCentre.x - centre.x);
- C.y = C.y + (newCentre.y - centre.y);
- centre = newCentre;
- }
- };
- class Quadrilateral : Shape
- {
- Coords A, B, C, D;
- public:
- Quadrilateral(Color color, double xA, double yA, double xB, double yB, double xC, double yC, double xD, double yD)
- : Shape(color, (xD + xC + xB + xA) / 4, (yD + yC + yB + yA) / 4), A(xA, yA), B(xB, yB), C(xC, yC), D(xD, yD)
- { }
- void Draw() override
- {
- cout << "Quadrilateral: (" << A.x << ", " << A.y << ") - (" << B.x << ", " << B.y << ") - (" << C.x << ", " << C.y << ") - (" << D.x << ", " << D.y << "); centre in " << "(" << centre.x << ", " << centre.y << "): " <<
- (color == transparent ? "transparent" :
- (color == red ? "red" :
- (color == black ? "black" :
- (color == white ? "white" : "unknown")))) << endl;
- }
- void Move(Coords newCentre) override
- {
- A.x = A.x + (newCentre.x - centre.x);
- A.y = A.y + (newCentre.y - centre.y);
- B.x = B.x + (newCentre.x - centre.x);
- B.y = B.y + (newCentre.y - centre.y);
- C.x = C.x + (newCentre.x - centre.x);
- C.y = C.y + (newCentre.y - centre.y);
- D.x = D.x + (newCentre.x - centre.x);
- D.y = D.y + (newCentre.y - centre.y);
- centre = newCentre;
- }
- };
- class Circle : Shape
- {
- public:
- double Radius;
- Circle(Color color, double x, double y, double Radius)
- : Shape(color, x, y)
- { }
- void Draw() override
- {
- cout << "Circle: radius=" << Radius << "; centre in " << "(" << centre.x << ", " << centre.y << "): " <<
- (color == transparent ? "transparent" :
- (color == red ? "red" :
- (color == black ? "black" :
- (color == white ? "white" : "unknown")))) << endl;
- }
- };
- int _tmain(int argc, _TCHAR* argv[])
- {
- cout << "How many shapes do you want to create?\n";
- int n;
- cin >> n;
- Shape** figures = new Shape*[n];
- for (int i = 0; i < n; i++)
- {
- Color sColor;
- cout << "Choose the color\n0-transparent\n1-black\n2-white\n3-red\n";
- int c;
- cin >> c;
- switch (c)
- {
- case 0: sColor = transparent; break;
- case 1: sColor = black; break;
- case 2: sColor = white; break;
- case 3: sColor = red; break;
- default: sColor = unknown; break;
- }
- cout << "Choose the type of shape\n0-point\n1-line\n2-triangle\n3-quadrilateral\n4-circle\n";
- int t;
- cin >> t;
- switch (t)
- {
- case 0:{ int x, y; cout << "Enter coords\n"; cin >> x >> y; figures[i] = (Shape*) new Point(sColor, x, y); break; }
- case 1:{int x1, y1, x2, y2; cout << "Enter coords of (.)A\n"; cin >> x1 >> y1;
- cout << "Enter coords of (.)B\n"; cin >> x2 >> y2;
- figures[i] = (Shape*) new Line(sColor, x1, y1, x2, y2);
- break; }
- case 2: {int x1, y1, x2, y2, x3, y3; cout << "Enter coords of (.)A\n"; cin >> x1 >> y1;
- cout << "Enter coords of (.)B\n"; cin >> x2 >> y2;
- cout << "Enter coords of (.)C\n"; cin >> x3 >> y3;
- figures[i] = (Shape*) new Triangle(sColor, x1, y1, x2, y2, x3, y3);
- break; }
- case 3: {int x1, y1, x2, y2, x3, y3, x4, y4; cout << "Enter coords of (.)A\n"; cin >> x1 >> y1;
- cout << "Enter coords of (.)B\n"; cin >> x2 >> y2;
- cout << "Enter coords of (.)C\n"; cin >> x3 >> y3;
- cout << "Enter coords of (.)D\n"; cin >> x4 >> y4;
- figures[i] = (Shape*) new Quadrilateral(sColor, x1, y1, x2, y2, x3, y3, x4, y4);
- break; }
- case 4: {int x, y; cout << "Enter coords of centre\n"; cin >> x >> y; cout << "Enter radius\n"; int r; cin >> r;
- figures[i] = (Shape*) new Circle(sColor, x, y, r);
- break; }
- default: cout << "Wrong value!\n"; break;
- }
- }
- for (int i = 0; i < n; i++)
- {
- figures[i]->Draw();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement