64,652
社区成员
发帖
与我相关
我的任务
分享
情况1 : x>0 y>0 第一象限, 若x>y,在第一象限的上半部分,否则下半部分
情况2 : x<0 y>0 第二象限,若x>-y,在第二象限的上半部分,否则下半部分
情况3 :x<0 y<0 第三象限。。。。。。
情况4 : x>0 y<0 第四象限。。。。。。
#include<iostream>
#include<math.h>
#define PI 3.1415926 //宏定义PI,意图是:一个圆周率的近似值。
using namespace std;
int whichSector(double X, double Y, double R) {
double mod;
mod = sqrt(X * X + Y * Y); //将点(X,Y)视为复平面上的点,与复数一一对应,现求复数的模。
double arg;
arg = atan2(Y, X); //求复数的辐角。
if (mod > R) { //如果复数的模大于预设的半径,则返回0。
return 0;
} else { //根据复数的辐角来判别该点落在那个扇区。
if (arg > (double) (0.0 * PI / 4.0)
&& arg < (double) (1.0 * PI / 4.0)) {
return 1;
}
if (arg > (double) (1.0 * PI / 4.0)
&& arg < (double) (2.0 * PI / 4.0)) {
return 2;
}
if (arg > (double) (2.0 * PI / 4.0)
&& arg < (double) (3.0 * PI / 4.0)) {
return 3;
}
if (arg > (double) (3.0 * PI / 4.0)
&& arg < (double) (4.0 * PI / 4.0)) {
return 4;
}
if (arg > (double) (-4.0 * PI / 4.0)
&& arg < (double) (-3.0 * PI / 4.0)) {
return 5;
}
if (arg > (double) (-3.0 * PI / 4.0)
&& arg < (double) (-2.0 * PI / 4.0)) {
return 6;
}
if (arg > (double) (-2.0 * PI / 4.0)
&& arg < (double) (-1.0 * PI / 4.0)) {
return 7;
}
if (arg > (double) (-1.0 * PI / 4.0)
&& arg < (double) (-0.0 * PI / 4.0)) {
return 8;
}
return 999; //这是几率极低的事件。
}
return 0;
}
int main(void) {
double r, x, y;
cout << "Please tell me the Radius of your Circle:" << endl;
cin >> r;
cout << "Please tell me the value of X of your Point:" << endl;
cin >> x;
cout << "Please tell me the value of Y of your Point:" << endl;
cin >> y;
int w;
w = whichSector(x, y, r);
if (0 == w) {
cout << "Your point is OUT of your Circle!" << endl;
return 0;
} else {
if (999 == w) {
cout
<< "Oops... Your Point is ***critically*** ON a border that belongs to no sector!"
<< endl;
} else {
cout << "Your Point is ON a certain area of the sector #" << w
<< "." << endl;
}
}
return 0;
}
+-------+-------+-------+------+-------+--------+
| x>0 | y>0 |x^2>y^2| Code | dValue| Sector#|
+-------+-------+-------+------+-------+--------+
| 1 | 1 | 1 | 111 | 7 | 1 |
+-------+-------+-------+------+-------+--------+
| 1 | 1 | 0 | 110 | 6 | 2 |
+-------+-------+-------+------+-------+--------+
| 0 | 1 | 0 | 010 | 2 | 3 |
+-------+-------+-------+------+-------+--------+
| 0 | 1 | 1 | 011 | 3 | 4 |
+-------+-------+-------+------+-------+--------+
| 0 | 0 | 1 | 001 | 1 | 5 |
+-------+-------+-------+------+-------+--------+
| 0 | 0 | 0 | 000 | 0 | 6 |
+-------+-------+-------+------+-------+--------+
| 1 | 0 | 0 | 100 | 4 | 7 |
+-------+-------+-------+------+-------+--------+
| 1 | 0 | 1 | 101 | 5 | 8 |
+-------+-------+-------+------+-------+--------+
+-------+--------+
| dValue| Sector#|
+-------+--------+
| 0 | 6 |
+-------+--------+
| 1 | 5 |
+-------+--------+
| 2 | 3 |
+-------+--------+
| 3 | 4 |
+-------+--------+
| 4 | 7 |
+-------+--------+
| 5 | 8 |
+-------+--------+
| 6 | 2 |
+-------+--------+
| 7 | 1 |
+-------+--------+
#include<iostream>
using namespace std;
int main(void) {
int sector[] = { 6, 5, 3, 4, 7, 8, 2, 1 };
double r, x, y;
cout << "Please tell me the Radius of your Circle:" << endl;
cin >> r;
cout << "Please tell me the value of X of your Point:" << endl;
cin >> x;
cout << "Please tell me the value of Y of your Point:" << endl;
cin >> y;
if (x * x + y * y > r * r) {
cout << "Your point is OUT of your Circle!" << endl;
} else {
cout<< "Your Point is ON a certain area of the sector #"
<< sector[(int) (x > 0) * 4 + (int) (y > 0) * 2
+ (int) (x * x > y * y)] << "." << endl;
}
return 0;
}