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:
enter image description here
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→L1
Repeat getKey
Ans→L2
Pxl-Change(L1(1),L1(2
Ans+L1→L1
L2*(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:
- How can I reduce the size of this program?
- How can I increase the speed of this program?
- How can I reduce the numbers of variables used in this program?
- 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.
3 Answers 3
On my TI-82, I get an ERR:DATA TYPE
on the following line:
L2*(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→L1
L1→L2
Repeat getKey
Pxl-Change(L1(1),L1(2
L2+2(L1={0,0})-2(L1={62,94}→L2
L1+L2→L1
End
-
1\$\begingroup\$ 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. \$\endgroup\$ankh-morpork– ankh-morpork2015年03月01日 12:56:41 +00:00Commented Mar 1, 2015 at 12:56 -
\$\begingroup\$ 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. \$\endgroup\$Timtech– Timtech2015年05月25日 01:15:46 +00:00Commented May 25, 2015 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(πrnot(Ans and Ans≠{62,94
End
Finally, if your calculator is in radian mode, you can save one more byte by removing the r
.
-
\$\begingroup\$ How many bytes would that save? \$\endgroup\$200_success– 200_success2015年05月16日 23:41:30 +00:00Commented May 16, 2015 at 23:41
-
\$\begingroup\$ @200_success 8 max \$\endgroup\$Timtech– Timtech2015年05月25日 01:10:10 +00:00Commented May 25, 2015 at 1:10
The program works great on my TI-84+. This answer will focus on the main loop:
{1,1→L1
Repeat getKey
Ans→L2
Pxl-Change(L1(1),L1(2
Ans+L1→L1
L2*(1-2not(Ans and Ans≠{62,94
End
- I spent way too long trying to decrease the size. First I thought you could eliminate the
*
, then I figured you could move→L2
to the long line and eliminate the third line. Then I thought you could even eliminateL2
completely. But they all don't work :( - You can increase the speed through size:speed proportion of this program by using a solution like Thomas's, with real( and imag(.
- Using one complex list is going to be faster than using two simple ones.
- You can't ;) this code is too good.
-
\$\begingroup\$ Hm, I didn't know my complex-list solution would be faster. \$\endgroup\$lirtosiast– lirtosiast2015年05月24日 21:15:16 +00:00Commented May 24, 2015 at 21:15
-
\$\begingroup\$ At least it's faster by proportion. \$\endgroup\$Timtech– Timtech2015年05月25日 01:09:32 +00:00Commented May 25, 2015 at 1:09
L2*(1-2not(Ans and Ans≠{62,94
— it doesn't do anything. \$\endgroup\$Ans
Variable, so that line meansL2*(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 lineAns→L2
. Clearly this does something. As a matter of fact, this line performs the bounds checks that stop the bouncing pixel from leaving the screen. \$\endgroup\$