1. 程式人生 > >C語言編寫程式計算圓上的點的座標

C語言編寫程式計算圓上的點的座標

Problem Description
There is a cycle with its center on the origin.
Now give you a point on the cycle, you are to find out the other two points on it, to maximize the sum of the distance between each other
you may assume that the radius of the cycle will not exceed 1000.

Input
There are T test cases, in each case there are 2 decimal number representing the coordinate of the given point.

Output
For each testcase you are supposed to output the coordinates of both of the unknow points by 3 decimal places of precision 
Alway output the lower one first(with a smaller Y-coordinate value), if they have the same Y value output the one with a smaller X.

NOTE
when output, if the absolute difference between the coordinate values X1 and X2 is smaller than 0.0005, we assume they are equal.

Sample Input
2
1.500 2.000
563.585 1.251

Sample Output
0.982 -2.299 -2.482 0.299
-280.709 -488.704 -282.876 487.453

譯:

問題描述:

有一個以原點為中心的圓
現在給你一個圓上的點,要求你找到圓上的另外的兩個點,保證這三個點彼此之間的距離最大
你可以假設圓的半徑不超過1000
輸入:
有T個測試資料,每個測試資料要求輸入兩個十進位制數代表給定點的座標
輸出
對於每個測試用例,您應該輸出兩個未知點的座標,精確到小數點後3位
總是先輸出較低的值(Y座標值較小),如果它們的Y值與較小X值的值相同的話。
請注意
當輸出時,如果X1和X2的絕對值小於0.0005,我們假設它們相等。
樣例輸入
2
1.500 - 2.000
563.585 - 1.251
樣例輸出
0.982 -2.299 -2.482 0.299
-280.709 -488.704 -282.876 487.453

 

思路:圓內接正三角形時,3個點彼此之間距離最大,可以先求角度(利用atan2可以由斜率得到角度),每次旋轉120,用極座標的方式再求直角座標。

#include<stdio.h>
#include<math.h>
#define PI 3.1415926
int main() {
	double x, y, r, a;
	int n;
	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		scanf("%lf%lf", &x, &y);
		r = sqrt(x*x + y * y);	//求半徑
		a = atan2(y, x);	//求角度
		for (int i = 0; i < 2; i++) {
			a -= PI * 2 / 3;		//旋轉120度
			printf("%0.3lf %0.3lf", r * cos(a), r * sin(a));
			if (i == 0)
				printf(" ");
		}
		printf("\n");
	}
	return 0;
}