A regular hexagon is a six-sided polygon. Each side is equal in length, with each interior angle measuring $120^0$.
The formula to calculate the area of a regular hexagon is as follows:
$\text{Area} = \frac{3 \times \sqrt{3} \times \text{side}^2}{2}.$
In the formula above, the side represents the length of a side of a hexagon.
We are given the length of a hexagon, and we have to calculate its area using the formula above.
Let’s suppose we have a side of length 5. When we put the value of the length in the formula above, we have the following equation:
$\begin{align*} \text{Area} &= \frac{3 \times \sqrt{3} \times \text{side}^2}{2}, \\ &= \frac{3 \times \sqrt{3} \times \text{5}^2}{2}, \\ &= \frac{3 \times \sqrt{3} \times \text{25}}{2}, \\ &\approx 64.95. \\ \end{align*}$
The approach here is to use the sqrt()
function for calculating the sqrt(3)
. We create a function and call it to calculate the area.
Let’s see an example to calculate the area of a hexagon:
sideLength = 5;function area = hexagon_area(side)area = (3 * sqrt(3) / 2) * side^2;endarea = hexagon_area(sideLength);disp(['Area of the hexagon: ', num2str(area)]);
In the code above:
Line 1: We declare a variable, side_length
, to store the length of a hexagon.
Lines 3-5: We create a function, hexagon_area
, that takes side
as the input parameter and calculates the area of the hexagon using the formula.
Line 7: We declare a variable, area
, to store the value of the area and call the function.
Line 9: We print the area of the hexagon.