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

来源:互联网 发布:7z解压缩软件 编辑:程序博客网 时间:2024/05/18 20:34

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

首先求三边边长:

    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;
}