Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>sierpinksi polygons</title>
  5. </head>
  6. <body>
  7. <canvas width="500" height="500" id="canvas"></canvas>
  8. <script>
  9. let canavas = document.getElementById("canvas");
  10. let ctx = canvas.getContext("2d");
  11. let imgdata = ctx.createImageData(500, 500);
  12. let data = imgdata.data;
  13. let vertices = [{x: 0, y: 0}, {x: 500, y: 0}, {x: 250, y: 433}];
  14.  
  15. function pixel(x, y) {
  16. data[4*y*canvas.width+4*x + 3] = 1; //sets alpha to 1 which makes the pixel black
  17. }
  18.  
  19. function random() {
  20. return vertices[Math.floor(Math.random()*vertices.length)];
  21. }
  22.  
  23. let point = {x: 0, y: 0};
  24.  
  25. for (let i = 0; i < 10000; i++) {
  26. let temp = random();
  27.  
  28. //algorithm to draw sierpinksi triangle pixel by pixel
  29. point.x = point.x + (temp.x - point.x)/2;
  30. point.y = point.y + (temp.y - point.y)/2;
  31. pixel(Math.round(point.x), Math.round(point.y));
  32. }
  33. console.log(imgdata)
  34. ctx.putImageData(imgdata, 0, 0);
  35. </script>
  36. </body>
  37. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement