# How to draw a hexagon from a line?

**URL:** <https://discourse.processing.org/t/how-to-draw-a-hexagon-from-a-line/19768>\
**Category:** Coding Questions\
**Created:** [April 14, 2020, 8:34am UTC](https://discourse.processing.org/t/how-to-draw-a-hexagon-from-a-line/19768 "2020-04-14T08:34:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tauter](https://avatars.discourse-cdn.com/v4/letter/t/e274bd/32.png) [@Tauter](https://discourse.processing.org/u/Tauter)\
**Post date:** [April 14, 2020, 8:34am UTC](https://discourse.processing.org/t/how-to-draw-a-hexagon-from-a-line/19768/1 "2020-04-14T08:34:17Z")

</div>

With given 2 point for example: (200,100) and (245,200), how can i make a hexagon out of this ?  
The hexagon should be drawn to the left side of the line. I can’t figure it out, no matter how i try.  
I can’t use anything besides my line drawing algorithm.  
Which is:

```auto
void lineDraw(float xa, float ya, float xb,float yb){
  float x = x1; float y = y1;
  float m =(y1-y2)/(x1-x2);
  if(abs(m)<=1)
  {
    if(x1<x2)
    {
      for(x=x1;x<=x2;x++)
      {
        point(x,y);
        y=y+m;
      }
    }
    if(x2<x1)
    {
      for(x=x1;x>=x2;x--)
      {
        point(x,y);
        y=y-m;
      }
    }
  }
  if(abs(m)>1)
  {
    if(y1<y2)
    {
      for(y=y1;y<=y2;y++)
      {
        point(x,y);
        x=x+(1/m); 
      }
    }
     if(y2<y1)
    {
      for(y=y1;y>=y2;y--)
      {
        point(x,y);
        x=x-(1/m); 
      }
    }
  }
};

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 14, 2020, 11:20am UTC](https://discourse.processing.org/t/how-to-draw-a-hexagon-from-a-line/19768/2 "2020-04-14T11:20:24Z")

</div>

do you know how to draw a circle manually?

```auto
for (angle ......
   x= r* cos(radians(angle)) + width/2;
   y= r* sin(radians(angle)) + height/2;

```

now when you choose a step of 360/6 and convert to radians, you receive the corners of the hexagon, connect them with lines

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [April 14, 2020, 2:15pm UTC](https://discourse.processing.org/t/how-to-draw-a-hexagon-from-a-line/19768/3 "2020-04-14T14:15:24Z")

</div>

Hello,

I assume that you want to build a hexagon and use the line element as one of the sides and build it around that.

I was able to do this as follows:

- translate the initial line (x1, y1, x2, y2) to 0, 0 (origin); this is as simple as (x1- x1, y1-y1, x2-x1, y2- y1).
- rotate endpoint of the line and save as xr, yr.  
[Rotation (mathematics) - Wikipedia](https://en.wikipedia.org/wiki/Rotation_(mathematics)#Two_dimensions)
- translate back by adding to x2, y2 to generate new rotated line (x2, y2, x2+xr, y2+yr).

I created one function to return the points for the new rotated line and used line().  
I called this function 5 more times and I had a hexagon!

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/6/6e16b004c22f19e32a2e8fd5d913aaed02f46d06.png)

There are other ways to do this.  
There are translate() and rotate() functions; I did not use these.  
I did the math. Have fun exploring the possibilities!

`:)`

> **Resources**
>
> I encourage you to review the resources available here:
> 
> - The Processing website has references, examples, tutorials, etc.  
> [https://processing.org/](https://processing.org/)
> - Those available with the Processing PDE (IDE).  
> An exploration of the tabs will reveal what is available; there are libraries and examples there.
> - The Coding Train  
> [https://www.youtube.com/channel/UCvjgXvBlbQiydffZU7m1\_aw](https://www.youtube.com/channel/UCvjgXvBlbQiydffZU7m1_aw)  
> [https://thecodingtrain.com/](https://thecodingtrain.com/)
> - Happy Coding  
> [Processing Tutorials - Happy Coding](https://happycoding.io/tutorials/processing/)
> - The Processing Foundation  
> [https://processingfoundation.org/](https://processingfoundation.org/)  
> Check out the tabs.  
> In the education tab, there is a reference to the Coding Train.
