1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
| #include <vector>
#include <array>
#include <iostream>
namespace shapes
{
class Coord
{
public:
explicit Coord(double x = 0.0, double y = 0.0);
Coord(const Coord& coord);
void setX(double x);
double getX() const;
void setY(double y);
double getY() const;
private:
double m_x, m_y;
};
class Shape
{
public:
Shape(size_t coordsCount = 0);
virtual ~Shape();
virtual void draw() = 0;
virtual void move(const Coord& coord) = 0;
const std::vector<Coord>& getCoords() const;
size_t getCoordsCount() const;
void setCoord(size_t index, const Coord& coord);
void setCoord(const Coord& oldCoord, const Coord& newCoord);
protected:
void setCoordsCount(size_t count);
private:
std::vector<Coord> m_coords;
};
class Point : public Shape
{
public:
explicit Point(const Coord& coord);
Point(const Point& point);
virtual ~Point();
virtual void draw() override;
virtual void move(const Coord& coord) override;
void setCoord(const Coord& coord);
const Coord& getCoord() const;
};
class Polygon : public Shape
{
public:
explicit Polygon(const std::vector<Coord>& coords);
Polygon(const Polygon& polygon);
virtual ~Polygon();
virtual void draw() override;
virtual void move(const Coord& coord) override;
};
class Line : public Polygon
{
public:
explicit Line(
double firstX = 0.0, double firstY = 0.0,
double secondX = 0.0, double secondY = 0.0);
explicit Line(
const Coord& firstPoint,
const Coord& secondPoint);
Line(const Line& line);
virtual ~Line();
void setFirstPoint(const Coord& coord);
const Coord& getFirstPoint() const;
void setSecondPoint(const Coord& coord);
const Coord& getSecondPoint() const;
};
class Triangle : public Polygon
{
public:
explicit Triangle(
double firstX = 0.0, double firstY = 0.0,
double secondX = 0.0, double secondY = 0.0,
double thirdX = 0.0, double thirdY = 0.0);
explicit Triangle(
const Coord& firstPoint,
const Coord& secondPoint,
const Coord& thirdPoint);
explicit Triangle(const std::array<Coord, 3>& coords);
Triangle(const Triangle& triangle);
virtual ~Triangle();
void setFirstPoint(const Coord& coord);
const Coord& getFirstPoint() const;
void setSecondPoint(const Coord& coord);
const Coord& getSecondPoint() const;
void setThirdPoint(const Coord& coord);
const Coord& getThirdPoint() const;
};
class Circle : public Shape
{
public:
explicit Circle(double x = 0.0, double y = 0.0);
explicit Circle(const Coord& center);
Circle(const Circle& circle);
virtual ~Circle();
virtual void draw() override;
virtual void move(const Coord& coord) override;
void setCenterPoint(const Coord& coord);
const Coord& getCenterPoint() const;
};
Coord::Coord(double x = 0.0, double y = 0.0) :
m_x(x),
m_y(y)
{}
Coord::Coord(const Coord& coord) :
Coord::Coord(coord.getX(), coord.getY())
{}
void Coord::setX(double x)
{
m_x = x;
}
double getX() const m_x
{
return m_x;
}
void setY(double y)
{
m_y = y;
}
double getY() const
{
return m_y;
}
Shape::Shape(size_t coordsCount = 0) :
m_coords(coordsCount)
{}
Shape::~Shape()
{
m_coords.clear();
}
const std::vector<Coord>& Shape::getCoords() const
{
return m_coords;
}
size_t Shape::getCoordsCount() const
{
return m_coords.size();
}
void Shape::setCoord(size_t index, const Coord& coord)
{
m_coords[index] = coord;
}
void Shape::setCoord(const Coord& oldCoord, const Coord& newCoord)
{
for (size_t i = 0, count = m_coords.size(); i < count; i++)
{
Coord& coord = m_coords[i];
if (coord.getX() == oldCoord.getX() && coord.getX() == oldCoord.getY())
coord = newCoord;
}
}
void Shape::setCoordsCount(size_t count)
{
m_coords.resize(count);
}
Point::Point(const Coord& coord) :
Shape::Shape(1)
{
setCoord(0, coord);
}
Point::Point(const Point& point) :
Point::Point(point.getCoord())
{}
Point::~Point() {}
void Point::draw()
{
//TODO: draw logic
}
void Point::move(const Coord& coord)
{
//TODO: move logic
}
const Coord& Point::getCoord() const
{
return getCoords()[0];
}
Polygon::Polygon(const std::vector<Coord>& coords) :
Shape::Shape(coords.size())
{
for (size_t i = 0, count = coords.size(); i < count; i++)
setCoord(i, coords[i]);
}
Polygon::Polygon(const Polygon& polygon) :
Polygon::Polygon(polygon.getCoords())
{}
Polygon::~Polygon() {}
void draw()
{
//TODO: draw logic
}
void move(const Coord& coord)
{
//TODO: move logic
}
Line::Line(double firstX, double firstY, double secondX, double secondY) :
Line::Line({ firstX, firstY }, { secondX, secondY })
{}
Line::Line(const Coord& firstPoint, const Coord& secondPoint)
{
setCoordsCount(2);
setFirstPoint(firstPoint);
setSecondPoint(secondPoint);
}
Line::Line(const Line& line) :
Line::Line(line.getFirstPoint(), line.getSecondPoint())
{}
Line::~Line() {}
void Line::setFirstPoint(const Coord& coord)
{
setCoord(0, coord);
}
const Coord& Line::getFirstPoint() const
{
const std::vector<Coord>& coords = getCoords();
return coords[0];
}
void Line::setSecondPoint(const Coord& coord)
{
setCoord(1, coord);
}
const Coord& Line::getSecondPoint() const
{
const std::vector<Coord>& coords = getCoords();
return coords[1];
}
Triangle::Triangle(
double firstX = 0.0, double firstY = 0.0,
double secondX = 0.0, double secondY = 0.0,
double thirdX = 0.0, double thirdY = 0.0) :
Triangle::Triangle(
{ firstX, firstY },
{ secondX, secondY },
{ thirdX, thirdY }
)
{}
Triangle::Triangle(
const Coord& firstPoint,
const Coord& secondPoint,
const Coord& thirdPoint)
{
setCoordsCount(3);
std::vector<Coord>& coords = const_cast<std::vector<Coord>&>(getCoords());
coords[0] = firstPoint;
coords[1] = secondPoint;
coords[2] = thirdPoint;
}
Triangle::Triangle(const std::array<Coord, 3>& coords)
{
setCoordsCount(3);
std::vector<Coord>& vcoords = const_cast<std::vector<Coord>&>(getCoords());
for (size_t i = 0, count = getCoordsCount(); i < count; i++)
vcoords[i] = coords[i];
}
Triangle::Triangle(const Triangle& triangle) :
Triangle::Triangle(
triangle.getFirstPoint(),
triangle.getSecondPoint(),
triangle.getThirdPoint())
{}
Triangle::~Triangle() {}
void Triangle::setFirstPoint(const Coord& coord)
{
setCoord(0, coord);
}
const Coord& Triangle::getFirstPoint() const
{
const std::vector<Coord>& coords = getCoords();
return coords[0];
}
void Triangle::setSecondPoint(const Coord& coord)
{
setCoord(1, coord);
}
const Coord& Triangle::getSecondPoint() const
{
const std::vector<Coord>& coords = getCoords();
return coords[1];
}
void Triangle::setThirdPoint(const Coord& coord)
{
setCoord(2, coord);
}
const Coord& Triangle::getThirdPoint() const
{
const std::vector<Coord>& coords = getCoords();
return coords[2];
}
Circle::Circle(double x = 0.0, double y = 0.0) :
Circle::Circle({ x, y })
{}
Circle::Circle(cosnt Coord& center) :
Shape::Shape(1)
{
setCenterPoint(center);
}
Circle::Circle(const Circle& circle) :
Circle::Circle(circle.getCenterPoint())
{}
Circle::~Circle() {}
void Circle::draw()
{
//TODO: draw logic
}
void Circle::move(const Coord& coord)
{
//TODO: move logic
}
void Circle::setCenterPoint(const Coord& coord)
{
setCoord(0, coord);
}
const Circle::Coord& getCenterPoint() const
{
const std::vector<Coord>& coords = getCoords();
return coords[0];
}
}
int main()
{
std::cout <<
"Choose shape:\n" <<
"1. Point\n" <<
"2. Line\n" <<
"3. Triangle\n" <<
"4. Circle\n";
int command;
std::cin >> command;
shapes::Shape* shape = nullptr;
switch(command)
{
case 1:
std::cout << "Enter point coords:\n";
std::cout << "x = ";
double x;
std::cin >> x;
std::cout << "y = ";
double y;
std::cin >> y;
shape = dynamic_cast<shapes::Shape*>(new shapes::Point(x, y));
break;
case 2:
std::cout << "Enter first point coords:\n";
std::cout << "x = ";
double x1;
std::cin >> x1;
std::cout << "y = ";
double y1;
std::cin >> y1;
std::cout << "Enter second point coords:\n";
std::cout << "x = ";
double x2;
std::cin >> x2;
std::cout << "y = ";
double y2;
std::cin >> y2;
shape = dynamic_cast<shapes::Shape*>(new shapes::Line(x1, y1, x2, y2));
break;
case 3:
std::cout << "Enter first point coords:\n";
std::cout << "x = ";
double x1;
std::cin >> x1;
std::cout << "y = ";
double y1;
std::cin >> y1;
std::cout << "Enter second point coords:\n";
std::cout << "x = ";
double x2;
std::cin >> x2;
std::cout << "y = ";
double y2;
std::cin >> y2;
std::cout << "Enter third point coords:\n";
std::cout << "x = ";
double x3;
std::cin >> x3;
std::cout << "y = ";
double y3;
std::cin >> y3;
shape = dynamic_cast<shapes::Shape*>(new shapes::Triangle(x1, y1, x2, y2, x3, y3));
break;
case 4:
std::cout << "Enter point coords:\n";
std::cout << "x = ";
double x;
std::cin >> x;
std::cout << "y = ";
double y;
std::cin >> y;
shape = dynamic_cast<shapes::Shape*>(new shapes::Circle(x, y));
break;
}
do
{
std::cout <<
"Choose command:\n" <<
"1. Move\n" <<
"2. Draw\n" <<
"0. Exit\n";
std::cin >> command;
switch (command)
{
case 1:
std::cout << "Enter point coords:\n";
std::cout << "x = ";
double x;
std::cin >> x;
std::cout << "y = ";
double y;
std::cin >> y;
shape->move({ x, y });
break;
case 2:
shape->draw();
break;
}
}
while (command > 0);
if (shape)
delete shape;
return 0;
} |
Partager