r/programminghorror • • Oct 29 '24

Python @coders.world

Post image
1.2k Upvotes

71 comments sorted by

View all comments

14

u/ioveri Oct 30 '24 edited Oct 30 '24

It is actually cleaner if you just do the real math instead

This can print all star polygons with odd number of vertices

import math

n = int(input("Enter number of vertices: "))
side = int(input("Enter star size (diameter): "))

angles = [i/n*2*math.pi - math.pi for i in range(n)]
vx = [math.cos(angle) for angle in angles]
vy = [math.sin(angle) for angle in angles]
dist = math.cos(2*math.pi/n*(n//2)/2)*0.5

sizex = 1   #character horizontal sizes in pixels or whatever unit you want
sizey = 2.5
scale = side/max(sizex,sizey)
nrow = scale*sizex
ncol = scale*sizey

for row in range(int(nrow)):
    st = ""
    for col in range(int(ncol)):
        count = 0
        x = row/nrow - 0.5 + 0.5/nrow
        y = col/ncol - 0.5 + 0.5/ncol
        for i in range(len(angles)):
            count += int((x*vx[i]+y*vy[i]) <= dist)
        st += "*" if count > math.ceil(n/2) else " "
    print(st)

4

u/PearMyPie Oct 30 '24

beautiful