计算坐标三角形的面积 给出三个顶点的坐标。
计算坐标三角形的面积 给出三个顶点的坐标。
#include <iostream>
#include <iomanip> //浮点用的库
#include <cmath>
using namespace std;
int main() {
double x1, y1, x2, y2, x3, y3;
cout<< "请输入三角形第一个顶角X坐标:";
cin>>x1;
cout<< "请输入三角形第一个顶角Y坐标:";
cin>>y1;
cout<< "请输入三角形第二个顶角X坐标:";
cin>>x2;
cout<< "请输入三角形第二个顶角Y坐标:";
cin>>y2;
cout<< "请输入三角形第三个顶角X坐标:";
cin>>x3;
cout<< "请输入三角形第三个顶角Y坐标:";
cin>>y3;
double area = abs((x1*(y2-y3) + x2*(y3-y1) + x3*(y1-y2)) / 2);
cout<<endl;
cout<<"这个三角形的面积=" ;
cout << fixed << setprecision(2) << area << endl;
return 0;
}
// #include <iostream>
// #include <iomanip>
// #include <cmath>
// using namespace std;
// int main() {
// double x1, y1, x2, y2, x3, y3;
// cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3;
// // 向量叉积法计算面积
// double area = abs((x2 - x1) * (y3 - y1) - (x3 - x1) * (y2 - y1)) / 2;
// cout << fixed << setprecision(2) << area;
// return 0;
// }
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main() {
double x1, y1, x2, y2, x3, y3;
cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3;
// 计算三边长度
double a = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
double b = sqrt((x3 - x2) * (x3 - x2) + (y3 - y2) * (y3 - y2));
double c = sqrt((x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3));
// 计算半周长
double s = (a + b + c) / 2;
// 海伦公式计算面积
double area = sqrt(s * (s - a) * (s - b) * (s - c));
cout << fixed << setprecision(2) << area;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int main()
{
double x1,y1,x2,y2,x3,y3;
cin>>x1>>y1>>x2>>y2>>x3>>y3;
//S=√p(p-a)(p-b)(p-c) p=(a+b+c)/2 a,b,c为边 d= √[(x2-x1)² + (y2-y1)²]
double a = sqrt( pow( (x2-x1),2) + pow( (y2-y1),2) );
double b = sqrt( pow( (x3-x2),2) + pow( (y3-y2),2) );
double c = sqrt( pow( (x1-x3),2) + pow( (y1-y3),2) );
double p = (a+b+c)/2;
double S = sqrt(p*(p-a)*(p-b)*(p-c));
cout<<fixed<<setprecision(2)<<S;
return 0;
}