Skip to main content
deleted 222 characters in body; edited tags
Source Link
200_success
  • 144.1k
  • 22
  • 188
  • 473
    var canvas = document.getElementById('chaos');
    var ctx = canvas.getContext('2d');
    
    const GenerateRand = () => Math.floor(Math.random() * 7);
    const updateDot = (x, y, point) => {
      let X = Math.min(x,point.x)+(Math.max(x,point.x)-Math.min(x,point.x))/2;
      let Y = Math.min(y,point.y)+(Math.max(y,point.y)-Math.min(y,point.y))/2;
      return {x: X, y: Y};
    }
    const createDot = (obj) => {
      ctx.beginPath();
      ctx.arc(obj.x, obj.y, 1, 0, 2 * Math.PI, false);
      ctx.lineWidth = 1;
      ctx.strokeStyle = '#fc3';
      ctx.stroke();
    }
    
    const pA = {x: canvas.width/2, y: 5};
    const pB = {x: 5, y: canvas.height-5} 
    const pC = {x: canvas.width-5, y: canvas.height-5}
    
    createDot(pA);
    createDot(pB);
    createDot(pC);
    
    const begin = (iterations) => {
      let x = canvas.width/4;
      let y = canvas.height/2;
        for(let i=0;i<iterations;i++) {
        createDot({x, y});
        let randN = GenerateRand();
        if(randN == 1 || randN == 2) {
          const currentDot = updateDot(x, y, pA);
          x = currentDot.x;
          y = currentDot.y;
        }
        else if(randN == 3 || randN == 4) {
          const currentDot = updateDot(x, y, pB);
          x = currentDot.x;
          y = currentDot.y;
        }
        else if(randN == 5 || randN == 6){
          const currentDot = updateDot(x, y, pC);
          x = currentDot.x;
          y = currentDot.y;
        }
      }
    }
    
    let time=0;
    let timer = setInterval(() => {
       if(time >= 500) return clearInterval(timer)
       begin(500);
       time++;
    }, 200);
<div>
      <canvas id="chaos" width="500" height="500"></canvas>
    </div>
    var canvas = document.getElementById('chaos');
    var ctx = canvas.getContext('2d');
    
    const GenerateRand = () => Math.floor(Math.random() * 7);
    const updateDot = (x, y, point) => {
      let X = Math.min(x,point.x)+(Math.max(x,point.x)-Math.min(x,point.x))/2;
      let Y = Math.min(y,point.y)+(Math.max(y,point.y)-Math.min(y,point.y))/2;
      return {x: X, y: Y};
    }
    const createDot = (obj) => {
      ctx.beginPath();
      ctx.arc(obj.x, obj.y, 1, 0, 2 * Math.PI, false);
      ctx.lineWidth = 1;
      ctx.strokeStyle = '#fc3';
      ctx.stroke();
    }
    
    const pA = {x: canvas.width/2, y: 5};
    const pB = {x: 5, y: canvas.height-5} 
    const pC = {x: canvas.width-5, y: canvas.height-5}
    
    createDot(pA);
    createDot(pB);
    createDot(pC);
    
    const begin = (iterations) => {
      let x = canvas.width/4;
      let y = canvas.height/2;
        for(let i=0;i<iterations;i++) {
        createDot({x, y});
        let randN = GenerateRand();
        if(randN == 1 || randN == 2) {
          const currentDot = updateDot(x, y, pA);
          x = currentDot.x;
          y = currentDot.y;
        }
        else if(randN == 3 || randN == 4) {
          const currentDot = updateDot(x, y, pB);
          x = currentDot.x;
          y = currentDot.y;
        }
        else if(randN == 5 || randN == 6){
          const currentDot = updateDot(x, y, pC);
          x = currentDot.x;
          y = currentDot.y;
        }
      }
    }
    
    let time=0;
    let timer = setInterval(() => {
       if(time >= 500) return clearInterval(timer)
       begin(500);
       time++;
    }, 200);
<div>
      <canvas id="chaos" width="500" height="500"></canvas>
    </div>
var canvas = document.getElementById('chaos');
var ctx = canvas.getContext('2d');

