码迷,mamicode.com
首页 > 其他好文 > 详细

UVA - 11437 - Triangle Fun (计算几何~)

时间:2015-02-10 23:18:33      阅读:534      评论:0      收藏:0      [点我收藏+]

标签:计算几何   acm   uva   

UVA - 11437

Time Limit: 1000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu

Submit Status

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.

 

Input

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.

 

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

 

Output for Sample Input

98099

206144

 


Problemsetter: Shahriar Manzoor

Source

Root :: Prominent Problemsetters :: Shahriar Manzoor
Root :: Competitive Programming: Increasing the Lower Bound of Programming Contests (Steven & Felix Halim) :: Chapter 7. (Computational) Geometry :: Geometry Basics :: Triangles (plus Circles)
Root :: AOAPC I: Beginning Algorithm Contests -- Training Guide (Rujia Liu) :: Chapter 4. Geometry :: Basic Problems
Root :: Competitive Programming 3: The New Lower Bound of Programming Contests (Steven & Felix Halim) :: (Computational) Geometry :: Basic Geometry :: Triangles (plus Circles)





思路:先求出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 (计算几何~)

标签:计算几何   acm   uva   

原文地址:http://blog.csdn.net/u014355480/article/details/43707511

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!