我正在试着画多个 pyramid 形状的圆,就像这样:balls in pyramid shape

我上了这堂"Balls"课:

class Balls {
  constructor(x, y, radius, color, ballCount) {
    this.x = x;
    this.y = y;
    this.radius = radius;
    this.color = color;

    this.balls = [];

    this.ballCount = ballCount;

    this.option = { restitution: 1, friction: 0.01, label: "ball" };
  }

  setupBalls() {
    for (var i = 0; i < this.ballCount; i++) {
      var ball = Bodies.circle(this.x, this.y , this.radius, this.option);
      this.balls.push(ball);
      World.add(engine.world, [this.balls[i]]);
    }
  }

  
  drawBalls() {
    fill(this.color);
    noStroke();
    for(var i = 0; i<this.balls.length; i++){
        drawVertices(this.balls[i].vertices);
    }
  }
}

在p5.js的‘Function Setup()’中调用‘setupBalls()’方法,在‘Function Draw()’中调用‘DrawBalls()’,如下所示:

function setup() {
    createCanvas(1200, 600);

    //create engine
    engine = Engine.create();
    //set world gravity to 0
    engine.world.gravity.y = 0;


    //balls
    balls = new Balls(width / 2 + 100, 250, 8, color(200, 0, 0), 15);
    balls.setupBalls();


}

 function draw() {

    background(125);
    Engine.update(engine);

    //balls
    balls.drawBalls();
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/matter-js/0.19.0/matter.min.js"></script>
<script 
src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.6.0/p5.min.js"> 
</script>

我试着使用嵌套的for循环来处理x和y位置,但我就是无法得到我想要的 pyramid 形状.

class Balls {
  constructor(x, y, radius, color, ballCount) {
    this.x = x;
    this.y = y;
    this.radius = radius;
    this.color = color;

    this.balls = [];

    this.ballCount = ballCount;

    this.option = {
      restitution: 1,
      friction: 0.01,
      label: "ball"
    };
  }

  setupBalls() {
    for (var i = 0; i < this.ballCount; i++) {
      var ball = Bodies.circle(this.x, this.y, this.radius, this.option);
      this.balls.push(ball);
      World.add(engine.world, [this.balls[i]]);
    }
  }


  drawBalls() {
    fill(this.color);
    noStroke();
    for (var i = 0; i < this.balls.length; i++) {
      drawVertices(this.balls[i].vertices);
    }
  }
}

//module aliases
var Engine = Matter.Engine;
var World = Matter.World;
var Bodies = Matter.Bodies;
var Body = Matter.Body;

function setup() {
  createCanvas(1200, 600);

  //create engine
  engine = Engine.create();
  //set world gravity to 0
  engine.world.gravity.y = 0;


  //balls
  balls = new Balls(width / 2 + 100, 250, 8, color(200, 0, 0), 15);
  balls.setupBalls();


}

function draw() {

  background(125);
  Engine.update(engine);

  //balls
  balls.drawBalls();
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/matter-js/0.19.0/matter.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.6.0/p5.min.js"></script>

推荐答案

我不熟悉matter.js,但下面的代码使用嵌套的for循环来创建一个三角形,使用圆形来模拟台球架的配置:

let numBalls = [1, 2, 3, 4, 5]; // Per column
let ballsMax = 5;
let lenDisparity = 0;
let counter = 0;

// Build by column
function circleGrid(l, t, diameter, hgap, vgap) {
  var x;
  var y; 
  for (let j = 0; j < numBalls.length; j++) {
  for (let k = 0; k < numBalls[counter]; k++) {   
      x = l + j*vgap;
      y = t + k*hgap;
      lenDisparity = ballsMax - numBalls[counter];
      if (lenDisparity > 0) {
        y += lenDisparity*hgap/2;
      }    
      fill(random(255),random(255),random(255));
      circle(x,y,diameter);
    }
    counter++;
  }
}

function setup() {
  createCanvas(400, 400);
  circleGrid(60,60,28,30,30);
}

function draw() {
}

CircleGridByColAndRow

下面的源代码演示了按列构建网格与按行构建网格的区别:

let numBalls = [1, 2, 3, 4, 5]; 
let ballsMax = 5;
let lenDisparity = 0;

function circleGridByCol(l, t, diameter, hgap, vgap) {
  var x;
  var y; 
  counter = 0;
  for (let j = 0; j < numBalls.length; j++) {
  for (let k = 0; k < numBalls[counter]; k++) {   
      x = l + j*vgap;
      y = t + k*hgap;
      lenDisparity = ballsMax - numBalls[counter];
      if (lenDisparity > 0) {
        y += lenDisparity*hgap/2;
      }    
      fill(random(255),random(255),random(255));
      circle(x,y,diameter);
    }
    counter++;
  }
}

function circleGridByRow(l, t, diameter, hgap, vgap) {
  var x;
  var y; 
  counter = 0;
  for (let k = 0; k < numBalls.length; k++) {
  for (let j = 0; j < numBalls[counter]; j++) {   
      x = l + j*vgap;
      y = t + k*hgap;
      lenDisparity = ballsMax - numBalls[counter];
      if (lenDisparity > 0) {
        x += lenDisparity*vgap/2;
      }    
      fill(random(255),random(255),random(255));
      circle(x,y,diameter);
    }
    counter++;
  }
}

function setup() {
  createCanvas(600, 250);
  circleGridByCol(60,60,28,30,30);
  circleGridByRow(250,60,28,30,30);
}

function draw() {
}

Javascript相关问答推荐

在JavaScript中检索一些文本

如何访问react路由v6加载器函数中的查询参数/搜索参数

JS、C++和C#给出不同的Base 64 Guid编码结果

使用TMS Web Core中的HTML模板中的参数调用过程

提交表格后保留Web表格中的收件箱值?

了解Node.js中的EventEums和浏览器中的addEventEums之间的关系

togglePopover()不打开但不关闭原生HTML popover'

点击按钮一次有文本出现和褪色,而不是点击两次?(html,CSS,JavaScript)

为什么Mutations 观察器用微任务队列而不是macrotask队列处理?

还原器未正确更新状态

使用Promise.All并发解决时,每个promise 的线性时间增加?

用JS从平面文件构建树形 struct 的JSON

如何在ASP.NET项目中使用Google Chart API JavaScript将二次轴线值格式化为百分比

400 bad request error posting through node-fetch

如果一个字符串前面有点、空格或无字符串,后面有空格、连字符或无字符串,则匹配正则表达式

Next.js中的服务器端组件列表筛选

Phaser3 preFX addGlow不支持zoom

如何在Reaction中清除输入字段

暂停后只有一次旋转JS

使用jQuery每隔几秒钟突出显示列表中的不同单词