We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOpenGL and 3D Libraries › Drawing a hexagon same formula but 2D != 3D
Page Index Toggle Pages: 1
Drawing a hexagon same formula but 2D != 3D (Read 3128 times)
Drawing a hexagon same formula but 2D != 3D
Sep 5th, 2009, 8:51am
 
Hi all, I draw a hexagon fine no problems, them I draw the same thing in 3D and as result I get a different shape, have a look here,

2D version
homepage.ntlworld.com/ricardo.sanchez/processing/hexagon_2d/

3D version
homepage.ntlworld.com/ricardo.sanchez/processing/hexagon_3d/

if you know what I am doing wrong please let me know

Thanks!
rS
Re: Drawing a hexagon same formula but 2D != 3D
Reply #1 - Sep 6th, 2009, 12:14am
 
First thing I see is the aspect ratio...that can't just be width/height, because you'll end up with a badly rounded int, so you need

(float)width / (float)height  ...
a   glu.gluLookAt(0, 0, 10, 0, 0, 0, 0, 1, 0);     wouldn't hurt...

You need a second   gl.glLoadIdentity();   after gl.glMatrixMode(GL.GL_MODELVIEW);      ....

glVertex2d doesn't mean "two dimensional" -- or rather it does, but just the 2 -- the "d' means double (as opposed to i/int or f/float)...so I'd use f, to be consistent...

And I tinkered with your variables but I'm not sure what you're doing.  In the past I've just defined a polygon of sides n as a series of points in a circle around 0,0...that's going to be easier to work with, too, since you know exactly where your hexagon is located...and I don't remember my hexagon geometry, but I think you're going to want the square root of 3 in there someplace, if you do define the points.

This may help with OpenGL setup:
http://benhem.com/games/GLP5align/

good luck --
Ben
Re: Drawing a hexagon same formula but 2D != 3D
Reply #2 - Sep 7th, 2009, 6:45am
 
Hi Ben,

Tahnks for the link it help me understand few things.

I am drawing the hexagon using this logic,
grantmuller.com/drawing-a-hexagon-in-processing-java/

But for some reason I get a different result when porting the code to the 3D space, now following your comments I manage to draw the hexagon in a different-simpler way
Code:
gl.glBegin(GL.GL_POLYGON);
for(int i = 0; i < numSides; i++) {
 float v1 = sin(i / numSides * TWO_PI) * sideLength;
 float v2 = cos(i / numSides * TWO_PI) * sideLength;
 gl.glVertex2f(v1, v2);
}
gl.glEnd();


With that I can now draw the shape with N number of sides, in this case I only need and hexagon.

So I am going to use that, but I am still wondering why the same code have a different result from 2D to 3D? If any one know what I am doing wrong please post it here

Until next time, thanks for watching!
rS
Re: Drawing a hexagon same formula but 2D != 3D
Reply #3 - Sep 7th, 2009, 1:46pm
 
Cool, glad you got it.  That's the same general purpose polygon script I use.  I also use that sin / cos for x-shift / y-shift so often that I have a "shiftX(float xpos, float angle, float speed)" function that gets copied into most sketches.  (And Y of course.)
Not sure why the other didn't work, but the code is messier and little errors can creep in easily.  My first guess was the aspect ratio in pure GL, which *was* screwy but not the culprit.  Curious, what happens with your original code if you just use OPENGL mode but leave the code the same (processing GL layer) ?
Oh -- one more thought -- GL is fussy about using .0 after numbers intended to be read as floats.  Is one of your numbers for the B and C points getting read as an int, because it's just written, say, "2" instead of "2.0" ?
--Ben
Re: Drawing a hexagon same formula but 2D != 3D
Reply #4 - Sep 7th, 2009, 2:31pm
 
you're dividing an int (i) by a float and it's rounding the answer down

try this:

int numSides = 6;

// zeros
for(int i = 0; i < numSides; i++) {
 println(i / numSides * TWO_PI);
}

// correct
for(int i = 0; i < numSides; i++) {
 println(i * TWO_PI / numSides);
}

(for extra points use a constant or a local variable so you only do the TWO_PI / numSides division once)
Re: Drawing a hexagon same formula but 2D != 3D
Reply #5 - Sep 8th, 2009, 1:09am
 
Hi,


After adding Ben (3D setup), and koogy (int vs float calculations accuracy) comments, the same formula works in 2D and 3D, but the last version I did work best for me, so try that out

Thanks to Ben and koogy
rS
Page Index Toggle Pages: 1