diff --git a/module-1/homework/Geometry/hierarchy/circle.cpp b/module-1/homework/Geometry/hierarchy/circle.cpp index e69de29b..548863c1 100755 --- a/module-1/homework/Geometry/hierarchy/circle.cpp +++ b/module-1/homework/Geometry/hierarchy/circle.cpp @@ -0,0 +1,17 @@ +#include "circle.h" + +Circle::Circle(Point center, double radius) : + Ellipse(center, center, 2 * radius) {} + +Circle::Circle(const Circle& origin) : Ellipse(origin) { +} + +Circle& Circle::operator=(const Circle& origin) { + focuses = origin.focuses; + focusSum = origin.focusSum; + return *this; +} + +double Circle::radius() { + return a; +} diff --git a/module-1/homework/Geometry/hierarchy/circle.h b/module-1/homework/Geometry/hierarchy/circle.h index e69de29b..9331eb15 100755 --- a/module-1/homework/Geometry/hierarchy/circle.h +++ b/module-1/homework/Geometry/hierarchy/circle.h @@ -0,0 +1,23 @@ +#include + +#include "ellipse.h" + +#ifndef HEIRARCHY_CIRCLE_H +#define HEIRARCHY_CIRCLE_H + +class Circle : protected Ellipse { +public: + Circle() = delete; + + Circle(Point center, double radius); + + Circle(const Circle& origin); + + Circle& operator=(const Circle& origin); + + ~Circle() = default; + + double radius(); +}; + +#endif //HEIRARCHY_CIRCLE_H diff --git a/module-1/homework/Geometry/hierarchy/ellipse.cpp b/module-1/homework/Geometry/hierarchy/ellipse.cpp index e69de29b..b2db8bdc 100755 --- a/module-1/homework/Geometry/hierarchy/ellipse.cpp +++ b/module-1/homework/Geometry/hierarchy/ellipse.cpp @@ -0,0 +1,141 @@ +#include "ellipse.h" + +Ellipse::Ellipse(Point focus1, Point focus2, double focusSum) : focuses(std::make_pair(focus1, focus2)), + focusSum(focusSum) { + a = focusSum / 2; + c = eucl(focuses.first, focuses.second) / 2; + b = std::sqrt(a * a - c * c); +} + +Ellipse::Ellipse(const Ellipse& origin) : focuses(origin.focuses), focusSum(origin.focusSum), a(origin.a), b(origin.b), + c(origin.c) {} + +Ellipse& Ellipse::operator=(const Ellipse& origin) { + focuses = origin.focuses; + focusSum = origin.focusSum; + return *this; +} + +std::pair Ellipse::getFocuses() { + return focuses; +} + +double Ellipse::eccentricity() const { + return c / a; +} + +Point Ellipse::center() const { + return {(focuses.first.x + focuses.second.x) / 2, (focuses.first.y + focuses.second.y) / 2}; +} + +double Ellipse::area() const { + return PI * a * b; +} + +double Ellipse::perimeter() const { + return 4 * (PI * a * b + (a - b)) / (a + b); +} + +bool Ellipse::operator==(const Shape& another) const { + const auto* other_ell = dynamic_cast(&another); + + if (other_ell) { + return focuses.first == other_ell->focuses.first && focuses.second == other_ell->focuses.second && + focusSum == other_ell->focusSum; + } + + return false; +} + +void Ellipse::rotate(Point center, double angle) { + double x = focuses.first.x; + double y = focuses.first.y; + focuses.first.x = + ((x - center.x) * cos(angle) - (y - center.y) * sin(angle)) + center.x; + focuses.first.y = + ((x - center.x) * sin(angle) + (y - center.y) * cos(angle)) + center.y; + + x = focuses.second.x; + y = focuses.second.y; + + focuses.second.x = + ((x - center.x) * cos(angle) - (y - center.y) * sin(angle)) + center.x; + focuses.second.y = + ((x - center.x) * sin(angle) + (y - center.y) * cos(angle)) + center.y; +} + +void Ellipse::reflex(Point center) { + focuses.first.x = 2 * center.x - focuses.first.x; + focuses.first.y = 2 * center.y - focuses.first.y; + focuses.second.x = 2 * center.x - focuses.second.x; + focuses.second.y = 2 * center.y - focuses.second.y; +} + +void Ellipse::reflex(Line axis) { + Point p1 = axis.p1; + Point p2 = axis.p2; + Point p3 = focuses.first; + double xx = p2.y - p1.y; + double yy = p1.x - p2.x; + double k = (p1.x * p2.y - p2.x * p1.y + p1.y * p3.x - p2.y * p3.x + p2.x * p3.y - p1.x * p3.y) / (xx * (p2.y - p1.y) * yy * (p1.x - p2.x)); + double px = p3.x + xx * k; + double py = p3.y + yy * k; + focuses.first.x = 2 * px - focuses.first.x; + focuses.first.y = 2 * py - focuses.first.y; + + p3 = focuses.second; + k = (p1.x * p2.y - p2.x * p1.y + p1.y * p3.x - p2.y * p3.x + p2.x * p3.y - p1.x * p3.y) / (xx * (p2.y - p1.y) + yy * (p1.x - p2.x)); + px = p3.x + xx * k; + py = p3.y + yy * k; + focuses.second.x = 2 * px - focuses.second.x; + focuses.second.y = 2 * py - focuses.second.y; + + +// Point _a = axis.p1; +// Point _b = axis.p2; +// Point _c = focuses.first; +// double v_x = _b.y - _a.y; +// double v_y = _a.x - _b.x; + +// double k = (_a.x * _b.y - _b.x * _a.y + _a.y * _c.x - _b.y * _c.x + _b.x * _c.y - _a.x * _c.y) / +// (v_x * (_b.y - _a.y) + v_y * (_a.x - _b.x)); +// double p_x = _c.x + v_x * k; +// double p_y = _c.y + v_y * k; +// focuses.first.x = 2 * p_x - focuses.first.x; +// focuses.first.y = 2 * p_y - focuses.first.y; + +// _c = focuses.second; +// k = (_a.x * _b.y - _b.x * _a.y + _a.y * _c.x - _b.y * _c.x + _b.x * _c.y - _a.x * _c.y) / +// (v_x * (_b.y - _a.y) + v_y * (_a.x - _b.x)); +// p_x = _c.x + v_x * k; +// p_y = _c.y + v_y * k; +// focuses.second.x = 2 * p_x - focuses.second.x; +// focuses.second.y = 2 * p_y - focuses.second.y; +} + +void Ellipse::scale(Point center, double coefficient) { + focuses.first.x = coefficient * (focuses.first.x - center.x) + center.x; + focuses.first.y = coefficient * (focuses.first.y - center.y) + center.y; + focuses.second.x = coefficient * (focuses.second.x - center.x) + center.x; + focuses.second.y = coefficient * (focuses.second.y - center.y) + center.y; +} + +bool Ellipse::containsPoint(Point point) const { + return eucl(focuses.first, point) + eucl(focuses.second, point) <= focusSum; +} + +bool Ellipse::isSimilarTo(const Shape& another) const { + const auto* ellipse_another = dynamic_cast(&another); + return ellipse_another && (a / ellipse_another->a == b / ellipse_another->b); +} + +bool Ellipse::isCongruentTo(const Shape& another) const { + const auto* ellipse_another = dynamic_cast(&another); + + if (ellipse_another) { + double k = a / ellipse_another->a; + return k == b / ellipse_another->b && k == 1; + } + + return false; +} diff --git a/module-1/homework/Geometry/hierarchy/ellipse.h b/module-1/homework/Geometry/hierarchy/ellipse.h index e69de29b..cbf66e20 100755 --- a/module-1/homework/Geometry/hierarchy/ellipse.h +++ b/module-1/homework/Geometry/hierarchy/ellipse.h @@ -0,0 +1,51 @@ +#include +#include +#include "mymath.h" +#include "shape.h" + +#ifndef HEIRARCHY_ELLIPSE_H +#define HEIRARCHY_ELLIPSE_H + +class Ellipse : public Shape { +public: + Ellipse(Point focus1, Point focus2, double focusSum); + + Ellipse(const Ellipse& origin); + + Ellipse& operator=(const Ellipse& origin); + + ~Ellipse() = default; + + double area() const override; + + double perimeter() const override; + + bool operator==(const Shape& another) const override; + + bool isCongruentTo(const Shape& another) const override; + + bool isSimilarTo(const Shape& another) const override; + + bool containsPoint(Point point) const override; + + void rotate(Point center, double angle) override; + + void reflex(Point center) override; + + void reflex(Line axis) override; + + void scale(Point center, double coefficient) override; + + std::pair getFocuses(); + double eccentricity() const; + Point center() const; + +protected: + std::pair focuses; + double focusSum; + double a; + double b; + double c; +}; + +#endif //HEIRARCHY_ELLIPSE_H diff --git a/module-1/homework/Geometry/hierarchy/line.cpp b/module-1/homework/Geometry/hierarchy/line.cpp index e69de29b..b4423159 100755 --- a/module-1/homework/Geometry/hierarchy/line.cpp +++ b/module-1/homework/Geometry/hierarchy/line.cpp @@ -0,0 +1,11 @@ +#include "line.h" + +Line::Line(Point p1, Point p2) : p1(p1), p2(p2) {} + +bool operator==(const Line& l1, const Line& l2) { + return (l1.p1 == l2.p1) and (l1.p2 == l2.p2); +} + +bool operator!=(const Line& l1, const Line& l2) { + return (l1.p1 != l2.p1) or (l1.p2 != l2.p2); +} diff --git a/module-1/homework/Geometry/hierarchy/line.h b/module-1/homework/Geometry/hierarchy/line.h index e69de29b..b5e74e09 100755 --- a/module-1/homework/Geometry/hierarchy/line.h +++ b/module-1/homework/Geometry/hierarchy/line.h @@ -0,0 +1,16 @@ +#include "point.h" + +#ifndef HIERARCHY_LINE_H +#define HIERARCHY_LINE_H + +struct Line { + Line(Point p1, Point p2); + + friend bool operator==(const Line& l1, const Line& l2); + friend bool operator!=(const Line& l1, const Line& l2); + + Point p1; + Point p2; +}; + +#endif //HIERARCHY_LINE_H diff --git a/module-1/homework/Geometry/hierarchy/point.cpp b/module-1/homework/Geometry/hierarchy/point.cpp index e69de29b..62a5199c 100755 --- a/module-1/homework/Geometry/hierarchy/point.cpp +++ b/module-1/homework/Geometry/hierarchy/point.cpp @@ -0,0 +1,21 @@ +#include "point.h" + +Point::Point(double x, double y) : x(x), y(y) {} + +bool operator==(const Point& p1, const Point& p2) { + return (p1.x == p2.x) and (p1.y == p2.y); +} + +bool operator!=(const Point& p1, const Point& p2) { + return (p1.x != p2.x) or (p1.y != p2.y); +} + +bool operator<(const Point& lhs, const Point& rhs) { + if (lhs.x < rhs.x) + return true; + + if (lhs.x == rhs.x) + return lhs.y < rhs.y; + + return false; +} diff --git a/module-1/homework/Geometry/hierarchy/point.h b/module-1/homework/Geometry/hierarchy/point.h index e69de29b..1acd0426 100755 --- a/module-1/homework/Geometry/hierarchy/point.h +++ b/module-1/homework/Geometry/hierarchy/point.h @@ -0,0 +1,15 @@ +#ifndef HIERARCHY_POINT_H +#define HIERARCHY_POINT_H + +struct Point { + Point(double x, double y); + + friend bool operator==(const Point& p1, const Point& p2); + friend bool operator!=(const Point& p1, const Point& p2); + friend bool operator<(const Point& lhs, const Point& rhs); + + double x; + double y; +}; + +#endif //HIERARCHY_POINT_H diff --git a/module-1/homework/Geometry/hierarchy/polygon.cpp b/module-1/homework/Geometry/hierarchy/polygon.cpp index e69de29b..191ce5f6 100755 --- a/module-1/homework/Geometry/hierarchy/polygon.cpp +++ b/module-1/homework/Geometry/hierarchy/polygon.cpp @@ -0,0 +1,207 @@ +#include "polygon.h" + + +Polygon::Polygon(std::vector& points) : points(points) { } + +Polygon::Polygon(const Polygon& origin) { + points = origin.points; +} + +Polygon& Polygon::operator=(const Polygon& origin) { + points = origin.points; + return *this; +} + +Polygon::~Polygon() { + points.clear(); +} + +size_t Polygon::verticesCount() const { + return points.size(); +} + +double Polygon::perimeter() const { + double perimeter = 0; + + perimeter += eucl(points.back(), points.front()); + + for (size_t i = 0; i < verticesCount() - 1; ++i) { + perimeter += eucl(points[i], points[i + 1]); + } + + return perimeter; +} + +double Polygon::area() const { + double sum = 0; + size_t size = verticesCount(); + + for (size_t i = 0; i < size; ++i) + sum += points[i].x * points[(i + 1) % size].y - points[(i + 1) % size].x * points[i].y; + + sum = std::abs(sum); + sum /= 2; + + return sum; +} + +bool Polygon::operator==(const Shape& another) const { + const auto* other_pol = dynamic_cast(&another); + + if (other_pol) { + if (verticesCount() != other_pol->verticesCount()) + return false; + + auto points1 = points; + auto points2 = other_pol->points; + std::sort(points1.begin(), points1.end()); + std::sort(points2.begin(), points2.end()); + + for (size_t i = 0; i < verticesCount(); ++i) + if (eucl(points1[i], points2[i]) > EPS) + return false; + + return true; + } + + return false; +} + +bool Polygon::isCongruentTo(const Shape& another) const { + const auto& other_pol = dynamic_cast(another); + + double k = eucl(points.front(), points.back()) / eucl(other_pol.points.front(), other_pol.points.back()); + + return k == 1 && isSimilarTo(another); +} + +bool Polygon::isSimilarTo(const Shape& another) const { + const auto& other_pol = dynamic_cast(another); + + if (verticesCount() != other_pol.verticesCount()) + return false; + + double k = eucl(points.front(), points.back()) / eucl(other_pol.points.front(), other_pol.points.back()); + + for (size_t i = 0; i < verticesCount() - 1; ++i) { + double nk = eucl(points[i], points[i + 1]) / eucl(other_pol.points[i], other_pol.points[i + 1]); + + if (nk != k) + return false; + } + + return true; +} + +bool Polygon::containsPoint(Point point) const { + bool flag; + double sFullTriangle; + double sFirstPartTriangle; + double sSecondPartTriangle; + double sThirdPartTriangle; + + for (size_t i = 0; i < verticesCount(); ++i) { + for (size_t j = 0; j < verticesCount() - 2; ++j) { + size_t i1 = (j + i) % verticesCount(); + size_t i2 = (j + i + 1) % verticesCount(); + + sFullTriangle = std::abs(points[i1].x * (points[i2].y - points[i].y) + + points[i2].x * (points[i].y - points[i1].y) + + points[i].x * (points[i1].y - points[i2].y)); + sFirstPartTriangle = std::abs(points[i1].x * (points[i2].y - point.y) + + points[i2].x * (point.y - points[i1].y) + + point.x * (points[i1].y - points[i2].y)); + sSecondPartTriangle = std::abs(points[i].x * (points[i2].y - point.y) + + points[i2].x * (point.y - points[i].y) + + point.x * (points[i].y - points[i2].y)); + sThirdPartTriangle = std::abs(points[i1].x * (points[i].y - point.y) + + points[i].x * (point.y - points[i1].y) + + point.x * (points[i1].y - points[i].y)); + + if (sFullTriangle == sFirstPartTriangle + sSecondPartTriangle + sThirdPartTriangle) { + flag = true; + break; + } + } + + if (!flag) + break; + } + + return flag; +} + +void Polygon::rotate(Point center, double angle) { + angle = angle / 180 * PI; + for (size_t i = 0; i < verticesCount(); ++i) { + double x = points[i].x - center.x; + double y = points[i].y - center.y; + points[i].x = (x * cos(angle) - y * sin(angle)) + center.x; + points[i].y = (x * sin(angle) + y * cos(angle)) + center.y; + } +} + +void Polygon::reflex(Point center) { + for (size_t i = 0; i < verticesCount(); ++i) { + points[i].x = 2 * center.x - points[i].x; + points[i].y = 2 * center.y - points[i].y; + } +} + +void Polygon::reflex(Line axis) { + for (size_t i = 0; i < verticesCount(); ++i) { + Point a = axis.p1; + Point b = axis.p2; + Point c = points[i]; + double v_x = b.y - a.y; + double v_y = a.x - b.x; + double k = (a.x * b.y - b.x * a.y + a.y * c.x - b.y * c.x + b.x * c.y - a.x * c.y) / + (v_x * (b.y - a.y) + v_y * (a.x - b.x)); + double p_x = c.x + v_x * k; + double p_y = c.y + v_y * k; + points[i].x = 2 * p_x - points[i].x; + points[i].y = 2 * p_y - points[i].y; + } +} + +void Polygon::scale(Point center, double coefficient) { + for (size_t i = 0; i < verticesCount(); ++i) { + points[i].x = coefficient * (points[i].x - center.x) + center.x; + points[i].y = coefficient * (points[i].y - center.y) + center.y; + } +} + +std::vector Polygon::getVertices() const { + return points; +} + +bool Polygon::isConvex() const { + Point vector_a_buf{ + points.front().x - points.back().x, + points.front().y - points.back().y + }; + + Point vector_b_buf{ + points[0].x - points[1].x, + points[0].y - points[1].y + }; + + bool sign = vector_a_buf.x * vector_b_buf.y - vector_a_buf.y * vector_b_buf.x >= 0; + + for (size_t i = 1; i < verticesCount(); ++i) { + Point vector_a{ + points[i].x - points[i - 1].x, + points[i].y - points[i - 1].y + }; + + Point vector_b{ + points[i].x - points[(i + 1) % verticesCount()].x, + points[i].y - points[(i + 1) % verticesCount()].y + }; + + if (sign != vector_a.x * vector_b.y - vector_a.y * vector_b.x >= 0) + return false; + } + + return true; +} diff --git a/module-1/homework/Geometry/hierarchy/polygon.h b/module-1/homework/Geometry/hierarchy/polygon.h index e69de29b..84edc1d1 100755 --- a/module-1/homework/Geometry/hierarchy/polygon.h +++ b/module-1/homework/Geometry/hierarchy/polygon.h @@ -0,0 +1,53 @@ +#include +#include "shape.h" +#include +#include +#include "mymath.h" + +#ifndef HEIRARCHY_POLYGON_H +#define HEIRARCHY_POLYGON_H + +class Polygon : public Shape { +public: + explicit Polygon(std::vector& points); + + Polygon(const Polygon& origin); + + Polygon& operator=(const Polygon& origin); + + ~Polygon(); + + double area() const override; + + double perimeter() const override; + + bool operator==(const Shape& another) const override; + + bool isCongruentTo(const Shape& another) const override; + + bool isSimilarTo(const Shape& another) const override; + + bool containsPoint(Point point) const override; + + void rotate(Point center, double angle) override; + + void reflex(Point center) override; + + void reflex(Line axis) override; + + void scale(Point center, double coefficient) override; + + size_t verticesCount() const; + + std::vector getVertices() const; + + bool isConvex() const; + +protected: + Polygon() = default; + +protected: + std::vector points; +}; + +#endif //HEIRARCHY_POLYGON_H diff --git a/module-1/homework/Geometry/hierarchy/rectangle.cpp b/module-1/homework/Geometry/hierarchy/rectangle.cpp index e69de29b..29b2974f 100755 --- a/module-1/homework/Geometry/hierarchy/rectangle.cpp +++ b/module-1/homework/Geometry/hierarchy/rectangle.cpp @@ -0,0 +1,26 @@ +#include "rectangle.h" + +Rectangle::Rectangle(Point point1, Point point2, double attitude) { + points.push_back(point1); + points.push_back(Point(point1.x, point2.y)); + points.push_back(point2); + points.push_back(Point(point2.x, point1.y)); +} + +std::pair Rectangle::diagonals() { + return std::pair(Line(points[0], points[2]), Line(points[1], points[3])); +} + +Point Rectangle::center() { + return Point((points[0].x + points[2].x) / 2, (points[0].y + points[2].y) / 2); +} + +Rectangle::Rectangle(const Rectangle& origin) { + points = origin.points; +} + +Rectangle& Rectangle::operator=(const Rectangle& origin) { + points = origin.points; + + return *this; +} diff --git a/module-1/homework/Geometry/hierarchy/rectangle.h b/module-1/homework/Geometry/hierarchy/rectangle.h index e69de29b..477670b9 100755 --- a/module-1/homework/Geometry/hierarchy/rectangle.h +++ b/module-1/homework/Geometry/hierarchy/rectangle.h @@ -0,0 +1,24 @@ +#include "polygon.h" +#include + +#ifndef HEIRARCHY_RECTANGLE_H +#define HEIRARCHY_RECTANGLE_H + +class Rectangle : public Polygon { +public: + Rectangle(Point point1, Point point2, double attitude); + + Rectangle(const Rectangle& origin); + + Rectangle& operator=(const Rectangle& origin); + + ~Rectangle() = default; + + Point center(); + std::pair diagonals(); + +protected: + Rectangle() = default; +}; + +#endif //HEIRARCHY_RECTANGLE_H diff --git a/module-1/homework/Geometry/hierarchy/shape.h b/module-1/homework/Geometry/hierarchy/shape.h index e69de29b..42802511 100755 --- a/module-1/homework/Geometry/hierarchy/shape.h +++ b/module-1/homework/Geometry/hierarchy/shape.h @@ -0,0 +1,32 @@ +#include "point.h" +#include "line.h" +#include "mymath.h" + +#ifndef HIERARCHY_SHAPE_H +#define HIERARCHY_SHAPE_H + +class Shape { +public: + + virtual double perimeter() const = 0; + + virtual double area() const = 0; + + virtual bool operator==(const Shape& another) const = 0; + + virtual bool isCongruentTo(const Shape& another) const = 0; + + virtual bool isSimilarTo(const Shape& another) const = 0; + + virtual bool containsPoint(Point point) const = 0; + + virtual void rotate(Point center, double angle) = 0; + + virtual void reflex(Point center) = 0; + + virtual void reflex(Line axis) = 0; + + virtual void scale(Point center, double coefficient) = 0; +}; + +#endif //HIERARCHY_SHAPE_H diff --git a/module-1/homework/Geometry/hierarchy/square.cpp b/module-1/homework/Geometry/hierarchy/square.cpp index e69de29b..25c3f3f2 100755 --- a/module-1/homework/Geometry/hierarchy/square.cpp +++ b/module-1/homework/Geometry/hierarchy/square.cpp @@ -0,0 +1,29 @@ +#include"square.h" + +Square::Square(std::pair point) { + points.push_back(point.first); + points.push_back(Point(point.first.x, point.second.y)); + points.push_back(point.second); + points.push_back(Point(point.second.x, point.first.y)); +} + +Square::Square(const Square& origin) { + points = origin.points; +} + +Square& Square::operator=(const Square& origin) { + points = origin.points; + return *this; +} + +Circle Square::inscribedCircle() { + Point p_r = Point((points[0].x + points[2].x) / 2, (points[0].y + points[2].y) / 2); + double r = eucl(p_r, points[0]); + return Circle(p_r, r); +} + +Circle Square::circumscribedCircle() { + Point p_r = Point((points[0].x + points[2].x) / 2, (points[0].y + points[2].y) / 2); + double r = eucl(points[0], points[2]) / 2; + return Circle(p_r, r); +} diff --git a/module-1/homework/Geometry/hierarchy/square.h b/module-1/homework/Geometry/hierarchy/square.h index e69de29b..0d5994c6 100755 --- a/module-1/homework/Geometry/hierarchy/square.h +++ b/module-1/homework/Geometry/hierarchy/square.h @@ -0,0 +1,25 @@ +#include "rectangle.h" +#include "circle.h" +#include + +#ifndef HIERARCHY_SQUARE_H +#define HIERARCHY_SQUARE_H + +class Square : protected Rectangle { +public: + Square() = delete; + + Square(std::pair points); + + Square(const Square& origin); + + Square& operator=(const Square& origin); + + ~Square() = default; + + Circle circumscribedCircle(); + + Circle inscribedCircle(); +}; + +#endif //HIERARCHY_SQUARE_H diff --git a/module-1/homework/Geometry/hierarchy/triangle.cpp b/module-1/homework/Geometry/hierarchy/triangle.cpp index e69de29b..d9ea8831 100755 --- a/module-1/homework/Geometry/hierarchy/triangle.cpp +++ b/module-1/homework/Geometry/hierarchy/triangle.cpp @@ -0,0 +1,45 @@ +#include "triangle.h" + +Triangle::Triangle(Point x, Point y, Point z) { + points.push_back(x); + points.push_back(y); + points.push_back(z); +} + +Triangle::Triangle(const Triangle& origin) { + points = origin.points; +} + +Triangle& Triangle::operator=(const Triangle& origin) { + points = origin.points; + return *this; +} + +Circle Triangle::circumscribedCircle() { + double a = eucl(points[0], points[1]); + double b = eucl(points[1], points[2]); + double c = eucl(points[2], points[0]); + + Point center = Point((a * points[0].x + b * points[1].x + c * points[2].x) / (a + b + c), + (a * points[0].y + b * points[1].y + c * points[2].y) / (a + b + c)); + double p = (a + b + c) / 2; + double r = std::sqrt((p - a) * (p - b) * (p - c) / p); + return Circle(center, r); +} + +Circle Triangle::inscribedCircle() { + double xy_1 = std::pow(points[0].x, 2) + std::pow(points[0].y, 2); + double xy_2 = std::pow(points[1].x, 2) + std::pow(points[1].y, 2); + double xy_3 = std::pow(points[2].x, 2) + std::pow(points[2].y, 2); + double a = xy_1 * points[1].y + xy_2 * points[2].y + xy_3 * points[0].y - xy_3 * points[1].y - xy_2 * points[0].y - + xy_1 * points[2].y; + double b = points[0].x * xy_2 + points[1].x * xy_3 + points[2].x * xy_1 - xy_2 * points[2].x - xy_1 * points[1].x - + xy_3 * points[0].x; + double c = points[0].x * points[1].y + points[1].x * points[2].y + points[2].x * points[0].y - + points[2].x * points[0].y - points[0].x * points[2].y; + + Point center = Point(a / (2 * c), b / (2 * c)); + double r = eucl(center, points[0]); + + return Circle(center, r); +} diff --git a/module-1/homework/Geometry/hierarchy/triangle.h b/module-1/homework/Geometry/hierarchy/triangle.h index e69de29b..761576f9 100755 --- a/module-1/homework/Geometry/hierarchy/triangle.h +++ b/module-1/homework/Geometry/hierarchy/triangle.h @@ -0,0 +1,28 @@ +#include "circle.h" +#include "polygon.h" +#include "point.h" +#include + +#ifndef HIERARCHY_TRIANGLE_H +#define HIERARCHY_TRIANGLE_H + +class Triangle : public Polygon { +public: + Triangle() = delete; + + Triangle(Point x, Point y, Point z); + + Triangle(const Triangle& origin); + + Triangle& operator=(const Triangle& origin); + + ~Triangle() = default; + + + Circle circumscribedCircle(); + + Circle inscribedCircle(); +}; + +#endif //HIERARCHY_TRIANGLE_H +