user12858479
user12858479

Reputation:

Calculating Distance From Coordinates

I want to calculate the distance from a file I have containing cartesian coordinates. I thought the best way to do this would be to read in the data using csv and try to right a look which can calculate the distance between points.

Sample of coordinate file: (x y z)

0.0     0.0     1.2
0.0     0.0     2.4
0.0     0.0     3.6
0.0     0.0     4.8
0.0     0.0     6.0
0.0     0.0     7.2
0.0     0.0     8.4
0.0     0.0     9.6
0.0     0.0     10.8
0.0     0.0     12.0
0.0     0.0     13.2
0.0     0.0     14.4
0.0     0.0     15.6
0.0     0.0     16.8
0.0     0.0     18.0
0.0     0.0     19.2
0.0     0.0     20.4
0.0     0.0     21.6
0.0     0.0     22.8
0.0     0.0     24.0
2.10602 4.30161 15.4191
1.63363 3.84311 14.6922
1.0001799999999998      3.49724 14.0046
1.4663  2.88937 13.3751
0.9758450000000001      2.6336  12.8105
0.65735 1.4835200000000002      12.7484
0.46493 0.5138590000000001      12.9022
0.08873760000000001     -0.398863       12.5105
0.201499        -1.09372        11.6219
0.7903600000000001      -1.72045        11.0056
0.241616        -2.2128900000000002     11.9763
-0.20944000000000002    -2.29307        11.1821
-0.89938        -1.56606        11.1211
-1.41711        -2.4558 11.2009
-1.8840700000000001     -2.52969        12.2187
-1.81385        -1.49946        11.8263
-0.789176       -1.79308        12.1988
-1.17975        -1.1326 12.8
-1.0421200000000002     -0.111174       12.8198
-0.39158699999999996    -0.355446       13.5661
-0.453186       -0.8214870000000001     19.3475
-0.701328       -1.75309        19.3045
0.259701        -1.77368        18.8392
0.15001099999999998     -0.755324       18.3631
0.564654        -0.5530970000000001     17.6175
1.14706 -0.608927       16.8936
0.984617        -0.22286999999999998    15.9514
0.800405        -0.139544       14.949000000000002
0.823068        0.11181500000000001     13.8241
0.553984        -0.08825939999999999    12.8295
-0.23958600000000002    0.412857        12.4171
0.34061199999999997     0.7038409999999999      11.6287

I cannot get the data to be read in properly and i'm not sure if i'm going about this in the best way. Can anyone help me?

Python code:

import numpy as np

from math import sqrt



def length(frame):
    N=frame.shape[0]
    for i in range(N):
        for j in range(i+1,N):
            l = sqrt(np.linalg.norm(frame[j]-frame[i]))





len = list()
frame = list()
f = open('cartesian.csv') #
for line in f:
    frame=np.array(frame)
    len.append(length(frame))
    frame=list()

with open('output.txt', 'a') as g:
    for item in len:
        g.write("%s\n" % item)
g.close()

However, in my output file all I get is a list of 'none'. I am not sure if I am even approaching this the right way.

The equation I want to compute is distance = sqrt((x2-x1)+(y2-y1)+(z2-z1)). Any help?

Upvotes: 0

Views: 77

Answers (1)

michjnich
michjnich

Reputation: 3385

I suspect you just need to set your delimiter. Your default will probably be a comma or a semicolon - I think it may depend on location settings.

When you create the reader, set:

reader = csv.reader(f, delimiter="\t")

(At least, if the seperator is tabs, which I think it is, looking at your data).

at the moment, the data contains no seperator (i.e. no ","), so it just reads a string, leaving you with one val instead of 3.

Upvotes: 1

Related Questions