著名的海伦公式,利用三角形的三条边进行三角形面积求解问题
struct Point{
int x;
int y;
//int z; 如果是空间中的点
};
double count_triangle_area(Point a, Point b, Point c) { double area = -1; double side[3]; side[0] = sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y, 2));
side[1] = sqrt(pow(a.x - z.x, 2) + pow(a.y - z.y, 2));
side[2] = sqrt(pow(b.x - z.x, 2) + pow(b.y - z.y, 2)); //side[0] = sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y, 2) + pow(a.z - b.z, 2)); //side[1] = sqrt(pow(a.x - c.x, 2) + pow(a.y - c.y, 2) + pow(a.z - c.z, 2)); //side[2] = sqrt(pow(c.x - b.x, 2) + pow(c.y - b.y, 2) + pow(c.z - b.z, 2));
if (side[0] + side[1] <= side[2] || side[0] + side[2] <= side[1] || side[1] + side[2] <= side[0]) return area; double p = (side[0] + side[1] + side[2]) / 2; area = sqrt(p*(p - side[0])*(p - side[1])*(p - side[2])); return area; }
时间: 2024-10-07 12:07:08