Saturday, April 22, 2023

Python: Drawing Regular Polygons (TI-84 CE Python, Numworks)

Python:  Drawing Regular Polygons (TI-84 CE Python, Numworks)



Introduction



The following scripts uses the Turtle module to draw regular polygons.   It turns out that every set of commands is slightly different in every calculator.  


For instance, there are no color names in the TI-84 Plus CE Python but they are present in Numworks.  


Also, the turtle module needs a t. (t-point) suffix of all turtle commands for the TI-84 Plus CE Python.  The Numworks turtle module does not need a suffix. 



TI-84 Plus CE Python:  dpolygon.py


from turtle import *

t=Turtle()


# initialization

s=60

n=int(input("# of sides? "))


# angle

a=180*(n-2)/n


# set turtle for TI-84

# color

k=-1

while k<0  or  k>4:

  print("Choose Color")

  print("0. blue")

  print("1. red")

  print("2. green")

  print("3. orange")

  print("4. indigo")

  k=int(input())

if k==0:

  t.pencolor(0,0,255)

if k==1:

  t.pencolor(255,0,0)

if k==2:

  t.pencolor(0,192,0)

if k==3:

  t.pencolor(255,165,0)

if k==4:

  t.pencolor(75,0,130)


t.clear()


# setup

t.penup()

t.left(180)

t.forward(60)

t.left(90)

t.forward(60)

t.left(90)


# draw the polygon

t.pendown()

for i in range(n):

  if n>6:

    t.forward(s/2)

  else:

    t.forward(s)

  t.left(180-a)

t.done()




Numworks Python:  dpythonnw.py


from math import *

from turtle import *


# initialization

s=60

n=int(input("# of sides? "))


# angle

a=180*(n-2)/n


# set color numworks

k=-1

l=['blue','red','green','orange','purple']

while k<0 or k>4:

  print("Choose Color")

  print("0. blue")

  print("1. red")

  print("2. green")

  print("3. orange")

  print("4. purple")

  k=int(input())

col=l[k]


# setup

penup()

left(180)

forward(60)

left(90)

forward(60)

left(90)


# draw the polygon

pendown()

color(col)

for i in range(n):

  if n>6:

    forward(s/2)

  else:

    forward(s)

  left(180-a)




Until next time,


Eddie 


All original content copyright, © 2011-2023.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author. 


Spotlight: Akron Brass FireCalc Pocket Computer

Spotlight: Akron Brass FireCalc Pocket Computer Welcome to a special Monday Edition of Eddie’s Math and Calculator blog. Thi...