题目链接:http://poj.org/problem?id=3813
Time Limit: 1000MS Memory Limit: 65536K
Description
In one of his notebooks, Euclid gave a complex procedure for solving the following problem. With computers, perhaps there is an easier way.
In a 2D plane, consider a line segment AB, another point C which is not collinear with AB, and a triangle DEF. The goal is to find points G and H such that:
H is on the ray AC (it may be closer to A than C or further away, but angle CAB is the same as angle HAB)
ABGH is a parallelogram (AB is parallel to HG, AH is parallel to BG)
The area of parallelogram ABGH is the same as the area of triangle DEF
Input
There will be several test cases. Each test case will consist of twelve real numbers, with no more than 3 decimal places each, on a single line. Those numbers will represent, in order:
AX AY BX BY CX CY DX DY EX EY FX FY
where point A is (AX,AY), point B is (BX,BY), and so on. Points A, B and C are guaranteed to NOT be collinear. Likewise, D, E and F are also guaranteed to be non-collinear. Every number is guaranteed to be in the range from -1000.0 to 1000.0 inclusive. End of the input will be signified by a line with twelve 0.0's.
Output
For each test case, print a single line with four decimal numbers. These represent points G and H, like this:
GX GY HX HY
where point G is (GX,GY) and point H is (HX,HY). Print all values rounded to 3 decimal places of precision (NOT truncated). Print a single space between numbers. Do not print any blank lines between answers.
Sample Input
0 0 5 0 0 5 3 2 7 2 0 4
1.3 2.6 12.1 4.5 8.1 13.7 2.2 0.1 9.8 6.6 1.9 6.7
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
Sample Output
5.000 0.800 0.000 0.800
13.756 7.204 2.956 5.304
Problem solving report:
Description: 找到点G和H使得:H在射线AC上、ABGH是平行四边形,且平行四边形ABGH的面积与三角形DEF的面积相同
Problem solving: 先求出DEF的面积,就可以得到平行四边形ABGH的高,即点H到AB的距离,然后再通过求三角形ABC的面积求出点C到AB的距离,通过点H到AB的距离与点C到AB的距离就可以求得AH的距离,然后利用射线AC即可求出点H和点G。注意利用叉乘求面积的时候要带上绝对值。。。
Accepted Code:
/*
* @Author: lzyws739307453
* @Language: C++
*/
#include <math.h>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef struct point {
double x, y;
point(){}
point(double x_, double y_) : x(x_), y(y_) {}
}vect;
point A, B, C, D, E, F, G, H;
double Cross(vect a, vect b) {
return a.x * b.y - a.y * b.x;
}
vect operator - (const point a, const point b) {
return vect(a.x - b.x, a.y - b.y);
}
double Area(point a, point b, point c) {
return fabs(Cross(b - a, c - a)) / 2;
}
int main() {
while (scanf("%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf", &A.x, &A.y, &B.x, &B.y, &C.x, &C.y, &D.x, &D.y, &E.x, &E.y, &F.x, &F.y) != EOF) {
if (!A.x && !A.y && !B.x && !B.y && !C.x && !C.y && !D.x && !D.y && !E.x && !E.y)
break;
double par = Area(A, B, C) * 2;
double triangle = Area(D, E, F);
double rate = triangle / par;
H.x = A.x + rate * (C.x - A.x);
H.y = A.y + rate * (C.y - A.y);
G.x = B.x + H.x - A.x;
G.y = B.y + H.y - A.y;
printf("%.3lf %.3lf %.3lf %.3lf\n", G.x, G.y, H.x, H.y);
}
return 0;
}