Limit maze to region

87 Views Asked by At

I have created a random hexagonal maze using an algorithm. But how do I limit the maze to just the green hexagonal region in the following picture? Note that the size of the maze and the green region can vary. Thanks!

enter image description here

1

There are 1 best solutions below

2
On BEST ANSWER

This works for even hexagons, but shouldn't be that difficult to adapt for odd hexagons.

<!DOCTYPE html>
<html>
<body>
<table>
<tr><td>
<span id='s1'></span>
</td><td>
<span id='s2'></span>
</td></tr>
</table>
</body>
<script>
ne=1;
while (ne%2!=0) ne=prompt('enter n (even)',4);
gride=new Array();
grido=new Array();

for (i=0;i<2*ne-1;i++) {gride[i]=new Array();for (j=0;j<ne;j++) gride[i][j]=1;}
for (i=0;i<2*ne-1;i++) {grido[i]=new Array();for (j=0;j<ne-1;j++) grido[i][j]=1;}
drawGrid(s1);
makeHexagon();
drawGrid(s2);

function drawGrid(target) {
str=''
for (i=0;i<2*ne-1;i++) {
for (j=0;j<ne;j++) str+=gride[i][j]+'---';
str+='<br>';
for (j=0;j<ne-1;j++) str+='--'+grido[i][j]+'-';
str+='<br>';
}
target.innerHTML=str;
}

function makeHexagon() {
cne=ne/2;
for (i=0;i<2*ne-1;i++) {
if (cne>0) for (j=0;j<cne;j++) {gride[i][j]='*';gride[i][ne-j-1]='*';}
if (i<ne-1) cne--; else if (i>ne-1) cne++;
}
cne=ne/2-1;
for (i=0;i<2*ne-1;i++) {
if (cne>0) for (j=0;j<cne;j++) {grido[i][j]='*';grido[i][ne-j-2]='*';}
if (i<ne-1) cne--; else cne++;
}
}

</script>
</html>

You don't really need the if (cne>0) bit in makeHexagon().