How to draw a hexagon from a line?

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:

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); 
      }
    }
  }
};
1 Like

do you know how to draw a circle manually?

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

2 Likes

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
  • 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!

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:

1 Like