# TI-Basic Bouncing Ball Animation

Anyone who has programmed in TI-Basic is likely to have written one of these programs. The concept for this program is simple.

The "ball" represented by a single point that originates from the upper left hand corner of the screen and bounces around the screen leaving a trail as it travels. The initial movement vector for the ball is (1,1), meaning one down, one right. During each pass through a loop, the "ball" is translated by the amount specified in the movement vector. When the ball would go of bounds, its movement vector is adjusted so that this does not happen. In short, it behaves like this:

except I increased the frame of the GIF.

I've reduced my code to a very short and clean snippet:

FnOff
PlotsOff
AxesOff
ClrDraw

{1,1→L₁
Repeat getKey
Ans→L₂
Pxl-Change(L₁(1),L₁(2
Ans+L₁→L₁
L₂*(1-2not(Ans and Ans≠{62,94
End

FnOn
PlotsOn
AxesOn
ClrDraw


What I would like to hear in responses can be enumerated in four points:

1. How can I reduce the size of this program?
2. How can I increase the speed of this program?
3. How can I reduce the numbers of variables used in this program?
4. How can I increase the clarity of this program without sacrificing any of the goals stated above?

People responding to this post should also keep in mind that this code was written for and tested on a TI-83 Plus calculator.

• Please check this line: L₂*(1-2not(Ans and Ans≠{62,94  — it doesn't do anything. – 200_success Mar 1 '15 at 7:00
• @200_success It does, in fact. Due to the nature of TI-Basic the result of every operation is stored into the Ans Variable, so that line means L₂*(1-2not(Ans and Ans≠{62,94→Ans. The next line ends the loop sending the code back to the top of the loop where there is the line Ans→L₂. Clearly this does something. As a matter of fact, this line performs the bounds checks that stop the bouncing pixel from leaving the screen. – ankh-morpork Mar 1 '15 at 12:09
• It didn't work on my TI-82. But I'll reopen the question if you claim it works. – 200_success Mar 1 '15 at 12:10
• @200_success I've tested this on a TI-83 Plus calculator. I'll add that to the post. – ankh-morpork Mar 1 '15 at 12:11

On my TI-82, I get an ERR:DATA TYPE on the following line:

L₂*(1-2not(Ans and Ans≠{62,94


Apparently, the TI-82 does not support operations not and and on lists.

I recommend that you avoid relying on Ans. Stating what you mean explicitly leads to less code that is less fragile and more readable. The following code also has one fewer statement within the loop.

{1,1→L₁
L₁→L₂
Repeat getKey
Pxl-Change(L₁(1),L₁(2
L₂+2(L₁={0,0})-2(L₁={62,94}→L₂
L₁+L₂→L₁
End

• I use the Ans varaibles so much because almost all TI-Basic sources maintain that "Ans is faster than the real, complex, list, matrix, and string variables". I will make the changes that move one line out of the loop. – ankh-morpork Mar 1 '15 at 12:56
• EZ optimizations like replacing L1 with Ans on the 2nd line to save a byte and removing the closing bracked on the long line. You can also reorder the long line to save another byte. Total 3 bytes saved here. – Timtech May 25 '15 at 1:15

This uses no variables other than Ans, but because real( and imag( are two-byte tokens, the code is larger. It is also faster than your code according to Timtech.

i+dim(identity(1                    //{1+i,1+i}
Repeat getKey
Pxl-Change(real(Ans(1)),real(Ans(2
Ans+imag(Ans)(1-2inot(real(Ans) and real(Ans)≠{62,94
End


In your original solution, you can thusly save a total of six bytes at the cost of a small amount of speed, and the use of named lists. Two bytes less than that is a program that starts at (2,2) instead of (1,1) because command order is switched:

dim(identity(1→A                    //{1,1
Repeat getKey
Ans→B
Ans+∟A→A
Pxl-Change(Ans(1),Ans(2
∟Bcos(πʳnot(Ans and Ans≠{62,94
End


Finally, if your calculator is in radian mode, you can save one more byte by removing the ʳ.

• How many bytes would that save? – 200_success May 16 '15 at 23:41
• @200_success 8 max – Timtech May 25 '15 at 1:10

The program works great on my TI-84+. This answer will focus on the main loop:

{1,1→L₁
Repeat getKey
Ans→L₂
Pxl-Change(L₁(1),L₁(2
Ans+L₁→L₁
L₂*(1-2not(Ans and Ans≠{62,94
End

1. I spent way too long trying to decrease the size. First I thought you could eliminate the *, then I figured you could move →L₂ to the long line and eliminate the third line. Then I thought you could even eliminate L₂ completely. But they all don't work :(
2. You can increase the speed through size:speed proportion of this program by using a solution like Thomas's, with real( and imag(.
3. Using one complex list is going to be faster than using two simple ones.
4. You can't ;) this code is too good.
• Hm, I didn't know my complex-list solution would be faster. – lirtosiast May 24 '15 at 21:15
• At least it's faster by proportion. – Timtech May 25 '15 at 1:09