首页 > 代码库 > 【UVA】11437 Triangle Fun(简单几何)
【UVA】11437 Triangle Fun(简单几何)
先求出在A,B,C上的三等分点在,这里使用向量运算进行加减就行了。
之后通过求出的三等分点 和 顶点的连线,求出3个交点。
最后用求出的三个交点算出面积。
注意:由于可能是钝角三角形,需要求其绝对值。
14116428 | 11437 | Triangle Fun | Accepted | C++ | 0.015 | 2014-08-30 03:27:36 |
#include <iostream> #include <cstdlib> #include <cstdio> #include <string> #include <cstring> #include <cmath> #include <vector> #include <queue> #include <stack> #include <algorithm> using namespace std; const double eps = 1e-10; struct Point { double x, y; Point(double x = 0, double y = 0) : x(x), y(y) { } bool operator < (const Point& a) const { if(a.x != x) return x < a.x; return y < a.y; } }; typedef Point Vector; Vector operator + (Vector A, Vector B) { return Vector(A.x+B.x, A.y+B.y); } Vector operator - (Point A, Point B) { return Vector(A.x-B.x, A.y-B.y); } Vector operator * (Vector A, double p) { return Vector(A.x*p, A.y*p); } Vector operator / (Vector A, double p) { return Vector(A.x/p, A.y/p); } int dcmp(double x) { if(fabs(x) < eps) return 0; else return x < 0 ? -1 : 1; } bool operator == (const Point& a, const Point &b) { return dcmp(a.x-b.x) == 0 && dcmp(a.y-b.y) == 0; } double Dot(Vector A, Vector B) { return A.x*B.x + A.y*B.y; } double Length(Vector A) { return sqrt(Dot(A, A)); } double Angle(Vector A, Vector B) { return acos(Dot(A, B) / Length(A) / Length(B)); } double Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; } double Area2(Point A, Point B, Point C) { return Cross(B-A, C-A); } Vector Rotate(Vector A, double rad) { return Vector(A.x*cos(rad)-A.y*sin(rad), A.x*sin(rad) + A.y*cos(rad)); } Point GetIntersection(Point P, Vector v, Point Q, Vector w) { Vector u = P-Q; double t = Cross(w, u) / Cross(v, w); return P+v*t; } bool SegmentProperIntersection(Point a1, Point a2, Point b1, Point b2) { double c1 = Cross(a2-a1, b1-a1), c2 = Cross(a2-a1, b2-a1); double c3 = Cross(b2-b1, a1-b1), c4 = Cross(b2-b1, a2-b1); return dcmp(c1) * dcmp(c2) < 0 && dcmp(c3) * dcmp(c4) < 0; } double PolygonArea(Point* p, int n) { double area = 0; for(int i = 1; i < n-1; i++) area += Cross(p[i]-p[0], p[i+1]-p[0]); return area; } bool OnSegment(Point p, Point a1, Point a2) { return dcmp(Cross(a1-p, a2-p)) == 0 && dcmp(Dot(a1-p, a2-p)) < 0; } #define MAXD 50 int main(){ int T; Point P[MAXD]; scanf("%d",&T); while(T--){ for(int i = 1 ; i <= 3 ; i++) scanf("%lf%lf",&P[i].x,&P[i].y); P[4].x = (2 * P[2].x + P[3].x) / 3; //D P[4].y = (2 * P[2].y + P[3].y) / 3; P[5].x = (2 * P[3].x + P[1].x) / 3; //E P[5].y = (2 * P[3].y + P[1].y) / 3; P[6].x = (2 * P[1].x + P[2].x) / 3; //F P[6].y = (2 * P[1].y + P[2].y) / 3; Point a = GetIntersection(P[4],P[1] - P[4],P[5],P[2] - P[5]); //cout << a.x << " " << a.y << endl; Point b = GetIntersection(P[4],P[1] - P[4],P[6],P[3] - P[6]); //cout << b.x << " " << b.y << endl; Point c = GetIntersection(P[5],P[2] - P[5],P[6],P[3] - P[6]); //cout << c.x << " " << c.y << endl; double ans = Area2(a,b,c) / 2; ans = floor(ans + 0.5); printf("%.f\n",fabs(ans)); } return 0; }
【UVA】11437 Triangle Fun(简单几何)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。