I'm wondering if someone could take some time to review this script. I'm parsing through a list of points of any length and calculating the distance. I'm wondering how to make my code better/more efficient (something I'm working on). A sample input file would be like so:
300.754178236262248 103.453277023380423 0,276.62980277988612 90.123295023340319 0,269.345711570634421 103.319531391674346 0,293.447811515317824 116.649513392506364 0,300.754178236262248 103.453277023380423 0
I'm not sure why the zeros are there; these are from the spacenet label csv's.
And this is my code:
import math
def calc(x1, y1, x2, y2):
dist = math.sqrt((x2-x1)**2 + (y2-y1)**2)
return dist
li = []
res = []
with open("/Users/jin/points.txt") as filestream:
for line in filestream:
temp = line.split(",") #splits by the comma in a single list
for i in temp:
temp = i.split(" ") #splits by spaces to individual lists of points
li.append(temp) #list of lists containing each point
# for item in li:
# x1 = item[1]
# y1 = item[0]
# for item in li:
# for pt in item:
# print pt
for index in range(len(li)-1):
one = li[index]
two = li[index+1]
x1 = float(one[0])
y1 = float(one[1])
x2 = float(two[0])
y2 = float(two[1])
res.append(calc(x1, y1, x2, y2))
print res