#include #include using namespace std; class point { friend class triangle; friend bool point_eq(point & a, point & b); public: point(); point(double x, double y); double x() const; double y() const; double distance(point & p) const; private: double my_x; double my_y; }; point::point() :my_x(0), my_y(0) { } point::point(double x, double y) :my_x(x), my_y(y) { } double point::x() const { return my_x; } double point::y() const { return my_y; } double point::distance(point & p) const { return sqrt(pow(my_x-p.my_x,2)+pow(my_y-p.my_y,2)); } bool point_eq(point & a, point & b) { return ((a.my_x == b.my_x) && (a.my_y == b.my_y)); } bool operator == (point & a, point & b) { return (a.x() == b.x() && a.y() == b.y()); } bool operator != (point & a, point & b) { return (a.x() != b.x() || a.y() != b.y()); } class triangle { public: triangle(); triangle(point a, point b, point c); point p1(); point p2(); point p3(); double area(); point center(); void shift(point v); private: point my_p1; point my_p2; point my_p3; }; triangle::triangle() { my_p1 = point(0,0); my_p2 = point(0,0); my_p3 = point(0,0); } triangle::triangle(point a, point b, point c) { my_p1 = a; my_p2 = b; my_p3 = c; } point triangle::p1() { return my_p1; } point triangle::p2() { return my_p2; } point triangle::p3() { return my_p3; } double triangle::area() { double a,b,c,s,Area; a = my_p1.distance(my_p2); b = my_p2.distance(my_p3); c = my_p3.distance(my_p1); s = (a+b+c)/2; Area = sqrt(s*(s-a)*(s-b)*(s-c)); return Area; } point triangle::center() { double x,y; x = (my_p1.my_x+my_p2.my_x+my_p3.my_x)/3.0; y = (my_p1.my_y+my_p2.my_y+my_p3.my_y)/3.0; return point(x,y); } void triangle::shift(point v) { my_p1.my_x += v.x(); my_p1.my_y += v.y(); my_p2.my_x += v.x(); my_p2.my_y += v.y(); my_p3.my_x += v.x(); my_p3.my_y += v.y(); } void main() { point a, b(3,4),c(3,0),v(1,1); if (a == b || a == c || b == c) { cout << "The three points can't form a triangle!!\n"; return; } cout << "The lengthes of the three sides of the triangle are: " << b.distance(a) << ", " << b.distance(c) << ", " << a.distance(c) << ".\n\n"; triangle t(a,b,c); cout << "a:(" << a.x() << "," << a.y() << ") "; cout << "b:(" << b.x() << "," << b.y() << ") "; cout << "c:(" << c.x() << "," << c.y() << ")\n"; cout << "The center of triangle is (" << t.center().x() << "," << t.center().y() << ").\n"; cout << "The area of the triangle is " << t.area() << endl << endl; if (v != point()) { t.shift(v); cout << "After shift" << endl; cout << "a:(" << t.p1().x() << "," << t.p1().y() << ") "; cout << "b:(" << t.p2().x() << "," << t.p2().y() << ") "; cout << "c:(" << t.p3().x() << "," << t.p3().y() << ")\n"; cout << "The center of triangle is (" << t.center().x() << "," << t.center().y() << ").\n"; cout << "The area of the triangle is " << t.area() << endl << endl; } }