GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 06-Dec-2008, 12:56
vikashkumar051 vikashkumar051 is offline
New Member
 
Join Date: Dec 2008
Posts: 11
vikashkumar051 is an unknown quantity at this point

Finding coordinates of a polyhex


Hi,
I am pasting here code for finding coordinates of a regular polygon.
CPP / C++ / C Code:

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
  #2  
Old 06-Dec-2008, 15:32
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Finding coordinates of a polyhex


Quote:
Originally Posted by vikashkumar051
Hi,
I am pasting here code for finding coordinates of a regular polygon.

Hmmm... I can't see where that code can find the coordinates of anything but a straight line. Wait while I test it---

Tick
Tick
Tick

Well, after cleaning up your print statement for the Y coordinate and resolving the undefined behavior caused by adding stuff to variables x_coord[i] and y_coord[i] where you never initialized them and getting rid of the non-standard getch() function call (since my system doesn't have it) and changing non-standard definition void main() to int main(), I get the following output:
Code:
X Coord = 10.000000 Y Coord = 10.000000 X Coord = 610.000000 Y Coord = 610.000000 X Coord = 1210.000000 Y Coord = 1210.000000 X Coord = 1810.000000 Y Coord = 1810.000000 X Coord = 2410.000000 Y Coord = 2410.000000 X Coord = 3010.000000 Y Coord = 3010.000000

Now, I see that it is, indeed, showing me the coordinates of a straight line.

So: Before trying to change the program to handle N-sided polygons, how about going back to wherever you got the formulas (or revisiting whatever thought process lead to the formulas) and let's get a regular hexagon, OK?


Regards,

Dave

Footnote: Judging by some of your variable names and calculations, I perceive that you want that particular program to calculate vertices of a regular hexagon with side length of 10 units and centered at (x,y) = (10,10).

Of course, I could be wrong.

But...

By my reckoning, and for your reference: A regular hexagon with side length 10 and centered at (10,10) might have vertices at the following coordinates (approximated to six decimal places):
Code:
(20.000000,10.000000) (15.000000,18.660254) ( 5.000000,18.660254) ( 0.000000,10.000000) ( 5.000000, 1.339746) (15.000000, 1.339746)
Last edited by davekw7x : 06-Dec-2008 at 16:56.
  #3  
Old 07-Dec-2008, 02:46
vikashkumar051 vikashkumar051 is offline
New Member
 
Join Date: Dec 2008
Posts: 11
vikashkumar051 is an unknown quantity at this point

Re: Finding coordinates of a polyhex


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

CPP / C++ / C Code:
x_coord[i] = x_coord[i] + side_length * cos(angle) + side_length ;
y_coord[i] = y_coord[i] + side_length * sin(angle) + side_length ;

Now the code will gives you coordinates of a regular hexagon.

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


Thanks
Vikash
  #4  
Old 07-Dec-2008, 09:30
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Finding coordinates of a polyhex


Quote:
Originally Posted by vikashkumar051
...
Now the code will gives you coordinates of a regular hexagon....
My problem is...

Well, I have a problem, too.

Here goes.

I modified your original program as follows:
  1. I included <math.h>
  2. I deleted the getch() statement and changed "void main" to "int main"
  3. I initialized the coordinate arrays
  4. I changed the print statement for Y coordinate so that it would give the printout that I think you wanted.
  5. I changed the statements for calculating the coordinates to the two in your latest post.
Here are the results;
Code:
X Coord = 20.000000 Y Coord = 10.000000 X Coord = 0.475870 Y Coord = 6.951894 X Coord = 18.141809 Y Coord = 15.806112 X Coord = 4.015399 Y Coord = 1.988474 X Coord = 13.257813 Y Coord = 19.454451 X Coord = 9.779034 Y Coord = 0.002442

Now, these are not (that's not) the vertices of a regular hexagon. See Footnote.

I don't know whether you are actually running something else or whether you didn't actually inspect the outputs from your program.

I can't see how we can help you understand how to modify your code to give results for N-sided regular polygons if it doesn't work as expected for the given program (which is specialized for N = 6).

So I respectfully suggest:
  1. Post the code that you are actually running. The complete code.
  2. Show us the outputd from your program


Regards,

Dave

Footnote: I have attached a plot of those points.
Attached Images
File Type: pdf plot1.pdf (3.2 KB, 12 views)
  #5  
Old 07-Dec-2008, 11:09
vikashkumar051 vikashkumar051 is offline
New Member
 
Join Date: Dec 2008
Posts: 11
vikashkumar051 is an unknown quantity at this point

Re: Finding coordinates of a polyhex


Hi,
Sorry for that much trouble.
Here I am pasting the complete code with output.

CPP / C++ / C Code:

#include<iostream.h>
#include<conio.h>
#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


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
  #6  
Old 07-Dec-2008, 14:17
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Finding coordinates of a polyhex


Quote:
Originally Posted by vikashkumar051
...Sorry for that much trouble...
Not much trouble for me, just some of your time wasted since we had no way of knowing what you are really running
Quote:
Originally Posted by vikashkumar051
Here I am pasting the complete code with output.
Well I have some C++ style issues, but I will bypass them for now, so that maybe you can make some progress.
Quote:
Originally Posted by vikashkumar051
Now, let say if we have 10 points we can build a dihex. With 17 points we can build a trihex....

Since we already have a way of building a hexagon, I would start there and see how to build two hexagons that would fit together to make a dihex:

I might make a function that builds a hexagon with a given side length and center coordinates. I might try a few cases and inspect the results visually. Then try to see how to combine several in a program in some useful way.

Suppose that the first hexagon will have center (x0, y0).
DIhex figures are typically drawn so that the second hexagon is drawn with center (x0+1.5*side_length, y0-sqrt(3)/2*side_length). (Try it.)

Now I can make two distinct hexagons in a program with a function like that.

CPP / C++ / C Code:
void gen_hexagon(double side_length,
                 double x_center, double y_center,
                 double *x_coord, double *y_coord);
.
.
.
int main()
{
.
.
.
    double x1_coord[6]; /* Code in gen_hexagon doesn't require these to be initialized */
    double y1_coord[6];
    double x2_coord[6];
    double y2_coord[6];

    int side_length = 10;
    double x_center = side_length;
    double y_center = side_length;
    gen_hexagon(side_length, x_center, y_center, x1_coord, y1_coord);

    x_center += 1.5 * side_length;
    y_center -= sqrt(3.0)/2.0 * side_length;
    gen_hexagon(side_length, x_center, y_center, x2_coord, y2_coord);


.
.
.

}

I might end up with two sets of six coordinate pairs.
Code:
Centered at (10.000000,10.000000) 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 Centered at (25.000000,1.339746) X Coord = 35.000000 Y Coord = 1.339746 X Coord = 30.000000 Y Coord = 10.000000 X Coord = 20.000000 Y Coord = 10.000000 X Coord = 15.000000 Y Coord = 1.339746 X Coord = 20.000000 Y Coord = -7.320508 X Coord = 30.000000 Y Coord = -7.320508

Now, I might visually inspect them to find the common side, noting that, of the 12 vertices shown, 10 are unique.

Now it's time for you do make some decisions:

If you were going to plot the figures, would you eliminate that common line? The way that you generate the points, the line between point 5 and point 0 of the first hexagon is co-located with the line from point 2 to point3 of the second. Is there a way of generating the points in order so that the last point of the first hexagon is the first point of the second. You can "rotate" the figures by shifting the coordinate sets. Stuff like that.

Anyhow, maybe that's a start.

Regards,

Dave

Footnote: The "gen_hexagon()" function that I have in mind would create the hexagon with center at the origin and then call a "translate()" function to put it at the specified center:

CPP / C++ / C Code:
void gen_hexagon(double side_length,
                 double x_center, double y_center,
                 double *x_coord, double *y_coord)
{
    double ext_angle = 60;
    double angle     = 0;
    int no_of_side   = 6;
    for (int i = no_of_side - 1; i >= 0; i--) {
        x_coord[i] = side_length * cos(deg2rad(angle));
        y_coord[i] = side_length * sin(deg2rad(angle));
        angle = angle + ext_angle;
    }
    translate_hexagon(x_coord, y_coord, x_center, y_center);
}

void translate_hexagon(double *x_coord,  double *y_coord, 
                    double  x_center, double  y_center)
{
    for (int i = 0; i < 6; i++) {
        x_coord[i] += x_center;
        y_coord[i] += y_center;
    }
}

Maybe I would create all hexagons with center (0,0) and then call translate() and rotate() functions to get them lined up in such a way that I could eliminate the common lines. All of these could be specialized for hexagons (rather than generic graphics transformations), since that's all that is required.

Or some such thing.

Also, in C or C++, I probably wouldn't have separate arrays for the coordinates; I would probably have some kind of "point" struct that is defined to have two values corresponding to x and y coordinate values. In C++ I might even have a "hexagon" class that would give the ability to create, translate, rotate, etc., hexagon objects.

I have attached a drawing of the output of the two-hexagon program(without trying to eliminate the lines between common vertices).
Attached Images
File Type: pdf Dihex.pdf (3.1 KB, 6 views)
Last edited by davekw7x : 07-Dec-2008 at 14:53.
  #7  
Old 08-Dec-2008, 08:04
vikashkumar051 vikashkumar051 is offline
New Member
 
Join Date: Dec 2008
Posts: 11
vikashkumar051 is an unknown quantity at this point

Re: Finding coordinates of a polyhex


Hi,
Thanks a lot for your help.
I think this is what you mean.

CPP / C++ / C Code:

#include<math.h>
#include<stdio.h>
void translate_hexagon(double *x_coord,  double *y_coord, double  x_center, double  y_center);
double deg2rad(double deg)
  {
    return (M_PI * deg / 180.0);
  }

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

void gen_hexagon(double side_length, double x_center, double y_center, double *x_coord, double *y_coord)
{
    double ext_angle = 60;
    double angle     = 0;
    int no_of_side   = 6;
    int i;
    for (i = no_of_side - 1; i >= 0; i--) {
	x_coord[i] = side_length * cos(deg2rad(angle))  ;
	y_coord[i] = side_length * sin(deg2rad(angle))  ;
	angle = angle + ext_angle;
    }
    translate_hexagon(x_coord, y_coord, x_center, y_center);
}

void translate_hexagon(double *x_coord,  double *y_coord, double  x_center, double  y_center)
{
    int i;
    for (i = 0; i < 6; i++) {
	x_coord[i] += x_center;
	y_coord[i] += y_center;
    }

}

int main()
{
    double x1_coord[6];/* Code in gen_hexagon doesn't require these to be initialized */
    double y1_coord[6];
    double x2_coord[6];
    double y2_coord[6];

    int side_length = 10,i;
    double x_center = side_length;
    double y_center = side_length;
    printf("\n Centered at (%f, %f)",x_center,y_center);
    gen_hexagon(side_length, x_center, y_center, x1_coord, y1_coord);
    for(i=0; i<6; i++)
	printf("\nX_Coord = %f & Y_Coord = %f ",x1_coord[i], y1_coord[i]);

    x_center += 1.5 * side_length;
    y_center -= sqrt(3.0)/2.0 * side_length;
    printf("\n\n\n Centered at (%f, %f)",x_center,y_center);
    gen_hexagon(side_length, x_center, y_center, x2_coord, y2_coord);
    for(i=0;i<6;i++)
	printf("\nX_Coord = %f & Y_Coord = %f ",x2_coord[i], y2_coord[i]);

}

OUTPUT
 Centered at (10.000000, 10.000000)
X_Coord = 15.000000 & Y_Coord = 1.339746
X_Coord = 5.000000 & Y_Coord = 1.339746
X_Coord = 0.000000 & Y_Coord = 10.000000
X_Coord = 5.000000 & Y_Coord = 18.660254
X_Coord = 15.000000 & Y_Coord = 18.660254
X_Coord = 20.000000 & Y_Coord = 10.000000


 Centered at (25.000000, 1.339746)
X_Coord = 30.000000 & Y_Coord = -7.320508
X_Coord = 20.000000 & Y_Coord = -7.320508
X_Coord = 15.000000 & Y_Coord = 1.339746
X_Coord = 20.000000 & Y_Coord = 10.000000
X_Coord = 30.000000 & Y_Coord = 10.000000
X_Coord = 35.000000 & Y_Coord = 1.339746


Please, tell me
1. how to remove common line,
2. how to "rotate" the figures, &
3. second hexagon can be at any side of the first hexagon, so what will be the centers(x,y) of second hexagon in that case.

The code suggested by you is really a good start for me. Thanks a lot.


Thanks
Vikash
  #8  
Old 08-Dec-2008, 09:14
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Finding coordinates of a polyhex


Quote:
Originally Posted by vikashkumar051
1. how to remove common line
In the first place, the program (so far) doesn't generate lines; it generates coordinates of vertex points. In order to draw a closed figure, there will be a line from the last point to the first. If you know that there is going to be another hexagon attached at the last two vertices, then don't draw the last line.
Quote:
Originally Posted by vikashkumar051
2. how to "rotate" the figures
Since these will be rotated in increments of 60 degrees, just shift the arrays of coordinate points. For example, to rotate a hexagon by (n*60 degrees) a straightforward approach might be something like:
CPP / C++ / C Code:
void rotate_hexagon(int n, double *x_coord, double *y_coord)
{
    double tempx[6], tempy[6];
    int i;
    for (i = 0; i < 6; i++) {
        tempx[i] = x_coord[(i+n)%6];
        tempy[i] = y_coord[(i+n)%6];
    }
    for (i = 0; i < 6; i++) {
        x_coord[i] = tempx[i];
        y_coord[i] = tempy[i];
    }
}

Quote:
Originally Posted by vikashkumar051
3. second hexagon can be at any side of the first hexagon
No it can't. It will be exactly where you tell the program to put it. The idea is that you have to decide where to put it and then make the program do it.

For example, the way that we are drawing the hexagons (so far), the first virtex is always to the right of the origin, and subsequent points march counterclockwise around the figure.

For my example, I chose to put the second down and to the right of the first. Building on your example, the center of the first was at (10,10), and my addition made the center of the second at (10*1.5*side_length, 10-sqrt(3)/2*side_length).

Now if I rotate the second figure three clicks to the left (3*60 degrees) here are the coordinates of the composite figure:
Code:
Centered at (10.000000,10.000000) 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 Centered at (25.000000,1.339746) X Coord = 15.000000 Y Coord = 1.339746 X Coord = 20.000000 Y Coord = -7.320508 X Coord = 30.000000 Y Coord = -7.320508 X Coord = 35.000000 Y Coord = 1.339746 X Coord = 30.000000 Y Coord = 10.000000 X Coord = 20.000000 Y Coord = 10.000000
Now the coordinates of the first point of the second figure are the same as those of the last point of the first figure. Furthermore, the last point of the second figure is co-located with the first point of the first figure. If you want to, you can make a composite figure by appending the second set to the first (you make it so that the common point will appear only once in the composite set).

So: Plot the two (open-ended) figures separately or make a composite by starting at the first point of the first and going through all points, and the result will be a closed dihex. See attachment.

Bottom line: You are creating a specialized program to do something special. I don't really know what the final product is supposed to be, so I have tried to convey some thought processes that can approach the problem, starting with your original hexagon-creating program. There are other ways of doing stuff like this. (Generic plotting programs that find ways of eliminating common lines from collections of independent figures. Stuff like that).

Starting with three functions (generate_hexagon, translate_hexagon, rotate_hexagon) you can create as many vertices of as many of the blasted things anywhere you want them to be. It is up to you to make the decision as to how you are going to draw the figures (if, indeed, you are going to draw them) and how to handle fewer points and more points, etc.

For example: Where will you add the third hexagon if there are more points?

How can you arrange the coordinates so that you can start at the first point and end at the last point? (Imagine yourself drawing the pictures with a pencil. How would you draw a trihex without picking up the pen from the paper? Can you make a program that will do that?

Start with specifics: one hexagon, two hexagons, etc.

Get some hex nuts from the hardware store and play with some configurations of polyhexes to decide how you want to build them



Look for inspiration at places like: http://www.asahi-net.or.jp/~uy7t-isn...es/Trihex.html


Regards,

Dave
Attached Images
File Type: pdf Dihex2.pdf (3.1 KB, 18 views)
Last edited by davekw7x : 08-Dec-2008 at 10:00.
  #9  
Old 08-Dec-2008, 13:02
vikashkumar051 vikashkumar051 is offline
New Member
 
Join Date: Dec 2008
Posts: 11
vikashkumar051 is an unknown quantity at this point

Re: Finding coordinates of a polyhex


Hi Dave,

Thanks a lot for your help & guidance.


Regards,
Vikash
  #10  
Old 12-Jan-2009, 06:29
vikashkumar051 vikashkumar051 is offline
New Member
 
Join Date: Dec 2008
Posts: 11
vikashkumar051 is an unknown quantity at this point

Different problem


Hi Dave,

I am stuck at some problem, can you please take a look at post "Finding alternate paths using Dijkstra algorithm". Please help me out in solving it.

Thanks in advance
Vikash
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Picture control and mouse coordinates senor.ram MS Visual C++ / MFC Forum 2 02-Jul-2007 08:00
coordinates of a circle IRONMAN C++ Forum 2 02-Jun-2006 14:31
Passing mouse coordinates to another program aditya_naidu1 MS Visual C++ / MFC Forum 2 20-May-2005 10:08
Finding a word in a 2d grid The_Kingpin C Programming Language 4 24-Feb-2005 20:53
3D graphics coordinates TekiFreek C++ Forum 0 18-Apr-2004 23:40

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 02:56.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.