Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions module-1/homework/Geometry/hierarchy/circle.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
23 changes: 23 additions & 0 deletions module-1/homework/Geometry/hierarchy/circle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <utility>

#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
141 changes: 141 additions & 0 deletions module-1/homework/Geometry/hierarchy/ellipse.cpp
Original file line number Diff line number Diff line change
@@ -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<Point, Point> 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<const Ellipse* >(&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<const Ellipse*>(&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<const Ellipse*>(&another);

if (ellipse_another) {
double k = a / ellipse_another->a;
return k == b / ellipse_another->b && k == 1;
}

return false;
}
51 changes: 51 additions & 0 deletions module-1/homework/Geometry/hierarchy/ellipse.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <utility>
#include <cmath>
#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<Point, Point> getFocuses();
double eccentricity() const;
Point center() const;

protected:
std::pair<Point, Point> focuses;
double focusSum;
double a;
double b;
double c;
};

#endif //HEIRARCHY_ELLIPSE_H
11 changes: 11 additions & 0 deletions module-1/homework/Geometry/hierarchy/line.cpp
Original file line number Diff line number Diff line change
@@ -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);
}
16 changes: 16 additions & 0 deletions module-1/homework/Geometry/hierarchy/line.h
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions module-1/homework/Geometry/hierarchy/point.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
15 changes: 15 additions & 0 deletions module-1/homework/Geometry/hierarchy/point.h
Original file line number Diff line number Diff line change
@@ -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
Loading