1. 海伦公式
s=sqrt(p*(p-a)*(p-b)*(p-c)) 其中a, b, c分别为三角形三边长, p为半周长;
代码:
1 #include <iostream> 2 #include <stdio.h> 3 #include <math.h> 4 #define distance(a, b) sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)) 5 using namespace std; 6 7 const double res=1e-6; 8 struct node{ 9 double x, y; 10 }; 11 12 double get_area(node a, node b, node c){//海伦公式计算三角形面积 13 double ab=distance(a, b); 14 double ac=distance(a, c); 15 double bc=distance(b, c); 16 double s=(ab+ac+bc)/2; 17 double area=sqrt(s*(s-ab)*(s-ac)*(s-bc)); 18 return area; 19 }
2. 用行列式计算三角形面积:
三角形面积等于三个定点构成行列式绝对值/2
x1 y1 1
s = x2 y2 1
x3 y3 1
area = fabs(s)/2
代码:
1 struct node{ 2 double x, y; 3 }; 4 5 double get_area(node a, node b, node c){ //***用行列式计算三角形面积 6 double area=fabs(a.x*b.y+a.y*c.x+b.x*c.y-a.x*c.y-a.y*b.x-b.y*c.x); 7 return area; 8 }