Untitled
unknown
plain_text
a year ago
2.7 kB
2
Indexable
Never
#include <iostream> using namespace std; class Segment { public: // Constructors Segment(double x1, double y1, double x2, double y2) { setEndpoints(x1, y1, x2, y2); } Segment(pair<double, double> point1, pair<double, double> point2) { setEndpoints(point1.first, point1.second, point2.first, point2.second); } // Function to check if a segment lies in one quarter of the plane bool isCompletelyInOneQuarter() const { return (bothEndpointsInSameQuarter()); } // Overload the equality operator for segments bool operator==(const Segment& other) const { return (this->p1 == other.p1 && this->p2 == other.p2) || (this->p1 == other.p2 && this->p2 == other.p1); } private: std::pair<double, double> p1, p2; // Helper function to set segment endpoints void setEndpoints(double x1, double y1, double x2, double y2) { p1 = make_pair(x1, y1); p2 = make_pair(x2, y2); } // Helper function to check if both endpoints are in the same quarter bool bothEndpointsInSameQuarter() const { return (sameQuarter(p1) && sameQuarter(p2)); } // Helper function to check if a point is in the same quarter of the plane bool sameQuarter(pair<double, double> point) const { return (point.first >= 0 && point.second >= 0) || (point.first <= 0 && point.second <= 0); } }; // Test function int main() { // Create segments Segment segment1(1.0, 2.0, 3.0, 4.0); Segment segment2(-1.0, -2.0, 2.0, 2.0); Segment segment3(2.0, 3.0, 5.0, -1.0); // Test if segments lie in one quarter if (segment1.isCompletelyInOneQuarter()) { cout << "Segment 1 is in one quarter of the plane." << endl; } else { cout << "Segment 1 is not in one quarter of the plane." << endl; } if (segment2.isCompletelyInOneQuarter()) { cout << "Segment 2 is in one quarter of the plane." << endl; } else { cout << "Segment 2 is not in one quarter of the plane." << endl; } if (segment3.isCompletelyInOneQuarter()) { cout << "Segment 3 is in one quarter of the plane." << endl; } else { cout << "Segment 3 is not in one quarter of the plane." << endl; } // Test equality of segments if (segment1 == segment2) { cout << "Segment 1 is equal to Segment 2." << endl; } else { cout << "Segment 1 is not equal to Segment 2." << endl; } if (segment1 == segment3) { cout << "Segment 1 is equal to Segment 3." << endl; } else { cout << "Segment 1 is not equal to Segment 3." << endl; } return 0; }