Advertisement
paranid5

Figure

Jun 23rd, 2020
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.73 KB | None | 0 0
  1. //Figure.h
  2.  
  3. #ifndef FIGURE_FIGURE_H
  4. #define FIGURE_FIGURE_H
  5. typedef long long ll ;
  6.  
  7. class Figure
  8. {
  9. public:
  10.     virtual bool dot_in(ll, ll) = 0;
  11.     virtual bool check() = 0;
  12.     virtual ~Figure() = 0;
  13. };
  14.  
  15. class Square : public Figure {
  16. public:
  17.     bool dot_in(ll, ll) override ;
  18.    
  19.     Square(ll,ll,ll,ll,ll,ll,ll,ll);
  20.    
  21.     ~Square() override;
  22.    
  23.     void set_x1(ll);
  24.     void set_x2(ll);
  25.     void set_x3(ll);
  26.     void set_x4(ll);
  27.     void set_y1(ll);
  28.     void set_y2(ll);
  29.     void set_y3(ll);
  30.     void set_y4(ll);
  31.    
  32.     ll get_x1() const;
  33.     ll get_x2() const;
  34.     ll get_x3() const;
  35.     ll get_x4() const;
  36.     ll get_y1() const;
  37.     ll get_y2() const;
  38.     ll get_y3() const;
  39.     ll get_y4() const;
  40.    
  41.     bool check() override ;
  42.  
  43. private:
  44.     int x1;
  45.     int x2;
  46.     int x3;
  47.     int x4;
  48.     int y1;
  49.     int y2;
  50.     int y3;
  51.     int y4;
  52. };
  53.  
  54. class Circule : public Figure {
  55. public:
  56.     bool dot_in(ll, ll) override ;
  57.    
  58.     Circule(ll, ll, ll);
  59.    
  60.     ~Circule() override;
  61.    
  62.     void set_x(ll);
  63.     void set_y(ll);
  64.     void set_r(ll);
  65.    
  66.     ll get_x() const;
  67.     ll get_y() const;
  68.     ll get_r() const;
  69.    
  70.     bool check() override ;
  71.  
  72. private:
  73.     int x;
  74.     int y;
  75.     int r;
  76. };
  77.  
  78.  
  79. #endif //FIGURE_FIGURE_H
  80.  
  81.  
  82.  
  83. //Figure.cpp
  84.  
  85. #include "Figure.h"
  86. #define ABS(X) ((X) > 0) ? (X) : (-(X))
  87.  
  88. ll triangle(const ll XA, const ll YA, const ll XB, const ll YB, const ll XC, const ll YC)
  89. {
  90.     ll S = ABS((XB - XA) * (YC - YA) - (XC - XA) * (YB - YA));
  91.     return S;
  92. }
  93.  
  94. ll length_2 (const ll x1, const ll y1, const ll x2, const ll y2)
  95. {
  96.     ll x = x1 - x2;
  97.     ll y = y1 - y2;
  98.     return (x * x + y * y);
  99. }
  100.  
  101. Square::Square ( const ll x1r, const ll x2r, const ll x3r, const ll x4r, const ll y1r, const ll y2r, const ll y3r,
  102.         const ll y4r )
  103. {
  104.     x1 = x1r;
  105.     x2 = x2r;
  106.     x3 = x3r;
  107.     x4 = x4r;
  108.     y1 = y1r;
  109.     y2 = y2r;
  110.     y3 = y3r;
  111.     y4 = y4r;
  112. }
  113.  
  114. bool Square::dot_in ( const ll dotx, const ll doty )
  115. {
  116.     return triangle ( x1, y1, x2, y2, dotx, doty ) + triangle ( x2, y2, x3, y3, dotx, doty ) +
  117.             triangle ( x3, y3, x4, y4, dotx, doty ) + triangle ( x4, y4, x1, y1, dotx, doty ) ==
  118.             triangle ( x1, y1, x2, y2, x3, y3 ) + triangle ( x3, y3, x4, y4, x1, y1 );
  119. }
  120.  
  121. bool Square::check ( )
  122. {
  123.     if (length_2(x1, y1, x2, y2) != length_2(x2, y2, x3, y3)) return false;
  124.     else if (length_2(x1, y1, x2, y2) != length_2(x3, y3, x4, y4)) return false;
  125.     else return length_2 ( x1, y1, x2, y2 ) == length_2 ( x4, y4, x1, y1 );
  126. }
  127.  
  128. void Square::set_x1 ( const ll x1r )
  129. {
  130.     x1 = x1r;
  131. }
  132.  
  133. void Square::set_x2 ( const ll x2r )
  134. {
  135.     x2 = x2r;
  136. }
  137.  
  138. void Square::set_x3 ( const ll x3r )
  139. {
  140.     x3 = x3r;
  141. }
  142.  
  143. void Square::set_x4 ( const ll x4r )
  144. {
  145.     x4 = x4r;
  146. }
  147.  
  148. void Square::set_y1 ( const ll y1r )
  149. {
  150.     y1 = y1r;
  151. }
  152.  
  153. void Square::set_y2 ( const ll y2r )
  154. {
  155.     y2 = y2r;
  156. }
  157.  
  158. void Square::set_y3 ( const ll y3r )
  159. {
  160.     y3 = y3r;
  161. }
  162.  
  163. void Square::set_y4 ( const ll y4r )
  164. {
  165.     y4 = y4r;
  166. }
  167.  
  168. ll Square::get_x1 ( ) const
  169. {
  170.     const ll x1r = x1;
  171.     return x1r;
  172. }
  173.  
  174. ll Square::get_x2 ( ) const
  175. {
  176.     const ll x2r = x2;
  177.     return x2r;
  178. }
  179.  
  180. ll Square::get_x3 ( ) const
  181. {
  182.     const ll x3r = x3;
  183.     return x3r;
  184. }
  185.  
  186. ll Square::get_x4 ( ) const
  187. {
  188.     const ll x4r = x4;
  189.     return x4r;
  190. }
  191.  
  192. ll Square::get_y1 ( ) const
  193. {
  194.     const ll y1r = y1;
  195.     return y1r;
  196. }
  197.  
  198. ll Square::get_y2 ( ) const
  199. {
  200.     const ll y2r = y2;
  201.     return y2r;
  202. }
  203.  
  204. ll Square::get_y3 ( ) const
  205. {
  206.     const ll y3r = y3;
  207.     return y3r;
  208. }
  209.  
  210. ll Square::get_y4 ( ) const
  211. {
  212.     const ll y4r = y4;
  213.     return y4r;
  214. }
  215.  
  216. Square::~Square ( )
  217. {
  218.  
  219. }
  220.  
  221. Circule::Circule ( ll xr, ll yr  , ll rr )
  222. {
  223.     x = xr;
  224.     y = yr;
  225.     r = rr;
  226. }
  227.  
  228. bool Circule::dot_in ( ll dotx, ll doty )
  229. {
  230.     return ((dotx - x) * (dotx - x) + (doty - y) * (doty - y) <= r * r );
  231. }
  232.  
  233. void Circule::set_x ( const ll xr )
  234. {
  235.     x = xr;
  236. }
  237.  
  238. void Circule::set_y ( const ll yr )
  239. {
  240.     y = yr;
  241. }
  242.  
  243. void Circule::set_r ( const ll rr )
  244. {
  245.     r = rr;
  246. }
  247.  
  248. ll Circule::get_x ( ) const
  249. {
  250.     const ll xr = x;
  251.     return xr;
  252. }
  253.  
  254. ll Circule::get_y ( ) const
  255. {
  256.     const ll yr = y;
  257.     return yr;
  258. }
  259.  
  260. ll Circule::get_r ( ) const
  261. {
  262.     const ll rr = r;
  263.     return rr;
  264. }
  265.  
  266. bool Circule::check ( )
  267. {
  268.     return r > 0;
  269. }
  270.  
  271. Circule::~Circule ( )
  272. {
  273.  
  274. }
  275.  
  276. Figure::~Figure ( )
  277. {
  278.  
  279. }
  280.  
  281.  
  282.  
  283. //main.cpp
  284.  
  285. #include "Figure.h"
  286. #include <cstdio>
  287. #include <cstring>
  288. #define _CRT_SECURE_NO_WARNINGS
  289.  
  290. int main ( )
  291. {
  292.     bool correct = true;
  293.     Figure* a;
  294.     char input[100];
  295.     do
  296.     {
  297.         std::printf("Choose figure: Square or Circle.\n");
  298.         std::printf("If you need circule, print circle, else print square: ");
  299.         std::scanf("%s", input);
  300.         if(std::strcmp(input, "square") == 0)
  301.         {
  302.             bool sq = false;
  303.             ll x1 = 0, x2 = 0, x3 = 0, x4 = 0, y1 = 0, y2 = 0, y3 = 0 , y4 = 0;
  304.             do
  305.             {
  306.                 std::printf("Enter the coordinates of the points separated by a space (first x, then y): ");
  307.                 std::scanf("%lld%lld%lld%lld%lld%lld%lld%lld", &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4);
  308.                 a = new Square(x1, x2, x3, x4, y1, y2, y3, y4);
  309.                 a->check ( ) ? (sq = true) : std::printf ( "The square is set incorrectly\n" );
  310.             }
  311.             while(!sq);
  312.         }
  313.         else if(std::strcmp(input, "circle") == 0)
  314.         {
  315.             bool c = false;
  316.             ll x = 0, y =0, r = 0;
  317.             do
  318.             {
  319.                 std::printf("Enter the center coordinate separated by a space (first x, then y): ");
  320.                 std::scanf("%lld%lld", &x, &y);
  321.                 std::printf("Set the radius: ");
  322.                 std::scanf("%lld", &r);
  323.                 a = new Circule(x, y, r);
  324.                 a->check ( ) ? (c = true) : std::printf ( "The circle is set incorrectly\n" );
  325.             }
  326.             while(!c);
  327.         }
  328.         else correct = false, std::printf("Incorrect input");
  329.     }
  330.     while(!correct);
  331.     std::printf("Enter the coordinates of the point to check, separated by a space (first x, then y):");
  332.     ll dot_x = 0, dot_y = 0;
  333.     std::scanf( "%lld%lld", &dot_x, &dot_y);
  334.     std::printf ( a->dot_in ( dot_x, dot_y ) ? "Point inside" : "Point outside" );
  335.     std::puts("\nEnd of program");
  336.     delete a;
  337.     return 0;
  338. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement