我想找到一个六边形或圆形的点的位置(扇区),分为 4 个不同的扇区(sc,s1,s2,s3)。我所拥有的只是该点的 x 和 y 坐标。我编写的基本代码已在此处给出,但它不会根据给定的 x 和 y 坐标返回正确的扇区。
这是为了更好地理解图片。
#include <stdio.h>
#include <math.h>
#define PI 3.14159265
#define num_sec 4
int main ()
{
double x, y, angle, radius;
x = 19.0; //x_coordinate of a point
y = -30.1; //y_coordinate of a point
angle = atan2 (y,x) * 180 / PI; // converting Cartesian plan to polar and radians to degrees also the arc angle are negative so to convert them into positive we add 360 in it.
if (angle < 0) angle += 360;
radius = hypot (x, y); // calculating radius
printf ("For x_coordinate=%f and y_coordinate=%f the Angle=%f and the Radius=%f \n\n",x,y,angle,radius);
int sector[num_sec];
char sc, s1, s2, s3;
if (radius <= 157.5) {sector[0] = sc;}
if (radius > 157.5 && angle>0 || angle <= 120) {sector[1] = s1;}
if (radius > 157.5 && angle > 120 || angle <= 240) {sector[2] = s2;}
if (radius > 157.5 && angle > 240 || angle <= 360) {sector[3] = s3;}
printf ("%f %f %f %f",sc,s1,s2,s3);
return 0;
}
最佳答案
基于一些假设,不确定 sc,s1 到底应该做什么,但我想我知道你想要什么......如果这有帮助
#include <stdio.h>
#include <math.h>
#define PI 3.14159265
#define num_sec 4
int main (){
double x, y, angle, radius;
x = 19.0; //x_coordinate of a point
y = -30.1; //y_coordinate of a point
angle = atan2 (y,x) * 180 / PI; // converting Cartesian plan to polar and radians to degrees also the arc angle are negative so to convert them into positive we add 360 in it.
if (angle < 0) angle += 360;
radius = hypot (x, y); // calculating radius
printf ("For x_cordinate=%f and y_cordinate=%f the Angle=%f and the Radius=%f \n\n",x,y,angle,radius);
int sector[num_sec]={0};
if (radius <= 157.5){
sector[0]++;
}
else if (radius > 157.5 && (angle > 0 && angle <= 120)) {
sector[2]++;
}
else if (radius > 157.5 && (angle > 120 && angle <= 240)) {
sector[1]++;
}
else if (radius > 157.5 && (angle > 240 && angle <= 360)){
sector[3]++;
}
printf ("%d %d %d %d",sector[0],sector[1],sector[2],sector[3]);
return 0;
}
关于c - 在C语言中,如何找到划分为不同扇区的六边形形状中的点的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21719373/