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 ??

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 
def ReadPointCloud(filename):
    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 = ReadPointCloud(r'Z:\data\NEHreflectance_Scanner 1_part.txt')
    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 4 Years Ago by woooee

This article has been dead for over six months. Start a new discussion instead.