山东省第四届ACM程序设计竞赛A题:Rescue The Princess(数学+计算几何)
Rescue The Princess
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 412 Solved: 168
[Submit][Status][Web Board]
Description
Several
days ago, a beast caught a beautiful princess and the princess was put
in prison. To rescue the princess, a prince who wanted to marry the
princess set out immediately. Yet, the beast set a maze. Only if the
prince find out the maze’s exit can he save the princess.
Now,
here comes the problem. The maze is a dimensional plane. The beast is
smart, and he hidden the princess snugly. He marked two coordinates of
an equilateral triangle in the maze. The two marked coordinates are
A(x1,y1) and B(x2,y2). The third coordinate C(x3,y3) is the maze’s exit.
If the prince can find out the exit, he can save the princess. After
the prince comes into the maze, he finds out the A(x1,y1) and B(x2,y2),
but he doesn’t know where the C(x3,y3) is. The prince need your help.
Can you calculate the C(x3,y3) and tell him?
Input
The
first line is an integer T(1 <= T <= 100) which is the number of
test cases. T test cases follow. Each test case contains two coordinates
A(x1,y1) and B(x2,y2), described by four floating-point numbers x1, y1,
x2, y2 ( |x1|, |y1|, |x2|, |y2| <= 1000.0).
Please notice that A(x1,y1) and B(x2,y2) and C(x3,y3) are in an
anticlockwise direction from the equilateral triangle. And coordinates
A(x1,y1) and B(x2,y2) are given by anticlockwise.
Output
For each test case, you should output the coordinate of C(x3,y3), the result should be rounded to 2 decimal places in a line.
Sample Input
4
-100.00 0.00 0.00 0.00
0.00 0.00 0.00 100.00
0.00 0.00 100.00 100.00
1.00 0.00 1.866 0.50
Sample Output
(-50.00,86.60) (-86.60,50.00) (-36.60,136.60) (1.00,1.00)
题目及算法分析:输入A点坐标,再输入B点坐标,求C点坐标,按A B C的逆时针顺序构成一个等边三角形。
C点必然出现在AB连线的垂直平分线上,然后在确定三边相等就行了。理论上无论哪种情况都会得到两个C点
坐标,但按照要求三点要是逆时针的。故,需要用到向量的叉积运算判断方向问题!另外,这只是常规思路,
还存在斜率为0和斜率不存在的情况,这两种只需要特判解决一下就行了。
#include <stdio.h> #include <math.h> #include <string.h> #include <stdlib.h> #include <algorithm> using namespace std; struct point { double x,y; }a,b, mid; double dist(point a, point b) { return (double) ( (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y) ); } int main() { int t; scanf("%d", &t); while(t--) { scanf("%lf %lf %lf %lf", &a.x ,&a.y, &b.x, &b.y); if(a.x==0 && b.x==0 ) //dou zai y zhou { //斜率为0 mid.x=(a.x+b.x)/2.0; mid.y=(a.y+b.y)/2.0; double lon=fabs(a.y-b.y); lon=lon*lon; lon=lon-(mid.y-b.y)*(mid.y-b.y); lon=sqrt(lon); printf("(%.2lf,%.2lf)\n", -1*lon, mid.y ); } else if(a.y==0 && b.y==0) {// 斜率 不存在 mid.x=(a.x+b.x)/2.0; mid.y=(a.y+b.y)/2.0; double lon=fabs(a.x-b.x); lon=lon*lon; lon=lon-(mid.x-a.x)*(mid.x-a.x); lon=sqrt(lon); printf("(%.2lf,%.2lf)\n", mid.x, lon); } else { double k, x, y; k=(a.y-b.y)/(a.x-b.x); k=(-1.0)/k; mid.x=(a.x+b.x)/2.0; mid.y=(a.y+b.y)/2.0; double lon=dist(a, b); double dd=k*mid.x-mid.y+a.y; x=((2*a.x+2*dd*k)+sqrt( (2*a.x+2*dd*k)*(2*a.x+2*dd*k) - 4*(k*k+1)*(a.x*a.x+dd*dd-lon) ) )/(2.0*(k*k+1)); y=k*x-k*mid.x+mid.y; point A, B; A.x = b.x-a.x; A.y = b.y-a.y; B.x = x-b.x; B.y = y-b.y; if( (A.x*B.y - A.y*B.x) > 0 ){ printf("(%.2lf,%.2lf)\n", x, y ); } else { x=((2*a.x+2*dd*k)-sqrt( (2*a.x+2*dd*k)*(2*a.x+2*dd*k) - 4*(k*k+1)*(a.x*a.x+dd*dd-lon) ) )/(2.0*(k*k+1)); y=k*x-k*mid.x+mid.y; printf("(%.2lf,%.2lf)\n", x, y ); } } } return 0; }