p5实现一个三维有限自动机效果代码

代码语言:html

所属分类:三维

代码描述:p5实现一个三维有限自动机效果代码,可旋转缩放

代码标签: p5 有限自动机

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html lang="en" >

<head>

  <meta charset="UTF-8">
  

  
  
  


</head>

  <body style="background-color:rgb(0,0,0);">

<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/p5.js"></script>
      <script >
//up and down change the rules for life
//spacebar will change the number of nodes
//edges and dead/alive are random initially
//doodle code for mathober "rule" prompt (needs cleanup)
//the last rule is just death
//i have many more complex rules in mind, but don't have time to do this all in one day.
let n = 50;
let r = 200;
let rule = 6;
let nodeArray = [];
let edgeArray = [];
let neighborArray = [];
let nRules = 8;
function setup() {
  createCanvas(windowWidth, windowHeight, WEBGL);
  r = min(width, height) / 2.5;
  angleMode(DEGREES);
  initializeNodes();
  initializeEdges();
  //frameRate(1);
  background(100);
  fill(191, 214, 212);
  plotEdges();
  ruleDots();
  for (let i = 0; i < n; i++) {
    nodeArray[i].display();
  }
}

function draw() {
  background(100);
  orbitControl();
  rotateX(frameCount / 10);
  rotateX(frameCount / 10);
  ruleDots();
  plotEdges();
  applyRules();
  noStroke();
  for (let i = 0; i < n; i++) {
    nodeArray[i].display();
  }
  livingNeighbors();
}
class nodeS {
  constructor(nId, a) {
    this.x = r * sin((nId * 360) / n) * sin((nId * 180) / n);
    this.y = r * cos((nId * 180) / n);
    this.z = r * sin((nId * 180) / n) * cos((nId * 360) / n);
    this.alive = a;
  }
  display() {
    if (this.alive) {
      fill(255);
    } else {
      fill(20);
    }
    push();
    noStroke();
    translate(this.x, this.y, this.z);
    sphere(5, 5, 5);
    pop();
  }
}
function initializeNodes() {
  for (let i = 0; i < n; i++) {
    nodeArray.push(new nodeS(i, randomBool()));
  }
}
function initializeEdges() {
  for (let i = 0; i < n; i++) {
    edgeArray[i] = [];
    for (let j = 0; j < i; j++) {
      edgeArray[i][i] = false;
      if (i !== j) {
        edgeArray[i][j] = randomBool();
      }
    }
  }
}
function randomBool() {
  a = random(0, 1);
  if (round(a) === 1) {
    return true;
  } else {
    return false;
  }
}
function plotEdges() {
  for (let i = 0; i < n; i++) {
    for (let j = 0; j < i; j++) {
      if (i != j && edgeArray[i][j]) {
        if (nodeArray[i].alive && nodeArray[j].alive) {
          stroke(255);
          strokeWeight(1);
        } else {
          stroke(0);
          strokeWeight(0.1);
        }
        line(
          nodeArray[i].x,
          nodeArray[i].y,
          nodeArray[i].z,
          nodeArray[j].x,
          nodeArray[j].y,
          nodeArray[j].z
        );
      }
    }
  }
}

function applyRules() {
  if (rule === 0) {
    t = floor(n / 50);
    for (let i = 0; i < n; i++) {
      if (neigh.........完整代码请登录后点击上方下载按钮下载查看

网友评论0