我想为我的TI-84编写一个基本程序,它可以在2D平面上找到多边形的面积。作为参考,我在蟒蛇多次写了这个,这里是做什么的:如何将值附加到列表然后遍历列表?
x_list,y_list,verts,tot_1,tot_2=[],[],int(input("How many vertices are on the polygon: ")),0,0 //sets vars to defaults and gets num of vertices
for i in range(verts): //gets X and Y values of each point for num. of vertices
x_list.append(float(input("X value of point %s: " % str(i+1)))) //appends x value given to x list
y_list.append(float(input("Y value of point %s: " % str(i+1)))) //appends y value given to y list
for ind in range(verts-1):
tot_1 += (x_list[ind]*y_list[ind+1])-(y_list[ind]*x_list[ind+1])
print(str(abs((tot_1)/2))) //prints area: abs value of total over two
这只是做了非常基本的算法,这里也显示在常规数学:http://www.mathopenref.com/coordpolygonarea.html
现在,当我尝试写在TI-Basic中(使用TI Connect应用程序并发送给计算器)也是如此,它会在第一次引用其中一个列表时返回语法错误; “检查所有输入的参数”。该线被星号包围。评论是不实际的代码
通过改变L1到list1的性格和L2到列表2字符改变计算器的代码时ClrHome //clears screen
Prompt V //gets number of vertices
0→T //sets total to 0
Disp V //displays vertices, was used for testing
For(N,1,V,1) //runs code for number of vertices
Input "x val: ",X //gets latest x val
Input "y val: ",Y //gets latest y val
**X→L1(1+dim(L1))** //appends x to listand
Y→L2(1+dim(L2)) //y to list
End //end for
For(I,1,P,1)
T+((L1(I)*L2(I+1))-(L2(I)*L1(I+1))→T //adds up total
End
Disp abs(T/2)
,它所作的只是返回值12.5 *数量的顶点-2 。我的问题是:
- 如何在计算机上的代码中表示列表?当我在代码中写入L1时,它实际上并不是我认为的内置列表变量,这就是导致语法错误的原因。我认为。
- 我需要重置列表变量吗?我第一次测试这个时,12.5 * vertices-2工作,所以它只是永久性地设置列表变量,现在当它在程序的后期运行中添加列表时,它永远不会到达那些索引?
- 代码是否有任何瑕疵导致它根本不起作用?我完全是TI-Basic的新手。