DEV Community

Bogdan Alexandru Militaru
Bogdan Alexandru Militaru

Posted on • Originally published at whyboobo.com on

Generate hexagons in JS based on center coordinates and radius

How to generate hexagons in javascript based on the center point coordinates and radius length

function drawHexagon (centerPointLatitude, centerPointLongitude, radiusLength) {
  const radius = radiusLength / 6371

  const angle = 2 * Math.PI / 6 // 2PI (360 degree) divded by 6 points of the hexagon shape

  const hexagon = []
  for (var i = 0; i < 6; i++) {
    const longitude = centerPointLongitude + (radius * Math.cos(angle * i))
    const latitude = centerPointLatitude + (radius * Math.sin(angle * i))

    hexagon.push([longitude, latitude])
  }

  // close the polygon adding the initial point
  hexagon.push(hexagon[0])

  return hexagon
}

drawHexagon(44.4268, 26.1024, 10)

Enter fullscreen mode Exit fullscreen mode

Image credits Pixabay

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay