Ensaio sobre π: 4

2023-04-11

"""2023-04-10"""
from helpers import HEIGHT
from helpers import save_image
from helpers import WIDTH
from helpers import write_legend
import py5
from pathlib import Path


IMG_NAME = Path(__file__).name.replace(".py", "")


# Reference https://decimal.info/digits-of-pi/value-of-pi-to-2120-decimal-places.html
PI = "3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593344612847564823378678316527120190914564856692346034861045432664821339360726024914127372458700660631558817488152092096282925409171536436789259036001133053054882046652138414695194151160943305727036575959195309218611738193261179310511854807446237996274956735188575272489122793818301194912983367336244065664308602139494639522473719070217986094370277053921717629317675238467481846766940513200056812714526356082778577134275778960917363717872146844090122495343014654958537105079227968925892354201995611212902196086403441815981362977477130996051870721134999999837297804995105973173281609631859502445945534690830264252230825334468503526193118817101000313783875288658753320838142061717766914730359825349042875546873115956286388235378759375195778185778053217122680661300192787661119590921642019893809525720106548586327886593615338182796823030195203530185296899577362259941389124972177528347913151557485724245415069595082953311686172785588907509838175463746493931925506040092770167113900984882401285836160356370766010471018194295559619894676783744944825537977472684710404753464620804668425906949129331367702898915210475216205696602405803815019351125338243003558764024749647326391419927260426992279678235478163600934172164121992458631503028618297455570674983850549458858692699569092721079750930295532116534498720275596023648066549911988183479775356636980742654252786255181841757467289097777279380008164706001614524919217321721477235014144197356854816136115735255213347574184946843852332390739414333454776241686251898356948556209921922218427255025425688767179049460165346680498862723279178608578438382796797668145410095388378636095068006422512520511739298489608412848862694560424196528502221066118630674427862203919494504712371378696095636437191728746776465757396241389086583264599581339047802759009946576407895126946839835259570982582262052248940772671947826848260147699090264013639443745530506820349625245174939965143"  # noQA

# Reference https://carbondesignsystem.com/data-visualization/color-palettes/
PALETTE = [
    "#f6f2ff",
    "#e8daff",
    "#d4bbff",
    "#be95ff",
    "#a56eff",
    "#8a3ffc",
    "#6929c4",
    "#491d8b",
    "#31135e",
    "#1c0f30",
]


def settings():
    py5.size(WIDTH, HEIGHT)


def setup():
    fundo()
    pi_circle()
    write_text()
    write_legend(PALETTE, IMG_NAME)
    write_table(PALETTE)
    save_image(IMG_NAME)


def fundo():
    py5.background("#f1ee8e")

def write_text():
    py5.fill(PALETTE[-1])
    py5.text_size(200)
    py5.text_align(py5.CENTER)
    py5.text("π", 80, 120)
    # First circle
    py5.fill("#000")
    py5.text_size(24)
    py5.text_align(py5.CENTER)
    py5.text("3", WIDTH / 2, HEIGHT / 2 + 5)

def write_table(palette):
    x = 10
    y = 650
    py5.text_size(12)
    py5.text_align(py5.CENTER)
    py5.stroke("#000")
    for idx, color in enumerate(palette):
        py5.fill(color)
        py5.circle(x, y, 10)
        py5.fill("#000")
        py5.text(idx, x + 15, y + 3)
        y += 15


def pi_circle():
    py5.no_stroke()
    with py5.push_matrix():
        angulo = 10
        py5.translate(WIDTH / 2, HEIGHT / 2)
        for i in range(0, len(PI)):
            if i == 0:
                d = 30
                radius = 0
            elif i == 1:
                d = 5
                radius = -20
            char = PI[i]
            digit = 0 if char == "." else int(char)
            py5.fill(PALETTE[digit])
            if i % 6 == 0 and radius > -350:
                radius = radius - 1
            if i % 360 == 0 and angulo > 2:
                angulo -= 1
                d += 1
            if radius > -350:
                py5.circle(0, radius, d)
                py5.rotate(py5.radians(angulo))



py5.run_sketch()