Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <math.h>
- #define _USE_MATH_DEFINES
- struct Point
- {
- Point(int x, int y): x(x), y(y) {}
- void operator -=(Point p)
- {
- x -= p.x;
- y -= p.y;
- }
- double Lenght(Point p)
- {
- return sqrt((x - p.x) * (x - p.x) + (y - p.y) * (y - p.y));
- }
- int x;
- int y;
- };
- class Shape{
- public:
- virtual double Area() = 0;
- virtual bool PointInside(Point p) = 0;
- virtual bool PointOnBorder(Point p) = 0;
- };
- class Circle: public Shape{
- public:
- Circle(Point c, double r): center(c), radius(r) {}
- double Area() {
- return M_PI*radius*radius;
- }
- bool PointInside(Point p) {
- return center.Lenght(p) < radius;
- }
- bool PointOnBorder(Point p){
- return center.Lenght(p) == radius;
- }
- private:
- Point center;
- double radius;
- };
- class Rectangle: public Shape
- {
- public:
- Rectangle(Point tl, Point br):top_left(tl), bottom_right(br) {}
- double Area()
- {
- return (top_left.y - bottom_right.y) * (bottom_right.x - top_left.x);
- }
- bool PointInside(Point p)
- {
- return (top_left.x < p.x && bottom_right.x > p.x && top_left.y > p.y && bottom_right.y < p.y);
- }
- bool PointOnBorder(Point p)
- {
- return (top_left.x == p.x || bottom_right.x == p.x || top_left.y == p.y || bottom_right.y == p.y );
- }
- private:
- Point top_left;
- Point bottom_right;
- };
- class Square: public Shape
- {
- public:
- Square(Point p, int s): top_left(p), side(s){}
- double Area()
- {
- return side*side;
- }
- bool PointInside(Point p)
- {
- return (top_left.x < p.x && p.x - top_left.x < side && top_left.y > p.y && top_left.y - p.y < side);
- }
- bool PointOnSide(Point p)
- {
- return (top_left.x == p.x || top_left.y == p.y || top_left.x+side == p.x || top_left.y - side == p.y);
- }
- private:
- Point top_left;
- int side;
- };
- class Triangle: public Shape
- {
- public:
- Triangle(Point p1, Point p2, Point p3): p1(p1), p2(p2), p3(p3) {}
- double Area()
- {
- double a = p1.Lenght(p2);
- double b = p2.Lenght(p3);
- double c = p3.Lenght(p1);
- double p = (a+b+c)/2;
- return sqrt(p*(p-a)*(p-b)*(p-c));
- }
- bool PointInside(Point p)
- {
- int a = (p1.x-p.x)*(p2.y-p1.y)-(p2.x-p1.x)*(p1.y-p.y);
- int b = (p2.x-p.x)*(p3.y-p2.y)-(p3.x-p2.x)*(p2.y-p.y);
- int c = (p3.x-p.x)*(p1.y-p3.y)-(p1.x-p3.x)*(p3.y-p.y);
- return ((a > 0 && b > 0 && c > 0)||(a < 0 && b < 0 && c < 0));
- }
- bool PointOnBorder(Point p)
- {
- int a = (p1.x-p.x)*(p2.y-p1.y)-(p2.x-p1.x)*(p1.y-p.y);
- int b = (p2.x-p.x)*(p3.y-p2.y)-(p3.x-p2.x)*(p2.y-p.y);
- int c = (p3.x-p.x)*(p1.y-p3.y)-(p1.x-p3.x)*(p3.y-p.y);
- return (a == 0 || b == 0 || c == 0);
- }
- private:
- Point p1;
- Point p2;
- Point p3;
- };
- int main(){
- int a = 1, b = 7;
- Point p1(a, b);
- Point p2(4, 5);
- Rectangle r(p1, p2);
- std::cerr << r.Area() << '\n';
- std::cerr << (r.PointInside(Point(10, 2)) ? "true":"false") << '\n';
- std::cerr << (r.PointOnBorder(Point(1, 5)) ? "true":"false") << '\n';
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment