TI-84 Plus CE Python: Drawing Bars
My inspiration for this post was from a class I was taking at brilliant.org. A free plug: brillant.org is a great web service that offers easy to follow and interactive classes in mathematics, physics, and programming. Classes are offered at every level.
Introduction
The following set of scripts draw a set of bars. The blue bar is the base bar, while orange bars are added to the right of the base bar. The user specifies the length of the base (b) and orange bars, known as the increment (c). The length is in pixels. The screen is 320 pixels long.
Each of the scripts uses the TI-specific module TI-draw module. If you have another calculator or platform, another similar drawing module is needed. The scripts were typed on a TI-84 Plus CE Python Edition, but should work on the TI-83 Premium CE Python Edition and TI-Nspire CX II (I haven’t tested either).
BAR1: Static
This script asks the user for the length of the base bar, increment bar, and the number of bars.
Note that show_draw() command end the execution of the script with the drawing on the screen. Press [clear] to exit the screen.
# static bar
from ti_draw import *
print("Positive
Integers Only")
b=int(input("base?
"))
c=int(input("increment? "))
n=int(input("#
of bars? "))
# total
t=b+c*n
#
draw
clear()
#
base
set_color(0,120,245)
fill_rect(0,80,b,40)
#
increment
set_color(255,135,10)
for i in range(n):
fill_rect(b+i*c,80,c,40)
#
text
set_color(0,0,0)
draw_text(0,160,"Total:
"+str(t)+" = "+str(b)+" + "+str(n)+" *
"+str(c))
draw_text(0,180,"Press [clear] to exit.")
#
draw
show_draw()
BAR2: Animate
This script asks the user for the length of the base bar, increment bar, and the number of bars. Only this time the drawing is animated as the number of increment bars is increased from 0 to n.
This script uses another module, time. This is needed for the sleep(s) command, where s is the number of seconds.
# animate bar
from ti_draw import *
from time import
*
print("Positive Integers Only")
b=int(input("base?
"))
c=int(input("increment? "))
n=int(input("#
of bars? "))
# total
t=b+c*n
# range
starts at 0
for i in range(n+1):
clear()
# base
set_color(0,120,245)
fill_rect(0,80,b,40)
# increment
set_color(255,135,10)
for j in range(i):
fill_rect(b+j*c,80,c,40)
t=b+c*i
# text
set_color(0,0,0)
draw_text(0,160,"Total: "+str(t)+"
= "+str(b)+" + "+str(n)+" * "+str(c))
# draw
sleep(0.5)
# for the screen to stay on the
bars at the end
set_color(255,0,0)
draw_text(0,180,"Press
[clear] to exit.")
show_draw()
BAR3: Control
Instead of giving a number of increment bars, the user controls the number of bars by pressing the right [ → ] and left [ ← ] keys. Exit by pressing the [ enter ] key.
This script uses the ti_system module. This allows for the wait_key() command, which stops execution until a key is pressed.
Key codes for TI-84 Plus CE Python and TI-83 CE Premium Python Edition:
[ → ]: right key, code 1
[ ← ]: left key, code 2
[ enter ]: enter key, code 5
# bar with get key
from ti_draw import *
from ti_system
import *
def drawsub(b,c,n):
# total
t=b+c*n
# draw
clear()
# base
set_color(0,120,245)
fill_rect(0,80,b,40)
set_color(255,135,10)
for i in
range(n):
fill_rect(b+i*c,80,c,40)
set_color(0,0,0)
draw_text(0,160,"Total: "+str(t)+" = "+str(b)+"
+ "+str(n)+" * "+str(c))
draw_text(0,180,"<-
or ->, [enter] to quit")
print("Positive
Integers Only")
b=int(input("base?
"))
c=int(input("increment? "))
print("Press
<- or ->")
# default value of n
n=0
#
max number of incr
m=int((320-b)/c)
# key
k=0
while
k!=5:
k=wait_key()
# left key
if k==2 and n>0:
n-=1
# right key
if k==1 and n<m:
n+=1
drawsub(b,c,n)
set_color(255,0,0)
draw_text(0,20,"DONE")
show_draw()
Download the scripts here: https://drive.google.com/file/d/1SKCXBm6lYi5cYurm7nqAqI-868lxvqpz/view?usp=sharing
Until next time and in good health,
Eddie
All original content copyright, © 2011-2024. 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.