DOCTYPE html html lang en head meta charset UTF 8 meta name viewport content width device width initial scale 1 0 title Chaos Game Fractal title style canvas border 1px solid black style head body canvas id chaosCanvas width 400 height 400 canvas script Get canvas and context const canvas document getElementById chaosCanvas const ctx canvas getContext 2d Define triangle vertices const vertices x 200 y 50 x 50 y 350 x 350 y 350 Function to get a random vertex function getRandomVertex return vertices Math floor Math random vertices length Function to draw a point function drawPoint point ctx fillStyle black ctx fillRect point x point y 1 1 Function to generate chaos game fractal function generateChaosGame iterations let point x Math random canvas width y Math random canvas height for let i 0 i iterations i Choose a random vertex const vertex getRandomVertex Move halfway to the chosen vertex point x point x vertex x 2 point y point y vertex y 2 Draw the point drawPoint point Call generateChaosGame to start the fractal generation generateChaosGame 10000 You can adjust the number of iterations as needed script body html DOCTYPE html html lang en head meta charset UTF 8 meta name viewport content width device width initial scale 1 0 title Chaos Game Fractal title style canvas border 1px solid black style head body script Define polygon vertices pentagon const pentagonVertices x 200 y 50 x 100 y 150 x 150 y 300 x 250 y 300 x 300 y 150 Define polygon vertices hexagon const hexagonVertices x 200 y 50 x 100 y 150 x 100 y 250 x 200 y 350 x 300 y 250 x 300 y 150 Function to get a random vertex function getRandomVertex vertices return vertices Math floor Math random vertices length Function to draw a point function drawPoint ctx point ctx fillStyle black ctx fillRect point x point y 1 1 Function to generate chaos game fractal function generateChaosGame vertices iterations const canvas document createElement canvas document body appendChild canvas const ctx canvas getContext 2d canvas width canvas height 400 document body appendChild document createElement br let point x Math random canvas width y Math random canvas height for let i 0 i iterations i Choose a random vertex const vertex getRandomVertex vertices Move halfway to the chosen vertex point x point x vertex x 2 point y point y vertex y 2 Draw the point drawPoint ctx point Call generateChaosGame to start the fractal generation for pentagon and hexagon generateChaosGame pentagonVertices 50000 You can adjust the number of iterations as needed generateChaosGame hexagonVertices 50000 You can adjust the number of iterations as needed script body html DOCTYPE html html lang en head meta charset UTF 8 meta name viewport content width device width initial scale 1 0 title Chaos Game Fractal title style canvas border 1px solid black style head body script Define polygon vertices heptagon const heptagonVertices x 200 y 50 x 130 y 100 x 100 y 190 x 150 y 280 x 250 y 280 x 300 y 190 x 270 y 100 Define polygon vertices octagon const octagonVertices x 200 y 50 x 130 y 100 x 100 y 190 x 130 y 280 x 200 y 330 x 270 y 280 x 300 y 190 x 270 y 100 Define polygon vertices nonagon const nonagonVertices x 200 y 50 x 145 y 85 x 115 y 150 x 115 y 230 x 145 y 295 x 200 y 330 x 255 y 295 x 285 y 230 x 285 y 150 Define polygon vertices decagon const decagonVertices x 200 y 50 x 155 y 75 x 120 y 125 x 100 y 190 x 100 y 260 x 120 y 325 x 155 y 375 x 200 y 400 x 245 y 375 x 280 y 325 x 300 y 260 x 300 y 190 x 280 y 125 x 245 y 75 Function to get a random vertex function getRandomVertex vertices return vertices Math floor Math random vertices length Function to draw a point function drawPoint ctx point ctx fillStyle black ctx fillRect point x point y 1 1 Function to generate chaos game fractal function generateChaosGame vertices iterations const canvas document createElement canvas canvas width canvas height 400 document body appendChild canvas const ctx canvas getContext 2d let point x Math random canvas width y Math random canvas height for let i 0 i iterations i Choose a random vertex const vertex getRandomVertex vertices Move halfway to the chosen vertex point x point x vertex x 2 point y point y vertex y 2 Draw the point drawPoint ctx point Call generateChaosGame to start the fractal generation for each polygon generateChaosGame heptagonVertices 50000 generateChaosGame octagonVertices 50000 generateChaosGame nonagonVertices 50000 generateChaosGame decagonVertices 99000 script body html DOCTYPE html html lang en head meta charset UTF 8 meta name viewport content width device width initial scale 1 0 title Generalized Chaos Game title style canvas border 1px solid black style head body canvas id chaosCanvas width 400 height 400 canvas script function dividingRateFiser n return Math pow 1 Math sin Math PI n 1 function dividingRateAlmeida n const k Math round n 2 4 const s_num 2 Math cos Math PI 1 2 k n 2 Math cos Math PI 1 2 1 2 n Math cos 2 k 1 Math PI 2 n 1 Math tan 2 k 1 Math PI 2 n Math tan Math PI n 2 k 2 Math PI 2 n const s_den 2 Math cos Math PI 1 2 k n return s_num s_den function generalizedCGR N divide_func n start let shape_coord if start shape_coord n_gon n start 0 else shape_coord n_gon n const r divide_func n const dataPoints Array from length 2 Array N fill 0 for let i 1 i N i const rand Math random for let j 0 j 2 j dataPoints j i dataPoints j i 1 shape_coord j Math floor n rand dataPoints j i 1 r return r shape_coord dataPoints function n_gon n start x 0 y 0 const angle 2 Math PI n const coordinates Array from length 2 Array n fill x 0 y 0 for let i 0 i n i const x start x Math cos angle i const y start y Math sin angle i coordinates 0 i x coordinates 1 i y return coordinates const canvas document getElementById chaosCanvas const ctx canvas getContext 2d const r shape_coord dataPoints generalizedCGR 4000 dividingRateAlmeida 8 Draw the shape ctx beginPath ctx moveTo shape_coord 0 0 shape_coord 1 0 for let i 1 i shape_coord 0 length i ctx lineTo shape_coord 0 i shape_coord 1 i ctx closePath ctx stroke Draw the data points for let i 0 i dataPoints 0 length i ctx fillRect 200 dataPoints 0 i 200 200 dataPoints 1 i 200 0 1 1 script body html
nVertices x 200 y 50 x 145 y 85 x 115 y 150 x 115 y 230 x 145 y 295 x 200 y 330 x 255 y 295 x 285 y 230 x 285 y 150 Define polygon vertices decagon const decagonVertices x 200 y 50 x 155 y 75 x 120 y 125 x 100 y 190 x 100 y 260 x 120 y 325 x 155 y 375 x 200 y 400 x 245 y 375 x 280 y 325 x 300 y 260 x 300 y 190 x 280 y 125 x 245 y 75 Function to get a random vertex function getRandomVertex vertices return vertices Math floor Math random vertices length Function to draw a point function drawPoint ctx point ctx fillStyle black ctx fillRect point x point y 1 1 Function to generate chaos game fractal function generateChaosGame vertices iterations const canvas document createElement canvas canvas width canvas height 400 document body appendChild canvas const ctx canvas getContext 2d let point x Math random canvas width y Math random canvas height for let i 0 i iterations i Choose a random vertex const vertex getRandomVertex vertices Move halfway to the chosen vertex point x point x vertex x 2 point y point y vertex y 2 Draw the point drawPoint ctx point Call generateChaosGame to start the fractal generation for each polygon generateChaosGame heptagonVertices 50000 generateChaosGame octagonVertices 50000 generateChaosGame nonagonVertices 50000 generateChaosGame decagonVertices 99000 script body html DOCTYPE html html lang en head meta charset UTF 8 meta name viewport content width device width initial scale 1 0 title Generalized Chaos Game title style canvas border 1px solid black style head body canvas id chaosCanvas width 400 height 400 canvas script function dividingRateFiser n return Math pow 1 Math sin Math PI n 1 function dividingRateAlmeida n const k Math round n 2 4 const s_num 2 Math cos Math PI 1 2 k n 2 Math cos Math PI 1 2 1 2 n Math cos 2 k 1 Math PI 2 n 1 Math tan 2 k 1 Math PI 2 n Math tan Math PI n 2 k 2 Math PI 2 n const s_den 2 Math cos Math PI 1 2 k n return s_num s_den function generalizedCGR N divide_func n start let shape_coord if start shape_coord n_gon n start 0 else shape_coord n_gon n const r divide_func n const dataPoints Array from length 2 Array N fill 0 for let i 1 i N i const rand Math random for let j 0 j 2 j dataPoints j i dataPoints j i 1 shape_coord j Math floor n rand dataPoints j i 1 r return r shape_coord dataPoints function n_gon n start x 0 y 0 const angle 2 Math PI n const coordinates Array from length 2 Array n fill x 0 y 0 for let i 0 i n i const x start x Math cos angle i const y start y Math sin angle i coordinates 0 i x coordinates 1 i y return coordinates const canvas document getElementById chaosCanvas const ctx canvas getContext 2d const r shape_coord dataPoints generalizedCGR 4000 dividingRateAlmeida 8 Draw the shape ctx beginPath ctx moveTo shape_coord 0 0 shape_coord 1 0 for let i 1 i shape_coord 0 length i ctx lineTo shape_coord 0 i shape_coord 1 i ctx closePath ctx stroke Draw the data points for let i 0 i dataPoints 0 length i ctx fillRect 200 dataPoints 0 i 200 200 dataPoints 1 i 200 0 1 1 script body html