diff options
19 files changed, 458 insertions, 3 deletions
| -rw-r--r-- | Sem. 12/Example_polymorphism/Circle.cpp | 7 | ||||
| -rw-r--r-- | Sem. 12/Example_polymorphism/Circle.h | 1 | ||||
| -rw-r--r-- | Sem. 12/Example_polymorphism/Rectangle.cpp | 9 | ||||
| -rw-r--r-- | Sem. 12/Example_polymorphism/Rectangle.h | 2 | ||||
| -rw-r--r-- | Sem. 12/Example_polymorphism/Shape.h | 2 | ||||
| -rw-r--r-- | Sem. 12/Example_polymorphism/Triangle.cpp | 12 | ||||
| -rw-r--r-- | Sem. 12/Example_polymorphism/Triangle.h | 2 | ||||
| -rw-r--r-- | Sem. 12/Example_polymorphism/main.cpp | 27 | ||||
| -rw-r--r-- | Sem. 13/ShapeCollection/Circle.cpp | 26 | ||||
| -rw-r--r-- | Sem. 13/ShapeCollection/Circle.h | 15 | ||||
| -rw-r--r-- | Sem. 13/ShapeCollection/Rectangle.cpp | 38 | ||||
| -rw-r--r-- | Sem. 13/ShapeCollection/Rectangle.h | 13 | ||||
| -rw-r--r-- | Sem. 13/ShapeCollection/Shape.cpp | 54 | ||||
| -rw-r--r-- | Sem. 13/ShapeCollection/Shape.h | 44 | ||||
| -rw-r--r-- | Sem. 13/ShapeCollection/ShapeCollection.cpp | 102 | ||||
| -rw-r--r-- | Sem. 13/ShapeCollection/ShapeCollection.h | 32 | ||||
| -rw-r--r-- | Sem. 13/ShapeCollection/Triangle.cpp | 42 | ||||
| -rw-r--r-- | Sem. 13/ShapeCollection/Triangle.h | 15 | ||||
| -rw-r--r-- | Sem. 13/ShapeCollection/main.cpp | 18 |
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 <limits> 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<double>::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 diff --git a/Sem. 13/ShapeCollection/Circle.cpp b/Sem. 13/ShapeCollection/Circle.cpp new file mode 100644 index 0000000..67762d8 --- /dev/null +++ b/Sem. 13/ShapeCollection/Circle.cpp @@ -0,0 +1,26 @@ +#include "Circle.h" + +const double PI = 3.1415; +Circle::Circle(int x, int y, double radius) : Shape(1), radius(radius) +{ + setPoint(0, x, y); +} + +double Circle::getArea() const +{ + return PI * radius * radius; +} +double Circle::getPer() const +{ + return 2 * PI * radius; +} +bool Circle::isPointIn(int x, int y) const +{ + Shape::point p(x, y); + return p.getDist(getPointAtIndex(0)) <= radius; +} +Shape* Circle::clone() const +{ + Shape* copy = new Circle(*this); + return copy; +}
\ No newline at end of file diff --git a/Sem. 13/ShapeCollection/Circle.h b/Sem. 13/ShapeCollection/Circle.h new file mode 100644 index 0000000..fbce695 --- /dev/null +++ b/Sem. 13/ShapeCollection/Circle.h @@ -0,0 +1,15 @@ +#pragma once +#include "Shape.h" + +class Circle : public Shape +{ + double radius; + +public: + Circle(int x, int y, double radius); + + double getArea() const override; + double getPer() const override; + bool isPointIn(int x, int y) const override; + Shape* clone() const override; +};
\ No newline at end of file diff --git a/Sem. 13/ShapeCollection/Rectangle.cpp b/Sem. 13/ShapeCollection/Rectangle.cpp new file mode 100644 index 0000000..448dcc7 --- /dev/null +++ b/Sem. 13/ShapeCollection/Rectangle.cpp @@ -0,0 +1,38 @@ +#include "Rectangle.h" + +Rectangle::Rectangle(int x1, int y1, int x3, int y3) : Shape(4) +{ + setPoint(0, x1, y1); + setPoint(1, x1, y3); + setPoint(2, x3, y3); + setPoint(3, x3, y1); +} +double Rectangle::getArea() const +{ + Shape::point p0 = getPointAtIndex(0); + Shape::point p1 = getPointAtIndex(1); + Shape::point p3 = getPointAtIndex(3); + + return p0.getDist(p1) * p0.getDist(p3); + +} +double Rectangle::getPer() const +{ + Shape::point p0 = getPointAtIndex(0); + Shape::point p1 = getPointAtIndex(1); + Shape::point p3 = getPointAtIndex(3); + + return 2*(p0.getDist(p1) + p0.getDist(p3)); +} + +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; +} +Shape* Rectangle::clone() const +{ + Shape* copy = new Rectangle(*this); + return copy; +} diff --git a/Sem. 13/ShapeCollection/Rectangle.h b/Sem. 13/ShapeCollection/Rectangle.h new file mode 100644 index 0000000..dedb22f --- /dev/null +++ b/Sem. 13/ShapeCollection/Rectangle.h @@ -0,0 +1,13 @@ +#pragma once +#include "Shape.h" + +class Rectangle : public Shape +{ +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; + Shape* clone() const override; + +};
\ No newline at end of file diff --git a/Sem. 13/ShapeCollection/Shape.cpp b/Sem. 13/ShapeCollection/Shape.cpp new file mode 100644 index 0000000..468c621 --- /dev/null +++ b/Sem. 13/ShapeCollection/Shape.cpp @@ -0,0 +1,54 @@ +#include "Shape.h" + +Shape::Shape(size_t pointsCount) : pointsCount(pointsCount) +{ + points = new point[pointsCount]; // [0,0], [0,0].... +} + +void Shape::copyFrom(const Shape& other) +{ + points = new point[other.pointsCount]; + + for (int i = 0; i < other.pointsCount; i++) + points[i] = other.points[i]; + + pointsCount = other.pointsCount; +} +void Shape::free() +{ + delete[] points; +} + +Shape::Shape(const Shape& other) +{ + copyFrom(other); +} +Shape& Shape::operator= (const Shape& other) +{ + if (this != &other) + { + free(); + copyFrom(other); + } + return *this; +} +Shape::~Shape() +{ + free(); +} + +const Shape::point& Shape::getPointAtIndex(size_t index) const +{ + if (index >= pointsCount) + throw std::exception("Invalid point index!"); + + return points[index]; +} + +void Shape::setPoint(size_t pointIndex, int x, int y) +{ + if (pointIndex >= pointsCount) + throw std::exception("Invalid point index!"); + + points[pointIndex] = point(x, y); +} diff --git a/Sem. 13/ShapeCollection/Shape.h b/Sem. 13/ShapeCollection/Shape.h new file mode 100644 index 0000000..143acc6 --- /dev/null +++ b/Sem. 13/ShapeCollection/Shape.h @@ -0,0 +1,44 @@ +#pragma once +#include <iostream> + +class Shape // Abstract class - no instances of Shape are allowed! +{ +protected: + struct point + { + point() :x(0), y(0){} + point(int x, int y) :x(x), y(y){} + int x; + int y; + double getDist(const point& other) const + { + int dx = x - other.x; + int dy = y - other.y; + + return sqrt(dx*dx + dy*dy); + } + }; + const point& getPointAtIndex(size_t index) const; +private: + point* points; + size_t pointsCount; + + void copyFrom(const Shape& other); + void free(); + + +public: + Shape(size_t pointsCount); + + Shape(const Shape& other); + Shape& operator=(const Shape& other); + virtual ~Shape(); //!!!!!! + + void setPoint(size_t pointIndex, int x, int y); + + virtual double getArea() const = 0; + virtual double getPer() const = 0; + virtual bool isPointIn(int x, int y) const = 0; + virtual Shape* clone() const = 0; + +};
\ No newline at end of file diff --git a/Sem. 13/ShapeCollection/ShapeCollection.cpp b/Sem. 13/ShapeCollection/ShapeCollection.cpp new file mode 100644 index 0000000..babe36d --- /dev/null +++ b/Sem. 13/ShapeCollection/ShapeCollection.cpp @@ -0,0 +1,102 @@ +#include "ShapeCollection.h" +#include <stdexcept> // std::out_of_range +#include "Rectangle.h" +#include "Circle.h" + +#include "Triangle.h" + +void ShapeCollection::free() +{ + for (size_t i = 0; i < shapesCount; i++) + delete shapes[i]; //не се инт. какъв обект е. (вирт дестр) + delete[] shapes; +} +void ShapeCollection::copyFrom(const ShapeCollection& other) +{ + shapes = new Shape* [other.shapesCount]; + shapesCount = other.shapesCount; + capacity = other.capacity; + + for (size_t i = 0; i < shapesCount; i++) + { + shapes[i] = other.shapes[i]->clone(); + } +} + +void ShapeCollection::resize() +{ + Shape** newCollection = new Shape * [capacity *= 2]; + for (size_t i = 0; i < shapesCount; i++) + newCollection[i] = shapes[i]; // !!не правим клониране + delete[] shapes; + shapes = newCollection; +} + +ShapeCollection::ShapeCollection() +{ + capacity = 8; + shapesCount = 0; + shapes = new Shape * [capacity]; +} + +ShapeCollection::ShapeCollection(const ShapeCollection& other) +{ + copyFrom(other); +} +ShapeCollection& ShapeCollection::operator=(const ShapeCollection& other) +{ + if (this != &other) + { + free(); + copyFrom(other); + } + return *this; +} +ShapeCollection::~ShapeCollection() +{ + free(); +} + +void ShapeCollection::addShape(Shape* shape) +{ + if (shapesCount == capacity) + resize(); + shapes[shapesCount++] = shape; //we dont make a copy here (it's a private function, called from creation funcions) +} + +void ShapeCollection::addRectangle(int x1, int y1, int x3, int y3) +{ + Rectangle* rect = new Rectangle(x1, y1, x3,y3); + addShape(rect); +} +void ShapeCollection::addCircle(int x1, int y1, int r) +{ + Circle* circlce = new Circle(x1, y1, r); + addShape(circlce); + +} + +void ShapeCollection::addTriangle(int x1, int y1, int x2, int y2, int x3, int y3) +{ + Triangle* tr = new Triangle(x1, y1,x2,y2, x3, y3); + addShape(tr); +} + +double ShapeCollection::getPerOfFigureByIndex(size_t ind) const +{ + if (ind >= shapesCount) + throw std::out_of_range("Out of range in shapes collection"); + return shapes[ind]->getPer(); +} +double ShapeCollection::getAreaOfFigureByIndex(size_t ind) const +{ + if (ind >= shapesCount) + throw std::out_of_range("Out of range in shapes collection"); + return shapes[ind]->getArea(); +} +double ShapeCollection::getIfPointInShapeByIndex(size_t ind, int x, int y) const +{ + if (ind >= shapesCount) + throw std::out_of_range("Out of range in shapes collection"); + return shapes[ind]->isPointIn(x, y); +}
\ No newline at end of file diff --git a/Sem. 13/ShapeCollection/ShapeCollection.h b/Sem. 13/ShapeCollection/ShapeCollection.h new file mode 100644 index 0000000..4adaca7 --- /dev/null +++ b/Sem. 13/ShapeCollection/ShapeCollection.h @@ -0,0 +1,32 @@ +#pragma once +#include "Shape.h" + +class ShapeCollection +{ + Shape** shapes; + size_t shapesCount; + size_t capacity; + + void free(); + void copyFrom(const ShapeCollection& other); + void resize(); + + void addShape(Shape* shape); + +public: + ShapeCollection(); + ShapeCollection(const ShapeCollection& other); + ShapeCollection& operator=(const ShapeCollection& other); + ~ShapeCollection(); + + + void addRectangle(int x1, int y1, int x3, int y3); + void addCircle(int x1, int y1, int r); + + void addTriangle(int x1, int y1, int x2, int y2, int x3, int y3); + + double getPerOfFigureByIndex(size_t ind) const; + double getAreaOfFigureByIndex(size_t ind) const; + double getIfPointInShapeByIndex(size_t ind, int x,int y) const; + +};
\ No newline at end of file diff --git a/Sem. 13/ShapeCollection/Triangle.cpp b/Sem. 13/ShapeCollection/Triangle.cpp new file mode 100644 index 0000000..350b321 --- /dev/null +++ b/Sem. 13/ShapeCollection/Triangle.cpp @@ -0,0 +1,42 @@ +#include "Triangle.h" +#include <limits> + +Triangle::Triangle(int x1, int y1, int x2, int y2, int x3, int y3) : Shape(3) +{ + setPoint(0, x1, y1); + setPoint(1, x2, y2); + setPoint(2, x3, y3); +} +double Triangle::getArea() const +{ + Shape::point p1 = getPointAtIndex(0); + Shape::point p2 = getPointAtIndex(1); + Shape::point p3 = getPointAtIndex(2); + + return abs(p1.x*p2.y + p2.x*p3.y + p3.x*p1.y - p1.y * p2.x - p2.y*p3.x - p3.y*p1.x) / 2.00; +} +double Triangle::getPer() const +{ + Shape::point p1 = getPointAtIndex(0); + Shape::point p2 = getPointAtIndex(1); + 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<double>::epsilon(); + +} + +Shape* Triangle::clone() const +{ + Shape* copy = new Triangle(*this); + return copy; +} diff --git a/Sem. 13/ShapeCollection/Triangle.h b/Sem. 13/ShapeCollection/Triangle.h new file mode 100644 index 0000000..39434e8 --- /dev/null +++ b/Sem. 13/ShapeCollection/Triangle.h @@ -0,0 +1,15 @@ +#pragma once + +#include "Shape.h" + +class Triangle : public Shape +{ + +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; + Shape* clone() const override; + +};
\ No newline at end of file diff --git a/Sem. 13/ShapeCollection/main.cpp b/Sem. 13/ShapeCollection/main.cpp new file mode 100644 index 0000000..4b6e784 --- /dev/null +++ b/Sem. 13/ShapeCollection/main.cpp @@ -0,0 +1,18 @@ +#include <iostream> +#include "ShapeCollection.h" +using namespace std; + +int main() +{ + ShapeCollection s; + + s.addCircle(3, 4, 1); + s.addTriangle(1, 2, 9, 4, 0, 0); + s.addRectangle(1, 1, 9, 9); + + std::cout << s.getIfPointInShapeByIndex(0, 3, 4.5)<< std::endl; + std::cout << s.getAreaOfFigureByIndex(2) << std::endl; + + + +}
\ No newline at end of file |
