仅供参考
#include <iostream>
#include <math.h>
using namespace std;
class myPoint {
public:
myPoint(double x0 = 0.0, double y0 = 0.0) :x(x0), y(y0) {}
myPoint(myPoint &np) :x(np.x), y(np.y) {}
double GetX() { return x; }
double GetY() { return y; }
void SetX(double x0) { x = x0; }
void SetY(double y0) { x = y0; }
void SetPoint(double x0, double y0) { x = x0; y = y0; }
void SetPoint(myPoint &np) { x = np.x; y = np.y; }
double GetLength(myPoint p) {
return sqrt((x - p.x)*(x - p.x) + (y - p.y)*(y - p.y));
}
void Printit() { cout << " (" << x << "," << y << ") "; }
private:
double x, y;
};
class Triangle {
public:
double GetLength(myPoint p1, myPoint p2, myPoint p3)
{
return (p1.GetLength(p2) + p2.GetLength(p3) + p3.GetLength(p1));
}
double GetArea(myPoint p1, myPoint p2, myPoint p3)
{
double a, b, c, s, area;
a = p1.GetLength(p2);
b = p2.GetLength(p3);
c = p3.GetLength(p1);
s = 0.5*(a + b + c);
area = sqrt(s*(s - a)*(s - b)*(s - c));
return area;
}
};
int main()
{
myPoint a(0, 0), b(4, 0), c(2, 3);
Triangle t;
cout << "area=" << t.GetArea(a, b, c) << endl;
cout << "length=" << t.GetLength(a, b, c) << endl;
getchar();
return 0;
}
结果: