Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

244
Views
Visualización incompleta del triángulo de Sierpinski

Estoy siguiendo el libro de texto The Nature of code 's Example 7.1. ( El código original está escrito en Java, pero dado que la biblioteca de procesamiento es funcionalmente idéntica a p5.js, lo he reescrito en JavaScript por conveniencia )

Creo que he copiado el código de ejemplos palabra por palabra, pero de alguna manera terminé con un resultado que no esperaba. Hay una porción incompleta en el triángulo de Sierpinski que se muestra.

Me gustaría saber dónde me estoy equivocando en mi código, o qué podría estar malinterpretando para causar este tipo de problema.

El triángulo de Sierpinski se muestra de forma incompleta

Aquí está el código de la imagen.

 class CA { constructor(ca_width) { this.generation = 0; this.w = 5; this.cells = new Array(1050 / this.w); this.newcells = new Array(this.cells.length); this.ruleset = [0, 1, 0, 1, 1, 0, 1, 0]; // [1,1,0,1,1,1,1,0]// for (let i = 0; i < this.cells.length; i++) { this.cells[i] = 0; } this.cells[this.cells.length / 2] = 1; } generate() { let nextgen = new Array(this.cells.length); for (let i = 1; i < this.cells.length - 1; i++) { let left = this.cells[i - 1]; let me = this.cells[i]; let right = this.cells[i + 1]; nextgen[i] = this.rules(left, me, right); } this.cells = nextgen; this.generation++; } rules(a, b, c) { let s = "" + a + b + c; let index = parseInt(s, 2); return this.ruleset[index]; } display() { for (let i = 0; i < this.cells.length; i++) { if (this.cells[i] == 0) fill(255); else fill(0); stroke(0); rect(i * this.w, this.generation * this.w, this.w, this.w); } } } let cA; function setup() { createCanvas(windowWidth, windowHeight); cA = new CA(1000); } function draw() { // createCanvas(windowWidth, 400); // background(150); cA.display(); cA.generate(); }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.1/p5.js"></script>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Según su lógica para calcular la próxima generación, this.cells[0] y this.cells.at(-1) siempre están indefinidos, lo que se representa en negro. Es posible que desee inicializarlos en 0 y posiblemente usar una lógica envolvente para calcular su valor (es decir cells[0] = rule(cells.at(-1), cells[0], cells[1]) ).

No sé cómo se ve su código Java, pero si está usando un new int[] , entonces se pondrá a cero y probablemente funcione como se esperaba, a diferencia de JS Array() .

En general, tenga cuidado con el constructor Array() en JS. Deja las celdas en un estado no inicializado ("ranuras vacías"), por lo que generalmente desea encadenar .fill(0) o distribuirlo/ map para convertirlo en una matriz utilizable y no sorprendente que esperaría que emitiera dicho constructor, como lo hace en Java.

Tenga en cuenta que Nature of Code está disponible en una versión p5.js , y existen traductores automáticos de Processing a p5.js, como pde2js . Tenga en cuenta que no he probado pde2js ni los otros traductores, pero parece que vale la pena echarles un vistazo.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!