UVA - 11437
Time Limit: 1000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
Description
Problem A
Triangle Fun
Input: Standard Input
Output: Standard Output
In 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 find the area of triangle PQR.
First line of the input file contains an integer N (0<N<1001) which denotes how many sets of inputs are there. Input for each set contains six floating-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.
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.
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 |
98099 206144
|
Problemsetter: Shahriar Manzoor
Source
思路:先求出D,E,F,再根据线段相交得到P,Q,R,,利用叉积求得面积即可。。
AC代码:
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <cmath> using namespace std; struct Point { double x, y; Point(double x = 0, double y = 0) : x(x) , y(y) { } }; typedef Point Vector; Vector operator + (Vector A, Vector B) { return Vector(A.x+B.x, A.y+B.y); } Vector operator - (Vector A, Vector 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); } bool operator < (const Point& a, const Point& b) { return a.x < b.x || (a.x == b.x && a.y < b.y); } const double eps = 1e-10; 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) ); } Vector Normal(Vector A) { double L = Length(A); return Vector(-A.y/L, A.x/L); } Point GetLineIntersection(Point P, Vector v, Point Q, Vector w) { Vector u = P - Q; double t = Cross(w, u) / Cross(v, w); return P + v * t; } double DistanceToLine(Point P, Point A, Point B) { Vector v1 = B-A, v2 = P - A; return fabs(Cross(v1,v2) / Length(v1)); } double DistanceToSegment(Point P, Point A, Point B) { if(A==B) return Length(P-A); Vector v1 = B - A, v2 = P - A, v3 = P - B; if(dcmp(Dot(v1, v2)) < 0) return Length(v2); else if(dcmp(Dot(v1, v3)) > 0) return Length(v3); else return fabs(Cross(v1, v2)) / Length(v1); } Point GetLineProjection(Point P, Point A, Point B) { Vector v = B - A; return A + v * ( Dot(v, P-A) / Dot(v, v) ); } bool SegmentProperIntersection(Point a1, Point a2, Point b1, Point b2) { double c1 = Cross(a2 - a1, b1 - a1), c2 = Cross(a2 - a1, b2 - a1), c3 = Cross(b2 - b1, a1 - b1), c4 = Cross(b2 - b1, a2 - b1); return dcmp(c1) * dcmp(c2) < 0 && dcmp(c3) * dcmp(c4) < 0; } bool OnSegment(Point p, Point a1, Point a2) { return dcmp(Cross(a1 - p, a2 - p)) == 0 && dcmp(Dot(a1 - p, a2 - p)) < 0; } double ConvexPolygonArea(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 / 2; } Point A, B, C, D, E, F, P, Q, R; Point get_z(Point A, Point B) { Vector v = B - A; return A + v/3; } int main() { int N; scanf("%d", &N); while(N--) { scanf("%lf %lf %lf %lf %lf %lf", &A.x, &A.y, &B.x, &B.y, &C.x, &C.y); F = get_z(A, B); D = get_z(B, C); E = get_z(C, A); //printf("%lf %lf\n%lf %lf\n%lf %lf\n", F.x, F.y, D.x, D.y, E.x, E.y); P = GetLineIntersection(A, D-A, B, E-B); Q = GetLineIntersection(C, F-C, B, E-B); R = GetLineIntersection(A, D-A, C, F-C); double ans = Area2(P, Q, R) / 2; printf("%.0lf\n", ans); } return 0; }
UVA - 11437 - Triangle Fun (计算几何~)
原文地址:http://blog.csdn.net/u014355480/article/details/43707511