get the points of a hexagon knowing only a what length each side should be and a point

52 Views Asked by At

I am trying to write a program to display a hexagon. The only values I know are a starting point, e.g. {0, 0}

How can I know the other 5 points if I want each length to be 2 units long for example.

The interior angle of a hexagon is 120%.

But I am struggling to know how to get the next points from the starting position and knowing the above facts.

I have found this code:

export const createPoints = (startingPoint: Point) => {
  const _s32 = Math.sqrt(3) / 2;
  const A = 25;

  const xDiff = 100;
  const yDiff = 100;

  return [
    [A + xDiff, 0 + yDiff],
    [A / 2 + xDiff, A * _s32 + yDiff],
    [-A / 2 + xDiff, A * _s32 + yDiff],
    [-A + xDiff, 0 + yDiff],
    [-A / 2 + xDiff, -A * _s32 + yDiff],
    [A / 2 + xDiff, -A * _s32 + yDiff]
  ];
};

But I do not understand the calculation.

_s32 is the square root of 3 divided by two

1

There are 1 best solutions below

0
On

The trick is to rotate the hexagon around the center of its mass, then you just need to rotate the radius by 60 degrees to get each point. Here's a pseudo code

p0 = (0, 0)  # location of the center - free parameter
phi = 0.0    # angle of the 1st point w.r.t x axis - free parameter
r = 10.0     # radius of the hexagon - free parameter    

p1 = p0 + r*(cos(0*60 + phi), sin(0*60 + phi))
p2 = p0 + r*(cos(1*60 + phi), sin(1*60 + phi))
p3 = p0 + r*(cos(2*60 + phi), sin(2*60 + phi))
p4 = p0 + r*(cos(3*60 + phi), sin(3*60 + phi))
p5 = p0 + r*(cos(4*60 + phi), sin(4*60 + phi))
p6 = p0 + r*(cos(5*60 + phi), sin(5*60 + phi))

Note I have written all angles in degrees - you will want to convert them to radians multiplying them by $\pi / 180$