简体   繁体   English

如何用一个更大的瓷砖制作网格

[英]How to make a grid with one bigger tile

I'm working on this program for a grid that generates geometric shapes and I want the function drawSubdividedCircle to always appear only once at a random position on the grid and take up the with and height of two rows/columns.我正在为一个生成几何形状的网格开发这个程序,我希望 function drawSubdividedCircle 始终只在网格上随机出现一次position 并占据行/列的宽度和高度。 I think the main problem is stacking because when i run it now the objects overlap.我认为主要问题是堆叠,因为当我现在运行它时,对象重叠。 This is basically my goal:这基本上是我的目标: 在此处输入图像描述

And this is the code:这是代码:

 let colors = [ "#F7F7F7", "#141414", "#f07f45", "#bcaad6", "#60388e", "#00afc6", "#aae3ea" ]; let tilesX = 5; let tilesY = 5; let tileW; let tileH; let tileSize; function drawSubdividedCircle (x, y, size, segments, layers) { segments = random (1,13); layers = random (1,13); const r = 360 / segments; for (let i = 0; i < segments; i++) { for (let j = 0; j < layers; j++) { fill(random(colors)); const s = map(j, 0, layers, size, 0); arc( x + size / 2, y + size / 2, s, s, radians(r * i), radians(r * (i + 1))); } } } function setup() { createCanvas(500, 500, SVG); tileW = width / tilesX; tileH = height / tilesY; tileSize = (tileW, tileH); } function draw() { noStroke(); frameRate(2); for (let x = 0; x < tilesX; x++) { for (let y = 0; y < tilesY; y++) { let r = random(1); if (r < 0.5) { ellipse((x - 0.5) * tileSize, (y - 0.5) * tileSize, tileSize, tileSize); fill(random(colors)); } else { rect(x * tileSize, y * tileSize, tileSize, tileSize); fill(random(colors)); }{ drawSubdividedCircle(x * tileSize, y * tileSize, tileSize*2); fill(random(colors)); } } } //save("sketch1.6.5.svg"); //print("saved svg"); //noLoop(); }

Added an array to keep track of the state of each individual tile.添加了一个数组来跟踪每个单独的图块的 state。 Could be optimized for sure, but this works just fine.可以肯定地进行优化,但这工作得很好。 https://editor.p5js.org/Kroepniek/sketches/MsFlcY3kl https://editor.p5js.org/Kroepniek/sketches/MsFlcY3kl

let colors = [
  "#F7F7F7",
  "#141414",
  "#f07f45",
  "#bcaad6",
  "#60388e",
  "#00afc6",
  "#aae3ea"
];

let tilesX = 5;
let tilesY = 5;

let tileW;
let tileH;
let tileSize;

let tileIsFree;

let subdividedCirclesCount = 0;
const subdividedCirclesLimit = 1;

function reinitTileStateArray()
{
  tileIsFree = [];
    
  for (let x = 0; x < tilesX; x++)
  {
    for (let y = 0; y < tilesY; y++)
    {
      tileIsFree[y * tilesX + x] = true;
    }
  }
  
  subdividedCirclesCount = 0;
}

function drawSubdividedCircle (x, y, size, segments, layers) {
  
  subdividedCirclesCount++;
  
  segments = random (1,13);
  layers = random (1,13);
  
  const r = 360 / segments;

  
  for (let j = 0; j < layers; j++)
  {
    for (let i = 0; i < segments; i++)
    {
      fill(random(colors));
      const s = map(j, 0, layers, size, 0);
      arc(
        x + size / 2,
        y + size / 2,
        s,
        s,
        radians(r * i),
        radians(r * (i + 1)));
    }
  }
}

function setup() {
  
  createCanvas(500, 500);
  frameRate(2);
  
  tileW    = width / tilesX;
  tileH    = height / tilesY;
  tileSize = (tileW, tileH);
}

function draw() {
  
  background(0);
  noStroke();
  
  reinitTileStateArray();
  
  let randomChance = 0.5;
  
  for (let y = 0; y < tilesY; y++)
  {  
    for (let x = 0; x < tilesX; x++)
    {
      let r = random();

      if (tileIsFree[y * tilesX + x])
      {
        fill(random(colors));
        rect(x * tileSize, y * tileSize, tileSize, tileSize);
        
        if (r < randomChance)
        {
          fill(random(colors));
          ellipse((x + 0.5) * tileSize, (y + 0.5) * tileSize, tileSize, tileSize);
        }
        else if (subdividedCirclesCount < subdividedCirclesLimit && tileIsFree[y * tilesX + x + 1])
        {
          drawSubdividedCircle(x * tileSize, y * tileSize, tileSize * 2);
          
          tileIsFree[y * tilesX + x]           = false;
          tileIsFree[y * tilesX + x + 1]       = false;
          tileIsFree[(y + 1) * tilesX + x]     = false;
          tileIsFree[(y + 1) * tilesX + x + 1] = false;
          
          randomChance = 0.75;
        }
      }
    }
  }
  //save("sketch1.6.5.svg");
  //print("saved svg");
  //noLoop();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM