I have a little problem to solve .. i have a array with x, y, z co-ordinates in it as a tuple. I am trying to find the distance between each point and sorting the points according to the min distance. Can soemone help me with the algorithm ??

2
Contributors
3
Replies
4
Views
5 Years
Discussion Span
Last Post by woooee
``````import cv
from math import floor, sqrt, ceil
from numpy import array, dot, subtract, add, linalg as lin

def calcdist(data):
for p in data:
x = p[:1]
y = p[:2]
z = p[:3]
for i in range(len(data)):
dist = sqrt((x[i]-x[i+1])^2 + (y[i]-y[i+1])^2 +(z[i]-z[i+1]^2))
return dist
return [tuple(map(float, l.split()[1:4])) for l in open(filename)]

def main (data):

for i in range(len(data)): # Finding Neighbours
for j in range(len(data)):
dist = calcdist(data)
print dist

if __name__ == '__main__':
data = data[0:100]
main(data)
``````

This is the code i have written and i am new to python so its not running can somebody improve it for me ??

Start by printing x, y, and z. And print the return. These are always good places to start.

``````def calcdist(data):
for p in data:
x = p[:1]
y = p[:2]
z = p[:3]
print "x, y, and z =", x, y, z

for i in range(len(data)):
dist = sqrt((x[i]-x[i+1])^2 + (y[i]-y[i+1])^2 +(z[i]-z[i+1]^2))
print "returning", dist, "for", x, y, z
return dist
``````

Finally, take a look here http://www.greenteapress.com/thinkpython/html/thinkpython007.html#@default466 for squares and square roots. Hint: the square code is incorrect.

Edited by woooee

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.