From 110481bee48bdb18f3d635cbc507aac1acc7c694 Mon Sep 17 00:00:00 2001 From: angeld55 Date: Tue, 24 May 2022 20:10:50 +0300 Subject: added shapes collection --- Sem. 12/Example_polymorphism/Circle.cpp | 7 ++++++- Sem. 12/Example_polymorphism/Circle.h | 1 + Sem. 12/Example_polymorphism/Rectangle.cpp | 9 ++++++++- Sem. 12/Example_polymorphism/Rectangle.h | 2 ++ Sem. 12/Example_polymorphism/Shape.h | 2 ++ Sem. 12/Example_polymorphism/Triangle.cpp | 12 ++++++++++++ Sem. 12/Example_polymorphism/Triangle.h | 2 ++ Sem. 12/Example_polymorphism/main.cpp | 27 ++++++++++++++++++++++++++- 8 files changed, 59 insertions(+), 3 deletions(-) (limited to 'Sem. 12/Example_polymorphism') diff --git a/Sem. 12/Example_polymorphism/Circle.cpp b/Sem. 12/Example_polymorphism/Circle.cpp index 8226582..c0368a0 100644 --- a/Sem. 12/Example_polymorphism/Circle.cpp +++ b/Sem. 12/Example_polymorphism/Circle.cpp @@ -13,4 +13,9 @@ double Circle::getArea() const double Circle::getPer() const { return 2 * PI * radius; -} \ No newline at end of file +} +bool Circle::isPointIn(int x, int y) const +{ + Shape::point p(x, y); + return p.getDist(getPointAtIndex(0)) <= radius; +} diff --git a/Sem. 12/Example_polymorphism/Circle.h b/Sem. 12/Example_polymorphism/Circle.h index 6449fc1..bf8a692 100644 --- a/Sem. 12/Example_polymorphism/Circle.h +++ b/Sem. 12/Example_polymorphism/Circle.h @@ -10,5 +10,6 @@ public: double getArea() const override; double getPer() const override; + bool isPointIn(int x, int y) const override; }; \ No newline at end of file diff --git a/Sem. 12/Example_polymorphism/Rectangle.cpp b/Sem. 12/Example_polymorphism/Rectangle.cpp index 3fa1026..3797b41 100644 --- a/Sem. 12/Example_polymorphism/Rectangle.cpp +++ b/Sem. 12/Example_polymorphism/Rectangle.cpp @@ -23,4 +23,11 @@ double Rectangle::getPer() const Shape::point p3 = getPointAtIndex(3); return 2*(p0.getDist(p1) + p0.getDist(p3)); -} \ No newline at end of file +} + +bool Rectangle::isPointIn(int x, int y) const +{ + Shape::point p(x, y); + return p.x >= getPointAtIndex(0).x && p.y >= getPointAtIndex(1).x && + p.y <= getPointAtIndex(0).y && p.y >= getPointAtIndex(2).y; +} diff --git a/Sem. 12/Example_polymorphism/Rectangle.h b/Sem. 12/Example_polymorphism/Rectangle.h index ccdad8e..b689637 100644 --- a/Sem. 12/Example_polymorphism/Rectangle.h +++ b/Sem. 12/Example_polymorphism/Rectangle.h @@ -7,4 +7,6 @@ public: Rectangle(int x1, int y1, int x3, int y3); double getArea() const override; double getPer() const override; + bool isPointIn(int x, int y) const override; + }; \ No newline at end of file diff --git a/Sem. 12/Example_polymorphism/Shape.h b/Sem. 12/Example_polymorphism/Shape.h index 62f5c41..6e189d7 100644 --- a/Sem. 12/Example_polymorphism/Shape.h +++ b/Sem. 12/Example_polymorphism/Shape.h @@ -38,4 +38,6 @@ public: virtual double getArea() const = 0; //pure virtual virtual double getPer() const = 0; // pure virtual + virtual bool isPointIn(int x, int y) const = 0; + }; \ No newline at end of file diff --git a/Sem. 12/Example_polymorphism/Triangle.cpp b/Sem. 12/Example_polymorphism/Triangle.cpp index 7de831c..4463ccc 100644 --- a/Sem. 12/Example_polymorphism/Triangle.cpp +++ b/Sem. 12/Example_polymorphism/Triangle.cpp @@ -1,4 +1,5 @@ #include "Triangle.h" +#include Triangle::Triangle(int x1, int y1, int x2, int y2, int x3, int y3) : Shape(3) { @@ -21,4 +22,15 @@ double Triangle::getPer() const Shape::point p3 = getPointAtIndex(2); return p1.getDist(p2) + p2.getDist(p3) + p3.getDist(p1); +} + +bool Triangle::isPointIn(int x, int y) const +{ + Shape::point p(x, y); + Triangle t1(getPointAtIndex(0).x, getPointAtIndex(0).y, getPointAtIndex(1).x, getPointAtIndex(1).y, p.x, p.y); + Triangle t2(getPointAtIndex(2).x, getPointAtIndex(2).y, getPointAtIndex(1).x, getPointAtIndex(1).y, p.x, p.y); + Triangle t3(getPointAtIndex(2).x, getPointAtIndex(2).y, getPointAtIndex(0).x, getPointAtIndex(0).y, p.x, p.y); + + return abs(t1.getArea() + t2.getArea() + t3.getArea() - getArea()) <= std::numeric_limits::epsilon(); + } \ No newline at end of file diff --git a/Sem. 12/Example_polymorphism/Triangle.h b/Sem. 12/Example_polymorphism/Triangle.h index c157de8..3f7f8cb 100644 --- a/Sem. 12/Example_polymorphism/Triangle.h +++ b/Sem. 12/Example_polymorphism/Triangle.h @@ -9,4 +9,6 @@ public: Triangle(int x1, int y1, int x2, int y2, int x3, int y3); double getArea() const override; double getPer() const override; + bool isPointIn(int x, int y) const override; + }; \ No newline at end of file diff --git a/Sem. 12/Example_polymorphism/main.cpp b/Sem. 12/Example_polymorphism/main.cpp index 89a5082..5d7fc69 100644 --- a/Sem. 12/Example_polymorphism/main.cpp +++ b/Sem. 12/Example_polymorphism/main.cpp @@ -17,12 +17,33 @@ void printPers(const Shape* const * shapes, size_t shapesCount) cout << shapes[i]->getPer() << endl; } +void checkPointIn(const Shape* const* shapes, size_t shapesCount, int x, int y) +{ + for (int i = 0; i < shapesCount; i++) + cout << shapes[i]->isPointIn(x,y) << endl; +} + void freeCollection(Shape** shapes, size_t shapesCount) { for (int i = 0; i < shapesCount; i++) delete shapes[i]; delete[] shapes; } + +struct A +{ + virtual void f() const + { + ; + } +}; +struct B : public A +{ + void f() const final + { + + } +}; int main() { Shape** arr = new Shape*[4]; @@ -33,9 +54,13 @@ int main() arr[3] = new Triangle(1, 1, 2, 2, 3, 3); printAreas(arr, 4); + std::cout << endl; printPers(arr, 4); + std::cout << endl; + + checkPointIn(arr, 4, 3, 3); + freeCollection(arr, 4); - } \ No newline at end of file -- cgit v1.3.1-23-gd82d