const GenerateRand = () => Math.floor(Math.random() * 7);
const updateDot = (x, y, point) => {
  let X = Math.min(x,point.x)+(Math.max(x,point.x)-Math.min(x,point.x))/2;
  let Y = Math.min(y,point.y)+(Math.max(y,point.y)-Math.min(y,point.y))/2;
  return {x: X, y: Y};
}
const createDot = (obj) => {
  ctx.beginPath();
  ctx.arc(obj.x, obj.y, 1, 0, 2 * Math.PI, false);
  ctx.lineWidth = 1;
  ctx.strokeStyle = '#fc3';
  ctx.stroke();
}

const pA = {x: canvas.width/2, y: 5};
const pB = {x: 5, y: canvas.height-5} 
const pC = {x: canvas.width-5, y: canvas.height-5}

createDot(pA);
createDot(pB);
createDot(pC);

const begin = (iterations) => {
  let x = canvas.width/4;
  let y = canvas.height/2;
    for(let i=0;i<iterations;i++) {
    createDot({x, y});
    let randN = GenerateRand();
    if(randN == 1 || randN == 2) {
      const currentDot = updateDot(x, y, pA);
      x = currentDot.x;
      y = currentDot.y;
    }
    else if(randN == 3 || randN == 4) {
      const currentDot = updateDot(x, y, pB);
      x = currentDot.x;
      y = currentDot.y;
    }
    else if(randN == 5 || randN == 6){
      const currentDot = updateDot(x, y, pC);
      x = currentDot.x;
      y = currentDot.y;
    }
  }
}

let time=0;
let timer = setInterval(() => {
   if(time >= 500) return clearInterval(timer)
   begin(500);
   time++;
}, 200);
<div>
      <canvas id="chaos" width="500" height="500"></canvas>
</div>
edited body; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Sierpinski’s Gasket Triangle in javascriptJavaScript

I wrote the Sierpinski’s Gasket Triangle in javascriptJavaScript, but iI feel the code can be better, especially from L32 to L47,. Could you make it more organized?

Sierpinski’s Gasket Triangle in javascript

I wrote the Sierpinski’s Gasket Triangle in javascript, but i feel the code can be better, especially from L32 to L47, Could you make it more organized?

Sierpinski’s Gasket Triangle in JavaScript

I wrote the Sierpinski’s Gasket Triangle in JavaScript, but I feel the code can be better, especially from L32 to L47. Could you make it more organized?

Source Link

Sierpinski’s Gasket Triangle in javascript

I wrote the Sierpinski’s Gasket Triangle in javascript, but i feel the code can be better, especially from L32 to L47, Could you make it more organized?

    var canvas = document.getElementById('chaos');
    var ctx = canvas.getContext('2d');
    
    const GenerateRand = () => Math.floor(Math.random() * 7);
    const updateDot = (x, y, point) => {
      let X = Math.min(x,point.x)+(Math.max(x,point.x)-Math.min(x,point.x))/2;
      let Y = Math.min(y,point.y)+(Math.max(y,point.y)-Math.min(y,point.y))/2;
      return {x: X, y: Y};
    }
    const createDot = (obj) => {
      ctx.beginPath();
      ctx.arc(obj.x, obj.y, 1, 0, 2 * Math.PI, false);
      ctx.lineWidth = 1;
      ctx.strokeStyle = '#fc3';
      ctx.stroke();
    }
    
    const pA = {x: canvas.width/2, y: 5};
    const pB = {x: 5, y: canvas.height-5} 
    const pC = {x: canvas.width-5, y: canvas.height-5}
    
    createDot(pA);
    createDot(pB);
    createDot(pC);
    
    const begin = (iterations) => {
      let x = canvas.width/4;
      let y = canvas.height/2;
        for(let i=0;i<iterations;i++) {
        createDot({x, y});
        let randN = GenerateRand();
        if(randN == 1 || randN == 2) {
          const currentDot = updateDot(x, y, pA);
          x = currentDot.x;
          y = currentDot.y;
        }
        else if(randN == 3 || randN == 4) {
          const currentDot = updateDot(x, y, pB);
          x = currentDot.x;
          y = currentDot.y;
        }
        else if(randN == 5 || randN == 6){
          const currentDot = updateDot(x, y, pC);
          x = currentDot.x;
          y = currentDot.y;
        }
      }
    }
    
    let time=0;
    let timer = setInterval(() => {
       if(time >= 500) return clearInterval(timer)
       begin(500);
       time++;
    }, 200);
<div>
      <canvas id="chaos" width="500" height="500"></canvas>
    </div>