与三角形相关的问题 WITH 有向面积
一、 输入三点的坐标,输出该三角形的面积
海伦公式————
//已知三点坐标求三角形面积 #include<iostream> #include<cstdio> #include<cmath> using namespace std; #define MAX 100 int main() { int x1,y1,x2,y2,x3,y3,i=0; double a,b,c,s,p,ss[MAX]; cin>>x1>>y1>>x2>>y2>>x3>>y3; while(x1 || y1 || x2 || y2 || x3 || y3) { a=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); b=sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3)); c=sqrt((x2-x3)*(x2-x3)+(y2-y3)*(y2-y3)); p=(a+b+c)*0.5; s=sqrt(p*(p-a)*(p-b)*(p-c)); ss[i]=s; i++; cin>>x1>>y1>>x2>>y2>>x3>>y3; } int n=i; for(int i=0; i<n; i++) printf("%.1f\n",ss[i]); return 0; }
海伦公式并不太好
更好的方法 —— 有向面积
#include<iostream> #include<cstdio> #include<stdlib.h> using namespace std; int main() { double x0, x1, x2, y0, y1, y2, s; cin >> x0 >> y0 >> x1 >> y1 >> x2 >> y2; while(x0 || y0 || x1 || y1 || x2 || y2) { s = (x0 * y1 + x1 * y2 + x2 * y0 - x0 * y2 - x1 * y0 - x2 * y1) / 2; if(s < 0) s = -1 * s; printf("%.1f\n", s); cin >> x0 >> y0 >> x1 >> y1 >> x2 >> y2; } return 0; }
#########################################################################
二、输入三角形三个顶点的坐标
判断该输入顺序是逆时针还是顺时针
纯解析几何题有木有!!!
//已知三点坐标判断是逆时针输入还是顺时针输入 #include<iostream> #include<cstdio> #include<cmath> using namespace std; int main() { int x1,y1,x2,y2,x3,y3,i=0; double a,b,c,s,p; cin>>x1>>y1>>x2>>y2>>x3>>y3; while(x1 || y1 || x2 || y2 || x3 || y3) { cout<<((x3-x1)*(y2-y1)/(x2-x1)+y1>y3)<<endl; cin>>x1>>y1>>x2>>y2>>x3>>y3; } return 0; }
更好的方法 —— 依然有向面积
#include<iostream> #include<cstdio> #include<stdlib.h> using namespace std; int main() { double x0, x1, x2, y0, y1, y2, s; cin >> x0 >> y0 >> x1 >> y1 >> x2 >> y2; while(x0 || y0 || x1 || y1 || x2 || y2) { s = x0 * y1 + x1 * y2 + x2 * y0 - x0 * y2 - x1 * y0 - x2 * y1; if(s < 0) //顺时针 cout << 1 << endl; else //逆时针 cout << 0 << endl; cin >> x0 >> y0 >> x1 >> y1 >> x2 >> y2; } return 0; }