24h購物| | PChome| 登入
2013-04-25 07:59:45| 人氣1,136| 回應0 | 上一篇 | 下一篇

[UVA][數學] 1249 - Euclid

推薦 0 收藏 0 轉貼0 訂閱站台

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 GH, AH is parallel to BG)
  • The area of parallelogram ABGH is the same as the area of triangle DEF


Input 

Input consists of multiple datasets. Each dataset will consist of twelve real numbers, with no more than 3 decimal places each, on a single line. Those numbers will represent the x and y coordinates of points A through F , as follows:


xA yA xB yB xC yC xD yD xE yE xF yF


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...1000.0 inclusive.

End of the input will be a line with twelve zero values (0.0).

Output 

For each input set, print a single line with four floating point numbers. These represent points G and H, like this:


xG yG xH yH


Print all values to a precision of 3 decimal places (rounded, NOT truncated). Print a single space between numbers.

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

很簡單的推導。


#include <stdio.h>
#include <math.h>
struct Pt {
double x, y;
void scan() {
scanf("%lf %lf", &x, &y);
}
};
#define eps 1e-8
double cross(Pt o, Pt a, Pt b) {
return (a.x-o.x)*(b.y-o.y)-(a.y-o.y)*(b.x-o.x);
}
double dist(Pt a, Pt b) {
return sqrt(pow(a.x-b.x,2)+pow(a.y-b.y,2));
}
int main() {
Pt in[6];
int i, j, k;
while(1) {
int eof = 1;
for(i = 0; i < 6; i++) {
in[i].scan();
if(fabs(in[i].x) > eps || fabs(in[i].y) > eps)
eof = 0;
}
if(eof) break;
double areaDEF = fabs(cross(in[3], in[4], in[5])/2);
double areaABC = fabs(cross(in[0], in[1], in[2])/2);
double distAB = dist(in[0], in[1]);
double distAC = dist(in[0], in[2]);
double sintheta = areaABC*2/distAB/distAC;
double distAH = areaDEF/sintheta/distAB;
Pt H, G;
H.x = in[0].x + (in[2].x-in[0].x)*distAH/distAC;
H.y = in[0].y + (in[2].y-in[0].y)*distAH/distAC;
G.x = H.x + (in[1].x-in[0].x);
G.y = H.y + (in[1].y-in[0].y);
printf("%.3lf %.3lf %.3lf %.3lf\n", G.x, G.y, H.x, H.y);
}
return 0;
}
/*
0 0 5 0 0 5 0 0 0 1 1 0
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
*/
 

台長: Morris
人氣(1,136) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][凸包、窮舉] 811 - The Fortified Forest
此分類上一篇:[UVA][dp、最長共同回文] 12473 - Common Palindrome

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文