avr39ripe

cppPointFriendQuadrantDistance

Aug 4th, 2021 (edited)
567
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2.  
  3. enum Quadrants { ZeroPoint, First, Second, Third, Fourth, XPositive, XNegative, YPositive, YNegative };
  4.  
  5. class Point
  6. {
  7.     int x;
  8.     int y;
  9. public:
  10.     Point() = default;
  11.     Point(int pX, int pY) : x{ pX }, y{ pY } {}
  12.     Point& setX(int pX) { x = pX; return *this; }
  13.     Point& setY(int pY) { y = pY; return *this; }
  14.     int getX()const { return x; }
  15.     int getY()const { return y; }
  16.     void show() const
  17.     {
  18.         std::cout << '(' << x << ',' << y << ')';
  19.     }
  20.     friend double distance(const Point& p1, const Point& p2);
  21.     friend int quadrant(const Point& p)
  22.     {
  23.         if ((!p.y and !p.x)) { return Quadrants::ZeroPoint; }
  24.         if (!p.y) { return p.x > 0 ? Quadrants::XPositive : Quadrants::XNegative; }
  25.         if (!p.x) { return p.y > 0 ? Quadrants::YPositive : Quadrants::YNegative; }
  26.         if (p.y > 0)
  27.         {
  28.             return p.x > 0 ? Quadrants::First : Quadrants::Second;
  29.         }
  30.         return p.x > 0 ? Quadrants::Fourth : Quadrants::Third;
  31.     }
  32. };
  33.  
  34. double distance(const Point& p1, const Point& p2)
  35. {
  36.     auto xLength{ p2.x - p1.x };
  37.     auto yLength{ p2.y - p1.y };
  38.     return sqrt(xLength * xLength + yLength * yLength);
  39. }
  40.  
  41. void quadrantDecode(int quadrant)
  42. {
  43.     if (quadrant == Quadrants::ZeroPoint) { std::cout << "Zero point"; }
  44.     else if (quadrant == Quadrants::First) { std::cout << "First quadrant"; }
  45.     else if (quadrant == Quadrants::Second) { std::cout << "Second quadrant"; }
  46.     else if (quadrant == Quadrants::Third) { std::cout << "Third quadrant"; }
  47.     else if (quadrant == Quadrants::Fourth) { std::cout << "Fourth quadrant"; }
  48.     else if (quadrant == Quadrants::XPositive) { std::cout << "X axis positive"; }
  49.     else if (quadrant == Quadrants::XNegative) { std::cout << "X axis negative"; }
  50.     else if (quadrant == Quadrants::YPositive) { std::cout << "Y axis positive"; }
  51.     else if (quadrant == Quadrants::YNegative) { std::cout << "Y axis negative"; }
  52. }
  53.  
  54. int main()
  55. {
  56.     Point p1{ 5,5 };
  57.     Point p2{ 10,10 };
  58.  
  59.     std::cout << "Distance between p1 and p2 is: " << distance(p1, p2) << '\n';
  60.  
  61.     const int testCases{ 9 };
  62.     Point points[testCases]{
  63.         {0,0},
  64.         {1,1},
  65.         {-1,1},
  66.         {-1,-1},
  67.         {1,-1},
  68.         {1,0},
  69.         {-1,0},
  70.         {0,1},
  71.         {0,-1}
  72.     };
  73.  
  74.     for (int i{ 0 }; i < testCases; ++i)
  75.     {
  76.         points[i].show();
  77.         std::cout << ' ';
  78.         quadrantDecode(quadrant(points[i]));
  79.         std::cout << '\n';
  80.     }
  81.  
  82.     return 0;
  83. }
RAW Paste Data