Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Figure.h
- #ifndef FIGURE_FIGURE_H
- #define FIGURE_FIGURE_H
- typedef long long ll ;
- class Figure
- {
- public:
- virtual bool dot_in(ll, ll) = 0;
- virtual bool check() = 0;
- virtual ~Figure() = 0;
- };
- class Square : public Figure {
- public:
- bool dot_in(ll, ll) override ;
- Square(ll,ll,ll,ll,ll,ll,ll,ll);
- ~Square() override;
- void set_x1(ll);
- void set_x2(ll);
- void set_x3(ll);
- void set_x4(ll);
- void set_y1(ll);
- void set_y2(ll);
- void set_y3(ll);
- void set_y4(ll);
- ll get_x1() const;
- ll get_x2() const;
- ll get_x3() const;
- ll get_x4() const;
- ll get_y1() const;
- ll get_y2() const;
- ll get_y3() const;
- ll get_y4() const;
- bool check() override ;
- private:
- int x1;
- int x2;
- int x3;
- int x4;
- int y1;
- int y2;
- int y3;
- int y4;
- };
- class Circule : public Figure {
- public:
- bool dot_in(ll, ll) override ;
- Circule(ll, ll, ll);
- ~Circule() override;
- void set_x(ll);
- void set_y(ll);
- void set_r(ll);
- ll get_x() const;
- ll get_y() const;
- ll get_r() const;
- bool check() override ;
- private:
- int x;
- int y;
- int r;
- };
- #endif //FIGURE_FIGURE_H
- //Figure.cpp
- #include "Figure.h"
- #define ABS(X) ((X) > 0) ? (X) : (-(X))
- ll triangle(const ll XA, const ll YA, const ll XB, const ll YB, const ll XC, const ll YC)
- {
- ll S = ABS((XB - XA) * (YC - YA) - (XC - XA) * (YB - YA));
- return S;
- }
- ll length_2 (const ll x1, const ll y1, const ll x2, const ll y2)
- {
- ll x = x1 - x2;
- ll y = y1 - y2;
- return (x * x + y * y);
- }
- Square::Square ( const ll x1r, const ll x2r, const ll x3r, const ll x4r, const ll y1r, const ll y2r, const ll y3r,
- const ll y4r )
- {
- x1 = x1r;
- x2 = x2r;
- x3 = x3r;
- x4 = x4r;
- y1 = y1r;
- y2 = y2r;
- y3 = y3r;
- y4 = y4r;
- }
- bool Square::dot_in ( const ll dotx, const ll doty )
- {
- return triangle ( x1, y1, x2, y2, dotx, doty ) + triangle ( x2, y2, x3, y3, dotx, doty ) +
- triangle ( x3, y3, x4, y4, dotx, doty ) + triangle ( x4, y4, x1, y1, dotx, doty ) ==
- triangle ( x1, y1, x2, y2, x3, y3 ) + triangle ( x3, y3, x4, y4, x1, y1 );
- }
- bool Square::check ( )
- {
- if (length_2(x1, y1, x2, y2) != length_2(x2, y2, x3, y3)) return false;
- else if (length_2(x1, y1, x2, y2) != length_2(x3, y3, x4, y4)) return false;
- else return length_2 ( x1, y1, x2, y2 ) == length_2 ( x4, y4, x1, y1 );
- }
- void Square::set_x1 ( const ll x1r )
- {
- x1 = x1r;
- }
- void Square::set_x2 ( const ll x2r )
- {
- x2 = x2r;
- }
- void Square::set_x3 ( const ll x3r )
- {
- x3 = x3r;
- }
- void Square::set_x4 ( const ll x4r )
- {
- x4 = x4r;
- }
- void Square::set_y1 ( const ll y1r )
- {
- y1 = y1r;
- }
- void Square::set_y2 ( const ll y2r )
- {
- y2 = y2r;
- }
- void Square::set_y3 ( const ll y3r )
- {
- y3 = y3r;
- }
- void Square::set_y4 ( const ll y4r )
- {
- y4 = y4r;
- }
- ll Square::get_x1 ( ) const
- {
- const ll x1r = x1;
- return x1r;
- }
- ll Square::get_x2 ( ) const
- {
- const ll x2r = x2;
- return x2r;
- }
- ll Square::get_x3 ( ) const
- {
- const ll x3r = x3;
- return x3r;
- }
- ll Square::get_x4 ( ) const
- {
- const ll x4r = x4;
- return x4r;
- }
- ll Square::get_y1 ( ) const
- {
- const ll y1r = y1;
- return y1r;
- }
- ll Square::get_y2 ( ) const
- {
- const ll y2r = y2;
- return y2r;
- }
- ll Square::get_y3 ( ) const
- {
- const ll y3r = y3;
- return y3r;
- }
- ll Square::get_y4 ( ) const
- {
- const ll y4r = y4;
- return y4r;
- }
- Square::~Square ( )
- {
- }
- Circule::Circule ( ll xr, ll yr , ll rr )
- {
- x = xr;
- y = yr;
- r = rr;
- }
- bool Circule::dot_in ( ll dotx, ll doty )
- {
- return ((dotx - x) * (dotx - x) + (doty - y) * (doty - y) <= r * r );
- }
- void Circule::set_x ( const ll xr )
- {
- x = xr;
- }
- void Circule::set_y ( const ll yr )
- {
- y = yr;
- }
- void Circule::set_r ( const ll rr )
- {
- r = rr;
- }
- ll Circule::get_x ( ) const
- {
- const ll xr = x;
- return xr;
- }
- ll Circule::get_y ( ) const
- {
- const ll yr = y;
- return yr;
- }
- ll Circule::get_r ( ) const
- {
- const ll rr = r;
- return rr;
- }
- bool Circule::check ( )
- {
- return r > 0;
- }
- Circule::~Circule ( )
- {
- }
- Figure::~Figure ( )
- {
- }
- //main.cpp
- #include "Figure.h"
- #include <cstdio>
- #include <cstring>
- #define _CRT_SECURE_NO_WARNINGS
- int main ( )
- {
- bool correct = true;
- Figure* a;
- char input[100];
- do
- {
- std::printf("Choose figure: Square or Circle.\n");
- std::printf("If you need circule, print circle, else print square: ");
- std::scanf("%s", input);
- if(std::strcmp(input, "square") == 0)
- {
- bool sq = false;
- ll x1 = 0, x2 = 0, x3 = 0, x4 = 0, y1 = 0, y2 = 0, y3 = 0 , y4 = 0;
- do
- {
- std::printf("Enter the coordinates of the points separated by a space (first x, then y): ");
- std::scanf("%lld%lld%lld%lld%lld%lld%lld%lld", &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4);
- a = new Square(x1, x2, x3, x4, y1, y2, y3, y4);
- a->check ( ) ? (sq = true) : std::printf ( "The square is set incorrectly\n" );
- }
- while(!sq);
- }
- else if(std::strcmp(input, "circle") == 0)
- {
- bool c = false;
- ll x = 0, y =0, r = 0;
- do
- {
- std::printf("Enter the center coordinate separated by a space (first x, then y): ");
- std::scanf("%lld%lld", &x, &y);
- std::printf("Set the radius: ");
- std::scanf("%lld", &r);
- a = new Circule(x, y, r);
- a->check ( ) ? (c = true) : std::printf ( "The circle is set incorrectly\n" );
- }
- while(!c);
- }
- else correct = false, std::printf("Incorrect input");
- }
- while(!correct);
- std::printf("Enter the coordinates of the point to check, separated by a space (first x, then y):");
- ll dot_x = 0, dot_y = 0;
- std::scanf( "%lld%lld", &dot_x, &dot_y);
- std::printf ( a->dot_in ( dot_x, dot_y ) ? "Point inside" : "Point outside" );
- std::puts("\nEnd of program");
- delete a;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement