Creating a simple function to draw a hexagon

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!

Tagged:

Answers

  • Answer ✓
    void setup() {
    
      size(1200, 800);
    
      drawHex(400, 400, 44);
    }
    
    void drawHex(float x, float y, float gs) {
      beginShape();
      vertex(x - gs, y - sqrt(3) * gs);
      vertex(x + gs, y - sqrt(3) * gs);
      vertex(x + 2 * gs, y);
      vertex(x + gs, y + sqrt(3) * gs);
      vertex(x - gs, y + sqrt(3) * gs);
      vertex(x - 2 * gs, y);
      endShape(CLOSE);
    }
    
  • edited March 2017 Answer ✓

    if you want to know the reason yours isn't working, try this

    println(3 / 10);
    
    println(3 / 10.0);
    

    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:

  • Answer ✓
    void setup() {
    
      size(1200, 800);
    
      drawHex(400, 400, 44);
    }
    
    void drawHex(float x, float y, float gs) {
      beginShape();
      for(int i=0;i<6;i++){
        float angle = i * 2 * PI / 6;
        vertex(x + gs*cos(angle),y + gs*sin(angle));
      }
      endShape(CLOSE);
    }
    
  • Thank you @Chrisir, @koogs, @jeremydouglass, and @jeremydouglass!

    if you want to know the reason yours isn't working, try this

    println(3 / 10);
     
    println(3 / 10.0);
    

    As far as my code goes, that did the trick! I'm a little confused though, why are the decimal points necessary?

    Thanks!

  • Answer ✓

    "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.

Sign In or Register to comment.