[hdu-2080] what is the angle II

The problem of xhd is as follows: there are two points in a plane, and the angle between the two points and the connecting line of the origin is calculated.  

Note: the angle range [0180], two points will not appear in the center of the circle.  

Input

The first row of input data is a data T, indicating that there are T groups of data.  
Each group of data has four real numbers x1,y1,x2,y2, which represent the coordinates of two points respectively, and the range of these real numbers is [- 1000010000].  

Output

For each group of input data, the output included angle is accurate to two decimal places.  

Sample Input

2
1 1 2 2
1 1 1 0

Sample Output

0.00
45.00

Sure enough, I'm still too weak. It's so troublesome and long to write a water topic...

Train of thought:

1. See which quadrant the two points are in, find out the included angle with the coordinate axis, and add and subtract.

2. Using vector method, a * b = |a|b|cos (α) = x1x2+y1y2;

When using atan() function, pay attention to that the return value is radian, which should be converted to angle, angle = radian * 180/PI.

Blogs about atan() and atan2():

https://www.cnblogs.com/dutlei/archive/2013/01/14/2860332.html

https://blog.csdn.net/tuyang120428941/article/details/5822041

ac Code:

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<cstring> 
#include<string.h>
#include<queue>
#define PI acos(-1.0)
using namespace std;
typedef long long ll;
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		double x1,x2,y1,y2;
		scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
		if(y1/x1>0)
		{
			if(y2/x2>0)
			{
				double a1=atan(y1/x1)*180/PI;
				double a2=atan(y2/x2)*180/PI;
				double ans=a1>a2?a1-a2:a2-a1;
				printf("%.2lf\n",ans>=0?ans:-ans);
			}
			else if(y2/x2<0)
			{
				double a1=atan(y1/x1)*180/PI;
				double a2=atan(y2/x2)*180/PI;
				double ans=180-a1+a2;
				printf("%.2lf\n",ans>=0?ans:-ans);
			}
			else if(x2>0)
			{
				double a1=atan(y1/x1)*180/PI;
				printf("%.2lf\n",a1);
			}
			else
			{
				double a1=atan(y1/x1)*180/PI;
				printf("%.2lf\n",180-a1);
			}
		}
		else if(y1/x1<0)
		{
			if(y2/x2>0)
			{
				double a1=atan(y1/x1)*180/PI;
				double a2=atan(y2/x2)*180/PI;
				double ans=180+a1-a2;
				printf("%.2lf\n",ans);
			}
			else if(y2/x2<0)
			{
				double a1=atan(y1/x1)*180/PI;
				double a2=atan(y2/x2)*180/PI;
				double ans=-a1+a2;
				printf("%.2lf\n",ans>=0?ans:-ans);
			}
			else if(x2>0)
			{
				double a1=atan(y1/x1)*180/PI;
				printf("%.2lf\n",180+a1);
			}
			else
			{
				double a1=atan(y1/x1)*180/PI;
				printf("%.2lf\n",-a1);
			}
		}
		else if(x1>0)
		{
			if(y2/x2>0)
			{
				double a2=atan(y2/x2)*180/PI;
				printf("%.2lf\n",a2);
			}
			else if(y2/x2<0)
			{
				double a2=atan(y2/x2)*180/PI;
				double ans=180+a2;
				printf("%.2lf\n",ans>=0?ans:-ans);
			}
			else if(x2>0)
			{
				double a1=0;
				printf("%.2lf\n",a1);
			}
			else
			{
				double a1=180;
				printf("%.2lf\n",a1);
			}
		}
		else{
			if(y2/x2>0)
			{
				double a2=atan(y2/x2)*180/PI;
				printf("%.2lf\n",180-a2);
			}
			else if(y2/x2<0)
			{
				double a2=atan(y2/x2)*180/PI;
				double ans=-a2;
				printf("%.2lf\n",ans>=0?ans:-ans);
			}
			else if(x2>0)
			{
				double a1=180;
				printf("%.2lf\n",a1);
			}
			else
			{
				double a1=0;
				printf("%.2lf\n",a1);
			}
		}
		
	}
	return 0;
 } 

Vector normal code:

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<cstring> 
#include<string.h>
#include<queue>
#define PI acos(-1.0) 
using namespace std;
int main()  
{  
    double x1,x2,y1,y2;  
    double a1,a2;
    int t;
    scanf("%d",&t);  
    while (t--)  
    {
        scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);  
        a1=sqrt(x1*x1+y1*y1)*sqrt(x2*x2+y2*y2); 
        a2=x1*x2+y1*y2;
        printf("%.2lf\n",acos(a2/a1)*180.0/PI);  
    }
    return 0;  
} 

 

Posted by Distant_storm on Fri, 31 Jan 2020 14:29:59 -0800