javascript - 在javascript中填充六边形区域

标签 javascript processing maze p5.js

我正在尝试使用此维基百科链接中提到的算法构建一个迷宫生成器:https://en.wikipedia.org/wiki/Maze_generation_algorithm 因为我试图练习机器学习,所以我想使用深度优先搜索算法,并且到目前为止我的代码运行良好:

mazegenerator.js:

var cells = [];
var sideofCell = 40;
var cellh = 40*Math.sin(1.04719755);
var cellx = 20;
var past = new Array();
var current;
function setup(){
createCanvas(1400,900);
    for(var i=0;i<20;i++){
        for(var j=0;j<10;j++){
            var cell = new Cells(i,j);
            cells.push(cell);
        }
    }
    current=cells[0];  
}

function draw(){
    background(150,100,180);
    for(var i=0;i<cells.length;i++){
        cells[i].show();
    }
    frameRate(5);


 current.visited = true;
    var next = current.checkNeighbors();
    if(next){
        past.push(current);
        next.visited = true;
        current = next; 
    }else{
        current = past.pop();
    }
    print(current,i);
    print(current.j);
}

function index(i,j){  
    if(i<0 || j<0 || i>19 || j>9){
        return -1;
    }
    return j+i*10;}

function Cells(i,j){
    this.i=i;
    this.j=j;
    this.visited = false;
    this.walls = [true,true,true,true,true,true,true,true] ;// Top , Bottom , TopLeft , TopRight , BottomLeft , BottomRight

    this.show = function(){
        var x = this.i*3*cellx+100;
        if(i==1 || i%2!=0){
            var y = this.j*2*cellh+100-cellh;
        }else{
        var y = this.j*2*cellh+100;
        }
        strokeWeight(2);
        stroke(255);
    fill(0);
    if(this.walls[0]){
    line(x+cellx,y,x+3*cellx,y);//Top Line
   }
   if(this.walls[1]){

    line(x+cellx,y+cellh*2,x+3*cellx,y+cellh*2);//Bottom Line 
   }
   if(this.walls[2]){

    line(x,y+cellh,x+cellx,y);//TopLeft Line
   }
   if(this.walls[3]){

    line(x+3*cellx,y,x+4*cellx,y+cellh);//TopRight Line
   }
   if(this.walls[4]){

    line(x,y+cellh,x+cellx,y+2*cellh);//BottomLeft Line
   }
   if(this.walls[5]){

    line(x+3*cellx,y+2*cellh,x+4*cellx,y+cellh);//BottomRight Line 
   }

}

    this.checkNeighbors = function(){
        var neighbors=[];
        if(i==1 || i%2!=0){
        var topRight = cells[index(i+1,j-1)];
        var topLeft = cells[index(i-1,j-1)];
        var bottomRight = cells[index(i+1,j)];
        var bottomLeft = cells[index(i-1,j)];
        }else{
        var topRight = cells[index(i+1,j)];
        var topLeft = cells[index(i-1,j)];
        var bottomRight = cells[index(i+1,j+1)];
        var bottomLeft = cells[index(i-1,j+1)];
        }
        var bottom = cells[index(i,j+1)];
        var top = cells[index(i,j-1)];

        if(topRight && topRight.visited==false){
            neighbors.push(topRight);
        }
        if(topLeft && topLeft.visited==false){
            neighbors.push(topLeft);
        }
        if(bottomRight && bottomRight.visited==false){
            neighbors.push(bottomRight);
        }
        if(bottomLeft && bottomLeft.visited==false){
            neighbors.push(bottomLeft);
        }
        if(top && top.visited==false){
            neighbors.push(top);
        }
        if(bottom && bottom.visited==false){
            neighbors.push(bottom);
        }

        if(neighbors.length>0){
            var r=floor(random(0,neighbors.length));
            if(neighbors[r]==top){
                this.walls[0]=false;
                neighbors[r].walls[1]=false;
            }if(neighbors[r]==bottom){
                this.walls[1]=false;
                neighbors[r].walls[0]=false;
            }if(neighbors[r]==topLeft){
                this.walls[2]=false;
                neighbors[r].walls[5]=false;
            }if(neighbors[r]==topRight){
                this.walls[3]=false;
                neighbors[r].walls[4]=false;
            }if(neighbors[r]==bottomLeft){
                this.walls[4]=false;
                neighbors[r].walls[3]=false;
            }if(neighbors[r]==bottomRight){
                this.walls[5]=false;
                neighbors[r].walls[2]=false;
            }
            return neighbors[r];
        }else{
            return undefined;
            }

}}

我想要做的是为访问的单元格着色,但由于没有内置函数来绘制六边形并填充它,我不知道如何填充这些六边形区域。我认为这可以通过填充来完成在六边形的每一侧都有一个矩形,然后在中心添加一个圆,但这效率太低而且太长。我将不胜感激任何帮助:)(顺便说一句,我正在使用 p5.js 框架)。

最佳答案

beginShape()vertex() 函数允许您绘制任意形状,包括六边形。

这是一个小例子:

function setup(){
  createCanvas(200, 200);
}

function draw(){
  background(64);
  fill(255);
  
  beginShape();

  vertex(200, 100);
  vertex(150, 186);
  vertex(50, 186);
  vertex(0, 100);
  vertex(50, 13);
  vertex(150, 13);
  vertex(200, 100);
  
  endShape();
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.14/p5.js"></script>

更多信息可以在the reference中找到.

关于javascript - 在javascript中填充六边形区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46221689/

相关文章:

javascript - 如何更改 Select2 箭头图标?

javascript - 不再在 table 上添加相同的产品

Java/处理 mousePressed 在循环中不起作用

java - 从文本文件动态生成球和迷宫之间的碰撞检测

javascript - JQuery click 事件在第二次点击时触发,但在搜索表单中第一次点击时不触发

javascript - 隐藏 Reactjs 表的列

java - 为什么我的客户端(处理)无法从 ServerSocket (Java) 读取数据?

java - 移动椭圆和矩形(多部分)的碰撞检测问题

java - 使用堆栈的迷宫中的老鼠 (Java)

python - 加速 Dijkstra 的算法来解决 3D 迷宫