#
# PYTHON • MyCompiler
# Fractais Poligonais
# https://[Log in to view URL]
# Fractais Poligonais
# 
# 2026 Julho Seg, 20 — Qua, 22
# by 
# j
# 

import random
import matplotlib.pyplot as plt

import numpy as np

from numpy import pi
from numpy import sin
from numpy import cos

#
# W A R N I N G 
#
# Para obter gráficos com alta qualidade
# é necessário configurar a variável MAX 
# para valores altos como 5000. Use,
# por exemplo, a chamada 
#
# graf(6,5000)
#
# No entanto haverá a necessidade de 
# mudar a ambiência de execução deste 
# código python para evitar o estouro 
# de tempo.
#
# To obtain high-quality graphs,
# you need to set the MAX variable 
# to a high value, such as 5000. Use,
# for example, the call 
# 
# graf(6,5000)
# 
# However, you will need to 
# change the execution environment for 
# this Python code to avoid a timeout.
#

def graf(n,MAX):

    plt.style.use('dark_background')
    plt.figure(figsize=(7,7))
        
    cx = []
    cy = []

    PX  = []
    PY  = []

    R = 10
    
    konst = (2*pi)/n
    
    delta = 0

    for j in range(0,n):

        cx.append(R*cos(j*konst+delta))
        cy.append(R*sin(j*konst+delta))

    for j in range(0,n-1):
        
        # plt.scatter(cx[j],cy[j], s=20, alpha=1, color="blue")

        if (j < n):
            
            PX.append([cx[j],cx[j+1]])
            PY.append([cy[j],cy[j+1]])

    PX.append([cx[0],cx[n-1]])
    PY.append([cy[0],cy[n-1]])
    
    for j in range(0,n):
        
        plt.plot(PX[j],PY[j], linewidth=0.5, color="blue")

    PA = []
    PB = []
    PC = []
    PD = []
    
    i = 0
    
    while ( i < MAX ):

       if ( i == 0 ):

          #
          # Criação da semente
          #
           
          FX = random.randint(0,100)
          FY = random.randint(0,100)
           
          x = 2.5*FX/100
          y = 2.5*FY/100

          plt.scatter(x,y, s=20, alpha=1, color="red")

       else:

          alpha = random.randint(0,n-1)
        
          beta  = alpha + 1

          if (beta > n-1):

             beta = 0

          x = (1/3)*(x + cx[alpha] + cx[beta] )
          y = (1/3)*(y + cy[alpha] + cy[beta] )

          plt.scatter(x,y, s=1, alpha=1, color="orange")

       i += 1
        
    tit = 'Fractal Poligonal'

    plt.axis('equal') 
    plt.title(tit)
    plt.xticks([])  
    plt.yticks([])
    plt.grid(False)
    plt.show()

graf(7,300)

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: