山东省第四届 A Rescue The Princess

题目描述

    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?

输入

    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.

输出

    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.

示例输入

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

示例输出

(-50.00,86.60)
(-86.60,50.00)
(-36.60,136.60)
(1.00,1.00)

#include <stdio.h>
#include <math.h>
const double pi = acos(-1.0);
int main()
{
    int t;
    double x1,x2,x3,y1,y2,y3,l,at;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
        at = atan2(y2-y1,x2-x1);
        l = sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
        x3 = x1+l*cos(at+pi/3.0);
        y3 = y1+l*sin(at+pi/3.0);
        printf("(%.2lf,%.2lf)\n",x3,y3);
    }
    return 0;
}

渐渐体会到了数学的可爱and可怕之处了。。。

也知道为什么大部分大神都是深井冰状态。。。


知识:

在C语言的math.h或C++中的cmath中有两个求反正切的函数atan(double x)与atan2(double y,double x)  他们返回的值是弧度 要转化为角度再自己处理下。

前者接受的是一个正切值(直线的斜率)得到夹角,但是由于正切的规律性本可以有两个角度的但它却只返回一个,因为atan的值域是从-90~90 也就是它只处理一四象限,所以一般不用它。

第二个atan2(double y,double x) 其中y代表已知点的Y坐标 同理x ,返回值是此点与远点连线与x轴正方向的夹角,这样它就可以处理四个象限的任意情况了,它的值域相应的也就是-180~180了

例如:

例1:斜率是1的直线的夹角

cout<<atan(1.0)*180/PI;//45°

cout<<atan2(1.0,1.0)*180/PI;//45° 第一象限

cout<<atan2(-1.0,-1.0)*180/PI;//-135°第三象限

后两个斜率都是1 但是atan只能求出一个45°

例2:斜率是-1的直线的角度

cout<<atan(-1.0)*180/PI;//-45°

cout<<atan2(-1.0,1.0)*180/PI;//-45° y为负 在第四象限

cout<<atan2(1.0,-1.0)*180/PI;//135° x为负 在第二象限

 

常用的不是求过原点的直线的夹角 往往是求一个线段的夹角 这对于atan2就更是如鱼得水了

例如求A(1.0,1.0) B(3.0,3.0)这个线段AB与x轴正方向的夹角

用atan2表示为 atan2(y2-y1,x2-x1) 即 atan2(3.0-1.0,3.0-1.0)

它的原理就相当于把A点平移到原点B点相应变成B'(x2-x1,y2-y1)点 这样就又回到先前了

例三:

A(0.0,5.0) B(5.0,10.0)

线段AB的夹角为

cout<<atan2(5.0,5.0)*180/PI;//45°

^_^


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

会飞的Anthony

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值