We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hey everyone,
I just started using Processing, and am trying to create a function that would draw a hexagon. This is what I have, but it's not working:
void setup() {
size(600, 300);
}
void draw() {
background(188, 110, 117); //light red background color
hexagon(130, 150, 100);
}
void hexagon(int x, int y, int tall) { // (center x-coordinate, center y-coordinate, height)
fill(109, 185, 153); //light green fill
stroke(79, 142, 115);
strokeWeight(6);
// Draw a hexagon by creating (x, y) vertices
beginShape();
vertex(x - 3/10 * tall, y - 1/2 * tall);
vertex(x + 3/10 * tall, y - 1/2 * tall);
vertex(x + 3/5 * tall, y);
vertex(x + 3/10 * tall, y + 1/2 * tall);
vertex(x - 3/10 * tall, y + 1/2 * tall);
vertex(x - 3/5 * tall, y);
endShape(CLOSE); //closes the first and last point
}
Any help with this would be greatly appreciated, thanks!
Answers
if you want to know the reason yours isn't working, try this
i'd be tempted to do your hexagon using a loop and 60' (TWO_PI / 6.0) angles.
@ChrissyG --
Also check out the regular polygon example code, which can be used to create a hexagon:
There are also a few interesting past discussion threads on hexagons:
Thank you @Chrisir, @koogs, @jeremydouglass, and @jeremydouglass!
As far as my code goes, that did the trick! I'm a little confused though, why are the decimal points necessary?
Thanks!
"integer division". divide an integer by another integer and you won't get the fractional part. and given the the non-fractional part of 0.3 is 0 then you'll just get the 0.
http://stackoverflow.com/questions/4685450/why-is-the-result-of-1-3-0
@koogs ahh I see. Thanks so much!
With 3 / 10.0 java assumes internally that you want a float result because 10.0 is a float; so the result doesn't loose its fractional part.
I still find this confusing / not obvious for a beginner.
Well, most floats cannot be represented accurately by an integer, but converting integer to float doesn't lose much accuracy. So I guess that explains it.