UVA_11437_TriangleFun
2015-10-22 19:38
281 查看
11437 - Triangle Fun
Time limit: 1.000 secondsIn the picture below you can see a triangle ABC. Point D, E and F divides the sides BC, CA and
AB into ratio 1:2 respectively. That is CD=2BD, AE=2CE and BF=2AF. A, D; B, E and C, F are
connected. AD and BE intersects at P, BE and CF intersects at Q and CF and AD intersects at R.
So now a new triangle PQR is formed. Given triangle ABC your job is to nd the area of triangle
PQR.
Input
First line of the input le contains an integer N (0 < N < 1001) which denotes how many sets of
inputs are there. Input for each set contains six
oating-point number Ax, Ay, Bx, By, Cx, Cy.
(0 Ax; Ay; Bx; By; Cx; Cy 10000) in one line line. These six numbers denote that the coordinate of
points A, B and C are (Ax; Ay), (Bx; By) and (Cx; Cy) respectively. A, B and C will never be collinear.
Output
For each set of input produce one line of output. This one line contains an integer AREA. Here AREA
is the area of triangle PQR, rounded to the nearest integer.
Sample Input
2
3994.707 9251.677 4152.916 7157.810 5156.835 2551.972
6903.233 3540.932 5171.382 3708.015 213.959 2519.852
Sample Output
98099
206144
简单的计算几何题目
套点模板求个面积即可
#include <iostream> #include <cstdio> #include <cmath> //需要用到部分函数 using namespace std; const double PI=acos(-1.0); //π的大小 const double eps=1e-8; //允许误差 int dcmp(double x) //误差修正 { if(fabs(x)<eps)return 0; if(x>0) return 1; return -1; } inline double sqr(double x) //平方 { return (x*x); } //以上基础 //以下点类 struct Point //点类,向量类 { double x,y; Point(){} Point(double a,double b):x(a),y(b){} void input() { scanf("%lf%lf",&x,&y); } friend Point operator +(const Point &a,const Point &b) //点之和 { return Point(a.x+b.x,a.y+b.y); } friend Point operator -(const Point &a,const Point &b) //点之差 { return Point(a.x-b.x,a.y-b.y); } friend bool operator ==(const Point &a,const Point &b) //点相同 { return dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)==0; } friend Point operator *(const Point &a,const double &b) //数乘 { return Point(a.x*b,a.y*b); } friend Point operator *(const double &a,const Point &b) { return Point(a*b.x,a*b.y); } friend Point operator /(const Point &a,const double &b) //除以某数 { return Point(a.x/b,a.y/b); } double norm() //向量的模长 { return sqrt(sqr(x)+sqr(y)); } }; typedef Point Vector; //向量是点的别名 double det(const Point &a,const Point &b) //向量a,b的叉积 { //a转向b的有向面积(逆时针转为正) return a.x*b.y-a.y*b.x; } double dot(const Point &a,const Point &b) //向量a,b的点积 { return a.x*b.x+a.y*b.y; } //以上点模板 //以下直线及线段模板 struct Segment //线段类 { Point a,b; Segment(){} Segment(Point x,Point y):a(x),b(y){} Segment(double x1,double y1,double x2,double y2) { a.x=x1;a.y=y1;b.x=x2;b.y=y2; } }; typedef Segment Sline; //同时也是直线的两点表示法 bool parallel(const Sline &a,const Sline &b) //线段所在直线是否平行 { return !dcmp(det(a.a-a.b,b.a-b.b)); } //求线段所在直线交点 如果平行返回0 否则返回1 交点保存在res bool sline_make_point(const Sline &a,const Sline &b,Point &res) { if(parallel(a,b)) return 0; double s1=det(a.a-b.a,b.b-b.a); double s2=det(a.b-b.a,b.b-b.a); res=(s1*a.b-s2*a.a)/(s1-s2); return 1; } //以上线段类 //多边形类 const int MP=1000; struct Polygon { int n; //多边形的顶点数 Point a[MP]; //顶点 Polygon(){} double area() //多边形面积 { double sum=0; a =a[0]; for(int i=0;i<n;i++) sum+=det(a[i+1],a[i]); //利用叉积的有向面积性质 return sum/2.0; } }; int main() { int t; Polygon AA,aa; AA.n=3;aa.n=3; Segment l1,l2,l3; Point ab,bc,ca; double ar; scanf("%d",&t); while(t--) { for(int i=0;i<3;i++) AA.a[i].input(); ab=(AA.a[1]-AA.a[0])/3.0+AA.a[0]; bc=(AA.a[2]-AA.a[1])/3.0+AA.a[1]; ca=(AA.a[0]-AA.a[2])/3.0+AA.a[2]; l1=Segment(ab,AA.a[2]); l2=Segment(bc,AA.a[0]); l3=Segment(ca,AA.a[1]); sline_make_point(l1,l2,aa.a[0]); sline_make_point(l2,l3,aa.a[1]); sline_make_point(l1,l3,aa.a[2]); printf("%.0lf\n",fabs(aa.area())); } return 0; }
相关文章推荐
- linux route命令学习
- 《设计模式》之策略模式
- XML的dom解析详解
- LeetCode题解——Happy Number
- RSS 2.0 php生成类,快读生成需要xml文件
- SPDY
- 通过开发者工具监控事件触发
- jdk7的新特性
- (笔记)Linux下system()函数的深度理解(整理)
- hdu5505
- 关于C#中的CompareTo使用方法
- ubuntu 中用vim打开txt等文件中文显示乱码的解决方法
- iOS开发拓展篇—音效的播放
- U-Boot启动过程完全分析
- hdu5505
- LeetCode OJ:Triangle(三角形)
- 关于Android Studio导入.9图片报错问题
- POJ2245 Lotto
- ajaxForm,ajaxSubmit
- MONGO JS 脚本