c - 在C语言中,如何找到划分为不同扇区的六边形形状中的点的位置

标签 c location region

我想找到一个六边形或圆形的点的位置(扇区),分为 4 个不同的扇区(sc,s1,s2,s3)。我所拥有的只是该点的 x 和 y 坐标。我编写的基本代码已在此处给出,但它不会根据给定的 x 和 y 坐标返回正确的扇区。

这是为了更好地理解图片。

http://i.imgur.com/ECLEuIB.png

#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/

相关文章:

java - 如何在已动态添加的JLabel 的JPanel 中获取Location()?

c# - 从国家名称或 RegionInfo 对象获取 CultureInfo 对象

iphone - MKMapView setRegion 不是常量

class - 在 drupal 8 的标题区域中添加自定义类

c - 带 sleep 的 C 线程

c - 当分配增加超过 malloc 大小时不会出现段错误

java - 使用广播接收器检查位置

c++ - 如何在 C 中以较低的时钟速度运行程序

将十六进制转换为 float 并将 float 转换为十六进制

google-maps - 让相机随着位置的变化而移动(Google map Api)