How to allow snake to grow in TI-BASIC - basic

I've been trying to make Snake in TI-BASIC for a few hours now and I was having a really hard time getting the snake the grow properly. So far I have:
ClrHome
5->Y
4->X
0->L
256->dim(|LSNAKE
X->T
1->A
While 1
ClrHome
Output(Y,X,"O"
If L>0:Then
For(Z,1,L
Output(|LSNAKE(V),|LSNAKE(Z),"O"
End
End
A->V
Y->|LSNAKE(A)
A+1->A
X->|LSNAKE(A)
A->Z
A+1->A
Input D
If D=25:Then
Y-1->Y
End
If D=34:Then
Y+1->Y
End
If D=24:Then
X-1->X
End
If D=26:Then
X+1->X
End
Output(7,1,|LSNAKE(1)
Output(8,1,|LSNAKE(2)
L+1->L
End
The ideas is that the previous coordinates are put into the list SNAKE using the A variable and that V and Z will recall the values. But it's not working out. The only snake games I can find online in TI-BASIC are extremely dense and have no comments. So I'm hoping I can get help here.

It looks like you're using
Input D
When you mean to use
getKey->D
There maybe be other problems I'm not seeing too.

Related

How to delete multiple plot axes made with subplot on scilab

how are you. I've all day trying to do the following: I'm designing a PID controller and depending on how much data I want to analize then I show only one plot or four plots in the same figure (this last multiple plot is made with subplot).
All the calculation and plots are made in one function with options in function dependind on what I want to see, I have several buttons and text inputs in the application that I'm coding and right know the PID design and the simple or multiple plots are working; my current program is that the axes handle is being assigned to local variables inside the function and of course this variables are distroyed when the program exits the function.
A more detailed explanation of the problem is the following: if I select multiple plot and then I want one plot then I don't know how to delete the four axes to clean the figure without closing it, so when I want to activate the simple plot then the program only erases one of the four plots and prints the simple plot over the remaining three while the remaining three subplots remains visible. Following is the piece of code that is giving me problems:
grafico=sistem_graf;
disp(grafico);
if(isdef('aa')==%T);
scf(aa);
delete(get("current_axes"));
disp("aa");
end
if(isdef('bb')==%T);
scf(bb);
delete(get("current_axes"));
disp("bb");
end
if(isdef('cc')==%T);
scf(cc);
delete(get("current_axes"));
disp("cc");
end
if(isdef('dd')==%T);
scf(dd);
delete(get("current_axes"));
disp("dd");
end
select ventana
case 0
tipoG=0;
plot2d(T,graffta);
xtitle('Ω(S)/R(S): Respuesta al escalón (Sistema original)','t [s]','Vel [rpm]');
legend(['Original';'C/control P';'C/Control PI';'C/Control PID']);
aa=get("current_axes");
aa.axes_bounds = [1/3 0 2/3 1];
case 1
tipoG=1;
// delete(get("current_axes"));
plot(T,[graffta;sist_P_ZN;sist_PI_ZN;sist_PID_ZN]);
xtitle('Ω(S)/R(S): Respuesta al escalón','t [s]','Vel [rpm]');
legend(['Original';'C/control P';'C/Control PI';'C/Control PID']);
aa=get("current_axes");
aa.axes_bounds=[1/3 0 2/3 1];
case 2
tipoG=2;
//delete(get("current_axes"));
subplot(421);
plot(T,[graffta;sist_P_ZN;sist_PI_ZN;sist_PID_ZN]);
xtitle('Ω(S)/R(S): Respuesta al escalón','t [s]','Vel [rpm]');
legend(['Original';'C/control P';'C/Control PI';'C/Control PID']);
aa=get("current_axes");
aa.axes_bounds=[1/3,0,1/3,1/2]
subplot(422);
plot2d(T,(kt*kp/(j*la))*(1/(afta*bfta))*(1+(1/(afta-bfta))*(bfta*exp(-afta*T)-afta*exp(-bfta*T))));
xtitle('Ω(t)','t [s]','Vel [rpm]');
bb=get("current_axes");
bb.axes_bounds=[2/3,0,1/3,1/2];
subplot(423);
plot2d(T,(kt/(j*la))*(1/(afta*bfta))*((1/(afta-bfta))*(bfta*afta*exp(-bfta*T)-afta*bfta*exp(-afta*T))));
xtitle('Variación de velocidad angular dΩ/dt','t [s]','Ac. [rpm/s^2]');
cc=get("current_axes");
cc.axes_bounds=[1/3,1/2,1/3,1/2];
subplot(424);
plot(T,[sist_P_ZN;sist_PI_ZN;sist_PID_ZN]);
xtitle('Controladores','t [s]','Vel [rpm]');
legend(['C/control P';'C/Control PI';'C/Control PID']);
dd=get("current_axes");
dd.axes_bounds=[2/3,1/2,1/3,1/2];
end
the four if(isdef("variable_name")) never doesn't works because when the program enters the functions those variables doesn't exists yet, those are created after the plots with subplot.
If the graphics properties are displayed from the Edit menú of the figure then all the children axes are shown but if I try to get the axes with for example
a=get(sistem_graf.Axes(1))
then a console display is shown saying that the property "Axes" doesn't exist so I don't know how to solve the problem.
Please help!
Thanks in advance.
Update 03/06/2021:
I found a way to have the behavior I need an is working at 90%, it only fails when I go from detailed plot to simple plot (is erasing one graphic) but backwards it works fine, I changed all the code before "Select ventana" to the following
if(imps(1) ~= -1)
if(size(imps) == 1)
sca(imps(1));
disp(imps(1));
delete(get("current_axes"));
elseif(size(imps) > 1)
sca(imps(1));
delete(get("current_axes"));
sca(imps(2));
delete(get("current_axes"));
sca(imps(3));
delete(get("current_axes"));
sca(imps(4));
delete(get("current_axes"));
end
end
and filled an array whose content is all the axes ploted last time that the function was called, that array is returned from the function to a global variable and finally that global variable is feed to the function in the next call and assigned to the local variable "imps".
I think is some kind of crazy but apparently scilab doesn't have incorpotated the static variable aproach.
If I have understood your problem, the small example below should help you:
function fun(nb)
ch = gcf().Children;
delete(ch(ch.type=="Axes"))
t = linspace(0,1,100);
if nb==1
plot(t,t)
else
for i=1:nb
subplot(2,2,i)
plot(t,t.^i)
end
end
endfunction
clf
uicontrol("style","pushbutton","units","normalized","Position",[0.1 0.1 0.4 0.1],"string","one plot","callback","fun(1)");
uicontrol("style","pushbutton","units","normalized","Position",[0.5 0.1 0.4 0.1],"string","four plots","callback","fun(4)");
I think that the mechanism you were missing is the following:
ch = gcf().Children;
delete(ch(ch.type=="Axes"))
i.e. something that allows to delete the plot axes and keep the uicontrols alive. The above construct uses logical indexing. Here ch.type=="Axes" is a vector of boolean values with components i egal to true when the type field of corresponding component ch(i) is Axes. Then, ch(ch.type=="Axes") is the subset of components of ch such that the boolean index has value true. Hence, at last, the subset of components of ch which are of Axes type.

Visual Python using a for loop to track a changing axis?

I have a giant array that I am importing from a text file holding 200,000 values which all describe how an arrow should be traversing along the x axis. Mx is the name of the array that I have imported from this text file. I am using visual python to try and simulate this movement across the axes.
arrow = arrow(length = 1.0,pos = (0,0,0),axis=(0,0,0),color=color.blue)
for i in range(len(Mx)):
rate(60)
arrow.axis.x = Mx[i]
When I run my code the arrow remains stationary, and "QObject::killTimers: timers cannot be stopped from another thread" this error message pops up. I am not sure why. Any advice would be really helpful.
What I found is that this fixed it for some reason.
arrow = arrow(length = 1.0,pos = (0,0,0),axis=(0,0,0),color=color.blue)
for i in range(len(Mx)):
rate(60)
x = Mx[i]
arrow.axis.x = x
Not 100 percent sure why, but I am guessing it has something to do with how python handles the array that I imported from the text file.

Polygon Area Calculations Differences Using MatLab's Polyarea to Excel's SumProduct

I am calculating the area enclosed by this polygon:
Given by a set of coordinates:
0 2.06667
1.14815 2.06667
3.44444 2.75556
5.74074 3.44444
9.18519 3.44444
16.0741 2.75556
26.4074 1.83704
35.5926 1.14815
38.2333 0.688889
38.75 0.028704
I originally did this in Excel, using the Sumproduct method, and got an answer which seemed reasonable. The Excel formula I'm using is:
=0.5*ABS(SUMPRODUCT(F30:F39,G31:G40)-SUMPRODUCT(G30:G39,F31:F40))
with F30:G40:
0 2.06667
1.14815 2.06667
3.44444 2.75556
5.74074 3.44444
9.18519 3.44444
16.0741 2.75556
26.4074 1.83704
35.5926 1.14815
38.2333 0.688889
38.75 0.028704
0 0
And the result is 87.74.
After moving to Matlab, I started using the polyarea and the answer looks unreasonable. The problematic code is:
Shape = [ 0 2.0667; 1.1482 2.0667; 3.4444 2.7556; 5.7407 3.4444; 9.1852 3.4444; 16.0741 2.7556;26.4074 1.8370; 35.5926 1.1482; 38.2333 0.6889; 38.7500 0.0287]
C = polyarea(Shape(:,1),Shape(:,2))
With the returned answer:
C =
47.6986
I have tried playing around with the order of points as one answer suggests here as well as adding the final points into the end (can't find the link again), but that does not seem to make a whole lot of difference.
One answer suggested using convhull for the maximum enclosed area, but that returned 50.34 with the Shape as it is currently defined.
So, my question is: how should this calculation be performed to get the correct result? The Excel-provided values look more correct to me based on the graph, but I am uncertain what I'm doing incorrectly in MatLab based on the help and advice I have investigated. If the error is in my Excel code, that would also be good to know.
You didn't close the polygon!
C = polyarea([Shape(:,1); 0],[Shape(:,2); 0])
C =
87.740954125
Matlab will not assume that (0,0) is in the polygon unless you explicitly state it!

Matlab - Using symbols in gscatter

Ok, so I'm trying to use gscatter to plot 8 different points in a figure. These 8 points are all different and thus I want to give them different symbols. I know that gscatter will automatically assign them different colors, but I also want to be able to use the figure in black and white. I have written the following code:
lincol = {'k';'k';'k';'k';'k';'k';'k';'k'};
linsym = {'+';'o';'*';'.';'x';'s';'d';'^'};
limits = [-1 1 -1 1];
close all
for i = 1:3;
figure(i); hold on
gscatter(RfootXdistpertRel(:,i),RfootYdistpertRel(:,i),lincol,linsym);
legend('Pert1', 'Pert2', 'Pert3', 'Pert4', 'Pert5', 'Pert6', 'Pert7', 'Pert8')
hline(0);
vline(0);
axis(limits);
end
According to the matlab syntax, I should be able to specify color and marker symbol in this way (gscatter(x,y,col,sym)). The variables used are 8 by 1 vectors, just as the lincol and linsym. However, it gives me an error:
Error using plot
Color value must be a 3 or 4 element vector
Can anyone help? It's just such a silly problem to have.
Luc
It seems that you have some errors in your code. The syntax for gscatter should include at least 3 parameters : x, y and group. It seems that group is missing.
Furthermore the definition of color and sym may be wrong. Try col = 'kkkkkkkk'; instead of lincol = {'k';'k';'k';'k';'k';'k';'k';'k'};.
I hope this helps.
Regards.
Jonay

Bumpy jigsaw type data into curve data

I have a problem I solved in Excel but I am completely stuck in Matlab.
A weight measuring how much liquid I pump is refilled when its almost empty. Here is a picture
Now I want to see it in one motion, not like a jigsaw. Here is my solution, doing it manually in Excel:
Now to Matlab where this should be done automatically: I know how to index the row before and after I have the bumps, but I'm kind of stuck now. I do d=diff(x) ok and now I can replace the high peaks when the bumps occur (i=d(:,1)>0) with 0 so it never happened. And how do I translate it back? Somehow "undiff(x)"? im completely lost.
Here's an extract of my x:
2533,30
3540,00
3484,90
3430,00
3375,00
3320,20
3265,60
3210,60
3155,80
3101,20
3046,50
2991,70
2937,00
2882,50
2828,10
2773,80
2719,30
2664,90
2610,50
2556,10
2501,60
3508,00
3454,00
3399,70
3352,10
Like this?
temp = [0; diff(x)];
temp(temp < 0) = 0;
y = x - cumsum(temp);
y(temp > 0) = interp1(y, find(temp > 0) + 0.5);
plot(y);
hen using the default plot() function, matlab will automatically draw a line plot and connect each point in the data you are plotting.
Instead it seems like you just need to plot the points individually and unconnected. So if you are currently doing something like plot(x) or area(x) instead try plot(x,'o'). This will cause Matlab to plot the points individually without connecting them as lines.
If you'd like to change the marker type used to plot each point, use doc linespec to get more info.

Resources