The four ACM Program design competition in Shandong province a question: Rescue the princess (Math + computational geometry)

Source: Internet
Author: User

Rescue the Princess

Time limit:1 Sec Memory limit:128 MB
submit:412 solved:168
[Submit] [Status] [Web Board]

DescriptionSeveral days ago, a beast caught a beautiful princess and the princess were 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 coordinates of an equilateral triangle in the maze. The marked coordinates is 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 to the maze, he finds out of 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?InputThe first line is a integer t (1 <= t <=) which is the number of the test cases. T test Cases follow. Each test case contains-coordinates A (x1,y1) and B (X2,y2), described by four floating-point numbers x1, y1, x2, y2 (| x1|, |y1|, |x2|, |y2| <= 1000.0). Notice that A (x1,y1) and B (X2,y2) and C (X3,y3) is in an anticlockwise direction from the equilateral t Riangle. and coordinates A (x1,y1) and B (X2,y2) is given by anticlockwise.Output

For each test case, you should output the coordinate of C (X3,Y3), the result should is rounded to 2 decimal places in a Li Ne.

Sample Input
4-100.00 0.00 0.00 0.000.00 0.00 0.00 100.000.00 0.00 100.00 100.001.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)
The problem and algorithm analysis: input a point coordinate, then enter B point coordinate, calculate c point coordinates, according to a B C counter-clockwise order constitute a equilateral triangle.
The C point is bound to appear on the perpendicular bisector of the AB connection, and then the three sides are equal on the line. Theoretically, in either case, you'll get two C points.
coordinates, but according to the requirements three points counterclockwise. Therefore, it is necessary to use the cross product of vectors to determine the direction of the problem! In addition, this is just a conventional idea,
There are also cases where the slope is 0 and the slope does not exist, both of which require a special solution.
#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, &AMP;B.Y);            if (a.x==0 && b.x==0)//dou Zai y Zhou {//slope is 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) {//slope does not exist 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)-(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)-(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;}

The four ACM Program design competition in Shandong province a question: Rescue the princess (Math + computational geometry)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.