Here's a home screen drawing program I wrote. You can toggle whether or not you interact with the drawing by pressing ENTER, and the plus and minus keys change between pen and eraser. It actively stores your drawing in a list, so when you re-open the program it'll load it up.


(It's a bit laggy in the gif, but that's just how jsTIfied's recording thing works.)

    Updated menu to work with arrow keys as well as number keys. 10/7/15
    Changed to custom menus for speed. 10/7/15
    Added compatibility with all TI-83/84 calculators. 10/6/15
    Minor performance improvements. 11/22/15

If you could help me optimize my program, that'd be awesome. Post your suggestions down below. (I need an explanation of what you're doing, so I can learn.)
Main issue: It's not very fast when I switch draw/erase and pen down/up.


Code:
ClrHome
2->dim(|LSCRN
If not(|LSCRN(1
Then
   Disp "CALCULATOR MODEL
   Output(2,1," TI-83/84/+
   Output(3,1," TI-84+ Color
   2->S
   0->C
   Repeat C and max(K={92,93,24,34,105
      getKey->K
      If Ans
      Output(S,1,"
      min(3,max(2,S+sum(DeltaList(K={25,34->S
      Output(S,1,"[|>]
      If K=105
      90+S->K
      If K=92:Then
         16->|LSCRN(1
         8->|LSCRN(2
         1->C
      End
      If K=93:Then
   26->|LSCRN(1
   10->|LSCRN(2
         1->C
      End
   End
End
|LSCRN(1->W
|LSCRN(2->H
prod(|LSCRN->dim(|LDRWDB
Lbl 0
ClrHome
"#->Str3
"+->Str2
1->Y
   0->D
1->M
1->I
W-7->J
Output(H,J,"Loading...
For(Y,1,H
   For(X,1,W
      If |LDRWDB(I):Then
         Output(Y,X,Str3
         Else
         Output(Y,X,"
      End
      I+1->I
   End
End
8->X
4->Y
Lbl 1
Repeat max(K={21,23,45
   (Y-1)W+X->I
   getKey->K
   If Ans:Then
      If D:Then
         Output(Y,X,Str1
         M->|LDRWDB(I)
         Else
         If |LDRWDB(I):Then
            Output(Y,X,Str3
            Else
            Output(Y,X,"
         End
      End
   End
   min(H,max(1,Y+sum(DeltaList(K={25,34->Y
   min(W,max(1,X+sum(DeltaList(K={24,26->X
   Output(Y,Ans,Str2
   If K=85
   0->M
   If K=95
   1->M
   If M:Then
      Str3->Str1
      "plotcross->Str2
      If D
      "+->Str2
      Else
      " ->Str1
      "plotsquare->Str2
      If D
      "o->Str2
   End
   If K=105
   not(D->D
End
ClrHome
Disp "MENU
Output(2,1," Back
Output(3,1," Quit
Output(4,1," Clear
Output(5,1," Help
2->S
0->C
Repeat C and max(K={92,93,24,34,105
   getKey->K
   If Ans
   Output(S,1,"
   min(5,max(2,S+sum(DeltaList(K={25,34->S
   Output(S,1,"[|>]
   If S=5 and K=105
   82->K
   If K=105
   90+S->K
   If K=92
   Goto 0
   If K=93:Then
      ClrHome
      Stop
   End
   If K=94:Then
      Fill(0,|LDRWDB
      ClrHome
      Goto 1
   End
   If K=82:Then
      ClrHome
      Disp "Move with arrows","","Toggle pen up or","down with ENTER","","+ Key: Draw","- Key: Erase
      Pause
      ClrHome
      Goto 0
   End
End
Ekyl wrote:
Basically, when I press Enter it should toggle draw and erase mode. However, it's only ever in erase mode for a second, so if I press Enter and then an arrow immediately after it won't leave behind an X; any delay will print an X. I don't know why it's doing this. Please help!


First of all, it should be Output(Y,Ans,"\plotcross

You could probably use something like this:

Code:
:ClrHome
:4→Y
:8→X
:"X "→Str1
:0->M
:Lbl 0
:Repeat max(K={21,23,45
:0
:Repeat Ans
:getKey→K
:End
:Output(Y,X,sub(Str1,M+1,1
:min(16,max(1,Ans+sum(DeltaList(K={25,34→Y
:min(8,max(1,X+sum(DeltaList(K={24,26→X
:Output(Y,Ans,"\plotcross
:If K=105
:not(M->M
:End
:Menu("Quit?","Yes",1,"No",0
:Lbl 1
:ClrHome
Do you understand what the following code does in full, or did you get it from another user? If you don't fully understand it, I strongly recommend unpacking it into something clearer, then later learning to optimize it:
Code:
:min(16,max(1,Ans+sum(DeltaList(K={25,34→Y
:min(8,max(1,X+sum(DeltaList(K={24,26→X
First, it's broken because you use Ans in the first line when you should be using Y instead: K will be in Ans. The full version of this code would be something like the following:
Code:
Y+(K=34)-(K=25
min(16,max(1,Ans->Y
X+(K=26)-(K=24
min(8,max(1,Ans->X

In addition, you shouldn't use Y for programs that might be graphical; although it's okay on the homescreen, if you moved this program to the graphscreen, the TI-OS will do exciting and unexpected things with Y that will break your program. I usually recommend (A, B) instead of (X, Y) (especially since I also tend to use X as a throwaway variable for things like loops).
KermMartian wrote:
Do you understand what the following code does in full, or did you get it from another user? If you don't fully understand it, I strongly recommend unpacking it into something clearer, then later learning to optimize it:
Code:
:min(16,max(1,Ans+sum(DeltaList(K={25,34→Y
:min(8,max(1,X+sum(DeltaList(K={24,26→X
First, it's broken because you use Ans in the first line when you should be using Y instead: K will be in Ans. The full version of this code would be something like the following:
Code:
Y+(K=34)-(K=25
min(16,max(1,Ans->Y
X+(K=26)-(K=24
min(8,max(1,Ans->X

In addition, you shouldn't use Y for programs that might be graphical; although it's okay on the homescreen, if you moved this program to the graphscreen, the TI-OS will do exciting and unexpected things with Y that will break your program. I usually recommend (A, B) instead of (X, Y) (especially since I also tend to use X as a throwaway variable for things like loops).


I use I for loops, usually, because that's common in C. Now, you're correct that I got the code from another user; in fact, I got it from the TI Basic wiki. It's a collaboration by many users. However, I searched for like 20 minutes about what min(, max(, and DeltaList( do, and when I finished I had a solid understanding of it.

You're also right about my use of Ans. I actually wrote this initially on my calculator, and since I was too lazy to get the cord to transfer the program, I decided to just type it up on the editor. It was correct in the original program, but when I typed it up here I messed it up.
jonbush wrote:
Ekyl wrote:
Basically, when I press Enter it should toggle draw and erase mode. However, it's only ever in erase mode for a second, so if I press Enter and then an arrow immediately after it won't leave behind an X; any delay will print an X. I don't know why it's doing this. Please help!


First of all, it should be Output(Y,Ans,"\plotcross

You could probably use something like this:

Code:
:ClrHome
:4→Y
:8→X
:"X "→Str1
:0->M
:Lbl 0
:Repeat max(K={21,23,45
:0
:Repeat Ans
:getKey→K
:End
:Output(Y,X,sub(Str1,M+1,1
:min(16,max(1,Ans+sum(DeltaList(K={25,34→Y
:min(8,max(1,X+sum(DeltaList(K={24,26→X
:Output(Y,Ans,"\plotcross
:If K=105
:not(M->M
:End
:Menu("Quit?","Yes",1,"No",0
:Lbl 1
:ClrHome


Thanks. I thought that if I use quotes, the editor wouldn't change plotcross to the symbol, but now I realize that the backslash prevents this.

What you wrote would work, but I specifically want x and space. Do you know WHY my program is broken?
I did some debugging, and this version should work Smile :


Code:
ClrHome
4->Y
8->X
"X "->Str1
0->M
Lbl 0
Repeat max(K={21,23,45
   Repeat Ans
   getKey->K
End
   Output(Y,X,sub(Str1,M+1,1
   min(8,max(1,Y+sum(DeltaList(K={25,34->Y
   min(16,max(1,X+sum(DeltaList(K={24,26->X
   Output(Y,X,"\plotcross
If K=105
not(M->M
End
Menu("Quit?","Yes",1,"No",0
Lbl 1
ClrHome
jonbush wrote:
I did some debugging, and this version should work Smile :


Code:
ClrHome
4->Y
8->X
"X "->Str1
0->M
Lbl 0
Repeat max(K={21,23,45
   0
   Repeat Ans
   getKey->K
End
   Output(Y,X,sub(Str1,M+1,1
   min(8,max(1,Y+sum(DeltaList(K={25,34->Y
   min(16,max(1,X+sum(DeltaList(K={24,26->X
   Output(Y,X,"\plotcross
If K=105
not(M->M
End
Menu("Quit?","Yes",1,"No",0
Lbl 1
ClrHome


I actually rewrote it, I just structured my If statement wrong. Now I can toggle between X and space.

Next, I'm going to write some code to store the drawing as a list, which can be written to. Since I can read out from this, I can use it to load a stored picture. Also, it should let me change it so that Enter is to toggle pen up/down, and plus/minus are to change between pen and eraser.
Ekyl wrote:
I actually rewrote it, I just structured my If statement wrong. Now I can toggle between X and space.

Next, I'm going to write some code to store the drawing as a list, which can be written to. Since I can read out from this, I can use it to load a stored picture. Also, it should let me change it so that Enter is to toggle pen up/down, and plus/minus are to change between pen and eraser.


I realized the If structure problem before, however this way is probably more optimized. That's a good idea with pen/eraser and up/down (unlike the built in pen function which has no undo). Also I don't think I needed the 0 before Repeat Ans, because Repeat checks at the end of the loop, so it will always go through once (do{}while(); style).
You might want to create a custom menu, like this one, made for the first menu:


Code:

Repeat max(K={45,92,93,94
Output(1,1,"Calculator Model
Output(2,1,"1. TI 84/83+
Output(3,1,"2. TI 84+ CSE
Output(4,1,"3. Quit
getKey->K
End
If K=45
Return
If K=92:Then
//do 84/83+ code
//code
End
If K=93:Then
//do 84+ CSE code
//code
End


This is untested code, but it should work. Smile
Unicorn wrote:
You might want to create a custom menu, like this one, made for the first menu:


Code:

Repeat max(K={45,92,93,94
Output(1,1,"Calculator Model
Output(2,1,"1. TI 84/83+
Output(3,1,"2. TI 84+ CSE
Output(4,1,"3. Quit
getKey->K
End
If K=45
Return
If K=92:Then
//do 84/83+ code
//code
End
If K=93:Then
//do 84+ CSE code
//code
End


This is untested code, but it should work. Smile


Looks good. I prefer the method of the normal menu, because it looks better; I might try this anyway.
What is the problem with multiple Goto's anyway?
Ekyl wrote:

Looks good. I prefer the method of the normal menu, because it looks better; I might try this anyway.
What is the problem with multiple Goto's anyway?


I probably will not explain this the best way, but essentially, when the program reaches a Goto (Like a Menu), it immediately starts looking for labels and Goto's throughout the whole program. This happens for the entire time you are running the program, slowing it down. If you don't have any, you free up cpu space for your program to use. It can also lead to a memory leak, if used in Loops and If Then statements.
Unicorn wrote:
Ekyl wrote:

Looks good. I prefer the method of the normal menu, because it looks better; I might try this anyway.
What is the problem with multiple Goto's anyway?


I probably will not explain this the best way, but essentially, when the program reaches a Goto (Like a Menu), it immediately starts looking for labels and Goto's throughout the whole program. This happens for the entire time you are running the program, slowing it down. If you don't have any, you free up cpu space for your program to use. It can also lead to a memory leak, if used in Loops and If Then statements.


Thanks! I'll see what I can change.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement