Search code examples
clocationregion

In C language, how to Find a location of a point in a Hexagonal shape divided in different sectors


I want to find the location (sector) of a point in hexagonal or circular shape divided in to 4 different sectors (sc,s1,s2,s3). All I have is the x and y coordinates of the point. The basic code I have written is given here but it doesn't return the correct sector according to given x and y coordinates.

Here is the picture for better understanding.

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;
}

Solution

  • based on a few assumptions, not sure what exactly sc,s1 are supposed do do, but I think I figured what you want.. if this helps

    #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;
      }