描述
用结构体表示点的坐标(整数),根据三角形的三个顶点(假设不在一条直线上),求面积。专门定义一个函数,专门根据三个顶点求面积。然后用这个函数,求若干个三角形的面积。三角形个数不超过100个。 要求用一个结构体表示点,用一个结构体表示三角形。
输入
第一行输入一个整数(n),然后每行输入6个整数,表示三个顶点的坐标。
输出
输出n行,每行一个三角形的面积。保留3位小数。
样例
输入
2
0 0 1 0 0 1
4 4 1 0 0 1
输出
0.500
3.500
输入
10
-1721 1826 4961 -4509 -2005 -3059
-173 436 -2612 -397 -1098 -4847
-4708 -2619 2420 3715 4717 4894
447 -3276 -230 -3463 -3131 4911
665 1297 2034 4894 3701 -1191
-3681 -4670 2672 -336 140 2711
3251 1868 545 2642 -2341 -2246
-4965 -2142 3723 4741 2527 -4222
-2685 -1965 -2812 -3158 -4712 -4897
4040 3942 4263 -2354 2444 -1197
输出
17220355.000
6057356.000
3072643.000
3105842.500
7163282.000
15165639.500
7730346.000
34819238.000
1022923.500
5597206.500
#include<iostream>
#include<vector>
#include<cmath>
#include<algorithm>
#include<cstdio>
using namespace std;
#define esp 1e-6
#define PI acos(-1)
int Sign(double x){//判断x大于0,小于0,还是等于0
return fabs(x)<esp?0:x>0?1:-1;
}
struct Point{ //存点
double x,y;
Point(){
}
Point(double xx,double yy):x(xx),y(yy) { }
Point operator-(const Point & p) const {
return Point(x-p.x,y-p.y);
}
bool operator <(const Point & p) const {
if( y < p.y)
return true;
else if( y > p.y)
return false;
else
return x < p.x;
}
};
typedef Point Vector;
double Cross(const Vector & v1, const Vector & v2){//叉积
return v1.x * v2.y - v2.x * v1.y;
}
double Distance(const Point & p1,const Point & p2){//求距离
return sqrt( (p1.x - p2.x)*(p1.x - p2.x) + (p1.y-p2.y)*(p1.y-p2.y));
}
struct Comp { //用来定义极角排序规则的函数对象
Point p0; //以p0为原点进行极角排序,极角相同的,离p0近算小
Comp(const Point & p):p0(p.x,p.y) { }
bool operator ()(const Point & p1,const Point & p2) const {
int s = Sign( Cross(p1-p0,p2-p0));
if( s > 0)
return true;
else if( s < 0)
return false;
else {
if( Distance(p0,p1)<Distance(p0,p2))
return true;
else
return false;
}
}
};
int Graham(vector<Point> & points,vector<Point> & stack) {//求凸包
//points是点集合
if( points.size() < 3)
return 0; //返回凸包顶点数
stack.clear();
//先按坐标排序,最左下的放到points[0]
sort(points.begin(),points.end());
//以points[0] 为原点进行极角排序
sort(points.begin()+1,points.end(),Comp(points[0]));
stack.push_back(points[0]);
stack.push_back(points[1]);
stack.push_back(points[2]);
for(int i = 3; i< points.size(); ++i) {
while(true) {
Point p2 =* (stack.end()-1);
Point p1 = *(stack.end()-2);
if( Sign(Cross(p2-p1,points[i]-p2) <= 0))
//p2->points[i]没有向左转,就让p2出栈
stack.pop_back();
else
break;
}
stack.push_back(points[i]);
}
return stack.size();
}
int main(){
double N,L,l,n1,xx1,yy1,xx2,yy2;
int n;
Point p;
vector<Point> points;
vector<Point> stack;
while(~scanf("%d",&n)&&n!=0){
double res=0;
points.clear();
stack.clear();
for(int i=0;i<n;i++){
scanf("%lf %lf",&n1,&l);
p.x=n1,p.y=l;
points.push_back(p);
}
int size = Graham(points,stack);
for(int i=0;i<=size-1;i++)
for(int j=i+1;j<=size-1;j++)
for(int k=j+1;k<=size-1;k++)
{
xx1=stack[j].x-stack[i].x;//求面积。任取三个点找最大的
yy1=stack[j].y-stack[i].y;
xx2=stack[k].x-stack[i].x;
yy2=stack[k].y-stack[i].y;
res=max(fabs(xx1*yy2-xx2*yy1),res);
}
printf("%.2lf\n",fabs(res)/2);
}
}
可以用这个算法
#include<stdio.h>
#include<math.h>
double fun1(double x1,double y1,double x2,double y2,double x3,double y3)
{
double a,b,c,s;
a=sqrt(pow((x1-x2),2)+pow((y1-y2),2));
b=sqrt(pow((x1-x3),2)+pow((y1-y3),2));
c=sqrt(pow((x3-x2),2)+pow((y3-y2),2));
if((a+b>c)&&(a+c>b)&&(b+c>a))
{
s=(1.0/4)*sqrt((a+b+c)*(a+b-c)*(a+c-b)*(b+c-a));
return s;
}
else
{
printf("It is not a triangle!");
return -1.0;
}
}
int main()
{
double x1,y1,x2,y2,x3,y3,s;
scanf("%lf%lf%lf%lf%lf%lf",&x1,&y1,&x2,&y2,&x3,&y3);
s=fun1(x1,y1,x2,y2,x3,y3);
if(s==-1.0)
printf("It is not a triangle!");
else
printf("%lf\n",s);
return 0;
}
//原文:https://blog.csdn.net/qq_43374681/article/details/84973210