Hi
I am pasting here code for finding coordinates of a regular polygon.

void main()
  {
     int no_of_side = 6, side_length=10, i;
     float ext_angle = 60; 
     float angle=0.0;
     float x_coord[6], y_coord[6];

    for(i=no_of_side-1; i>=0; i--)
       {
	 x_coord[i] = x_coord[i] + side_length * angle + side_length ;
	 y_coord[i] = y_coord[i] + side_length * angle + side_length ;
	 angle = angle + ext_angle;
       }

     for(i=no_of_side-1; i>=0; i--)
       {
	 printf("\nX Coord = %f  ",x_coord[i]);
	 printf("\Y Coord = %f \n",y_coord[i]);
       }
  getch();
}

What will be the code for finding coordinates of a polyhex, let suppose there are 20 or 50 points, how to arrange these points in a polyhex?

Thanks in advance
Vikash

x_coord = x_coord + side_length * angle + side_length ;
y_coord = y_coord + side_length * angle + side_length ;
The red vars are used without initialisation, this is not always a good thing to do.
Polyhexes have all their points where they should be, so what do you mean by 20 or 50 points?

The lack of initialization is a problem I fully concur with.

But your maths is junk.
You are multiplying a side length an angle in degrees!!!
Try some trig.

You want to do it is set

x_coord[i]=x_coord[i-1]+side_length*sin(angle);
y_coord[i]=y_coord[i-1]+side_length*cos(angle);

Note that you are setting the next point based on the previous.
Obviously, you might need to set the first point, and you might need to convert from a centre point representation.

And remember to convert angles to radians.

Additionally, write a short point class for 2d and 3d points because it will make life much easier later.

The code above does scale for larger objects.

Sorry, I forget to write cos & sin for y_coord & x_coord respectively. Initilize array x_coord[] & y_coord[] with zero.

My problem is : I have let say 20 or 50 points, I want to arrange these points in a polyhex, i.e., what will be the coordinates for those points in a polyhex? Means we have to find out coordinates of a polyhex so that 20 or 50 points or more points can be arranged to give a figure of polyhex.


Thanks
Vikash

They're not multiples of 6.

See a dihex, it has ten points.

To Vikash : start with one hexagon and build it up from there. It must be a matter of coordinate translation. I think you must also determine if you can form a polyhex with the points you have.
Simple example : with 5 points you can't even make a monohex.

Hi,
My problem is to build to polyhex. The code above with correction can be used to build a monohex.

Now, let say if we have 10 points we can build a dihex. With 17 points we can build a trihex.
The question is :
1. How we can build this dihex, trihex, tetrahex & so on.
2. If suppose we have 11 to 16 points, I want to build a dihex with 10 points & the remaining points should arrange itself with dihex which satisfy the properties of a regular hexagon, e.g. all the points which have arranged itself with dihex should have an exterior angle of 60 degree, vertex angle of 120 degree and so on.
3. Same with 20 or 50 points, i.e. go on building a polyhex, & the remaining points should satisfy the properties of a regular hexagon. All the remaining points should be attached to the polyhex.


Thanks
Vikash

Here is the complete code for finding coordinates of a regular hexagon.

#include<math.h>
#include<stdio.h>
double deg2rad(double deg)
  {
    return (M_PI * deg / 180.0);
  }

double rad2deg(double rad)
  {
    return  (180.0 * rad / M_PI);
  }

int main()
  {   
     int no_of_side = 6, side_length=10, i;
     double ext_angle = 60;
     double angle=0.0;
     double x_coord[6]={0,0,0,0,0,0}, y_coord[6]={0,0,0,0,0,0};

     for(i=no_of_side-1; i>=0; i--)
       {
	 x_coord[i] = x_coord[i] + side_length * cos(deg2rad(angle)) + side_length ;
	 y_coord[i] = y_coord[i] + side_length * sin(deg2rad(angle)) + side_length ;
	 angle = angle + ext_angle;
       }

     for(i=no_of_side-1; i>=0; i--)
       {
	 printf("\nX Coord = %f  ",x_coord[i]);
	 printf("\Y Coord = %f \n",y_coord[i]);
       }

  return 0;
}


OUTPUT

X Coord = 20.000000  Y Coord = 10.000000
X Coord = 15.000000  Y Coord = 18.660254
X Coord = 5.000000  Y Coord = 18.660254
X Coord = 0.000000  Y Coord = 10.000000
X Coord = 5.000000  Y Coord = 1.339746
X Coord = 15.000000  Y Coord = 1.339746

So, what will be code for the scenario mentioned above.

Thanks
Vikash

Well obviously I write stuff which is incomprehensible! :(

You need

x_coord[i]=xcoord[i-1]+...

(or the way you have the loop backwards, you need i+1.)

If you had used a point class, then you would have easily been able to take the dot product and seen that the angles between your points are incorrect.

Since Salmen's reference is to a stub page in wikipedia I am leaving the page from mathematica/wolfram. It is much more descriptive.

http://mathworld.wolfram.com/Polyhex.html

The best way to tackle the problem is to write a point class... (see earlier post) and remove duplicates and then inner points.

Further help when you show a bit more careful effort and some level of actually having read any of the references.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.