Thursday, October 20, 2022

ASMR Programming - Python Turtle 02

 Tinkering Turtle Graphics

Creating Polygon and Stars




Code:
import turtle as t
from math import sin,cos,radians

t.setup(800,800)
t.bgcolor('black')


def ngon(n,r,sa):
    '''
    POLYGON
    '''
    ang = 360/n
    p = t.pos()
    t.up()
    for i in range(n+1):
        a=radians(i*ang+sa)
        x=p[0]+cos(a)*r
        y=p[1]+sin(a)*r
        t.goto(x,y)
        t.down()
    t.up()
    t.goto(p)

def star(n,r1,r2,sa):
    '''
    STAR
    '''
    ang = 360/(n*2)
    p=t.pos()
    t.up()
    for i in range(n*2+1):
        a=radians(i*ang+sa)
        x=p[0]+cos(a)*(r2 if i%2==0 else r1)
        y=p[0]+sin(a)*(r2 if i%2==0 else r1)
        t.goto(x,y)
        t.down()
    t.up()
    t.goto(p)

def starx(n,r,sa):
    '''
    STAR CROSSING
    '''
    ang=720/n
    p=t.pos()
    t.up()
    for i in range(n+1):
        a=radians(i*ang+sa)
        x=p[0]+cos(a)*r
        y=p[0]+sin(a)*r
        t.goto(x,y)
        t.down()
    t.up()
    t.goto(p)


t.pencolor('white')
ngon(3,150,90)
t.pencolor('white')
star(10,100,200,90)
t.pencolor('white')
starx(7,50,90) #--- needs more work..


t.done()


Keyboard I use:


No comments:

Post a Comment

Python Turtle Graphics Drawing - Gigabyte

 Python Turtle Graphics Drawing - Gigabyte Youtube Source code: from turtle import * setup ( 600 , 600 ) color ( '#2D68AE' ) up (...