loulett

Shapes

May 18th, 2015
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.24 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3.  
  4. #define _USE_MATH_DEFINES
  5.  
  6. struct Point
  7. {
  8.     Point(int x, int y): x(x), y(y) {}
  9.  
  10.     void operator -=(Point p)
  11.     {
  12.         x -= p.x;
  13.         y -= p.y;
  14.     }
  15.  
  16.     double Lenght(Point p)
  17.     {
  18.         return sqrt((x - p.x) * (x - p.x) + (y - p.y) * (y - p.y));
  19.     }
  20.  
  21.     int x;
  22.     int y;
  23. };
  24.  
  25. class Shape{
  26. public:
  27.     virtual double Area() = 0;
  28.     virtual bool PointInside(Point p) = 0;
  29.     virtual bool PointOnBorder(Point p) = 0;
  30. };
  31.  
  32. class Circle: public Shape{
  33. public:
  34.     Circle(Point c, double r): center(c), radius(r) {}
  35.  
  36.     double Area() {
  37.         return M_PI*radius*radius;
  38.     }
  39.  
  40.     bool PointInside(Point p) {
  41.         return center.Lenght(p) < radius;
  42.     }
  43.  
  44.     bool PointOnBorder(Point p){
  45.         return center.Lenght(p) == radius;
  46.     }
  47.  
  48. private:
  49.     Point center;
  50.     double radius;
  51. };
  52.  
  53. class Rectangle: public Shape
  54. {
  55. public:
  56.     Rectangle(Point tl, Point br):top_left(tl), bottom_right(br) {}
  57.  
  58.     double Area()
  59.     {
  60.         return (top_left.y - bottom_right.y) * (bottom_right.x - top_left.x);
  61.     }
  62.  
  63.     bool PointInside(Point p)
  64.     {
  65.         return (top_left.x < p.x && bottom_right.x > p.x && top_left.y > p.y && bottom_right.y < p.y);
  66.     }
  67.  
  68.     bool PointOnBorder(Point p)
  69.     {
  70.         return (top_left.x == p.x || bottom_right.x == p.x || top_left.y == p.y || bottom_right.y == p.y );
  71.     }
  72.  
  73. private:
  74.     Point top_left;
  75.     Point bottom_right;
  76. };
  77.  
  78. class Square: public Shape
  79. {
  80. public:
  81.     Square(Point p, int s): top_left(p), side(s){}
  82.  
  83.     double Area()
  84.     {
  85.         return side*side;
  86.     }
  87.  
  88.     bool PointInside(Point p)
  89.     {
  90.         return (top_left.x < p.x && p.x - top_left.x < side && top_left.y > p.y && top_left.y - p.y < side);
  91.     }
  92.  
  93.     bool PointOnSide(Point p)
  94.     {
  95.         return (top_left.x == p.x || top_left.y == p.y || top_left.x+side == p.x || top_left.y - side == p.y);
  96.     }
  97.  
  98. private:
  99.     Point top_left;
  100.     int side;
  101. };
  102.  
  103. class Triangle: public Shape
  104. {
  105. public:
  106.     Triangle(Point p1, Point p2, Point p3): p1(p1), p2(p2), p3(p3) {}
  107.  
  108.     double Area()
  109.     {
  110.         double a = p1.Lenght(p2);
  111.         double b = p2.Lenght(p3);
  112.         double c = p3.Lenght(p1);
  113.         double p = (a+b+c)/2;
  114.         return sqrt(p*(p-a)*(p-b)*(p-c));
  115.     }
  116.  
  117.     bool PointInside(Point p)
  118.     {
  119.         int a = (p1.x-p.x)*(p2.y-p1.y)-(p2.x-p1.x)*(p1.y-p.y);
  120.         int b = (p2.x-p.x)*(p3.y-p2.y)-(p3.x-p2.x)*(p2.y-p.y);
  121.         int c = (p3.x-p.x)*(p1.y-p3.y)-(p1.x-p3.x)*(p3.y-p.y);
  122.         return ((a > 0 && b > 0 && c > 0)||(a < 0 && b < 0 && c < 0));
  123.     }
  124.  
  125.     bool PointOnBorder(Point p)
  126.     {
  127.         int a = (p1.x-p.x)*(p2.y-p1.y)-(p2.x-p1.x)*(p1.y-p.y);
  128.         int b = (p2.x-p.x)*(p3.y-p2.y)-(p3.x-p2.x)*(p2.y-p.y);
  129.         int c = (p3.x-p.x)*(p1.y-p3.y)-(p1.x-p3.x)*(p3.y-p.y);
  130.         return (a == 0 || b == 0 || c == 0);
  131.     }
  132.  
  133. private:
  134.     Point p1;
  135.     Point p2;
  136.     Point p3;
  137. };
  138.  
  139.  
  140. int main(){
  141.     int a = 1, b = 7;
  142.     Point p1(a, b);
  143.     Point p2(4, 5);
  144.     Rectangle r(p1, p2);
  145.     std::cerr << r.Area() << '\n';
  146.     std::cerr << (r.PointInside(Point(10, 2)) ? "true":"false") << '\n';
  147.     std::cerr << (r.PointOnBorder(Point(1, 5)) ? "true":"false") << '\n';
  148.  
  149.     return 0;
  150. }
Advertisement
Add Comment
Please, Sign In to add comment