您的位置:首页 > 其它

由在三点坐标求三角形面积

2010-12-13 09:19 351 查看
由在三点坐标求三角形面积

首先求三边边长:

a b c

用海伦公式求面积:
三角形的面积 area=sqrt( p*(p- a) * (p - b) * (p - c) )
其中p = (a + b + c) / 2;
源码:
#include <iostream>
#include <cmath>
using namespace std;
class Point
{
float x,y;
public:
Point(float x1=0,float y1=0)//:x(x1),y(y1)
{
x=x1;
y=y1;
}
float getX()
{
return x;
}
float getY()
{
return y;
}
//friend float GetDistancebyPoint(Point Pa,Point Pb);
};

class Triangle
{
Point a;
Point b;
Point c;
public:
Triangle(Point a1,Point b1,Point c1):a(a1),b(b1),c(c1)
{}

float GetDistancebyPoint(Point Pa,Point Pb)
{
return sqrt (abs(Pa.getX() - Pb.getX())*abs(Pa.getX() - Pb.getX()) + abs(Pa.getY() - Pb.getY()) * abs(Pa.getY() - Pb.getY()));
}

float area()
{
float p = 0.0;
float a1=GetDistancebyPoint(a,b);
float b1=GetDistancebyPoint(a,c);
float c1=GetDistancebyPoint(b,c);
p = (a1 + b1 + c1) / 2;

return (sqrt(p * (p - a1) * (p - b1) * (p - c1)));

}

};

int main()
{
Point a(0,0);
Point b(1,2);
Point c(2,0);
Triangle t(a,b,c);
cout<<"三角形面积为:"<<t.area()<<endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: