Points nearest to each other The program in Listing 113 find

(Points nearest to each other) The program in Listing 11.3 finds the two points in a two-dimensional space nearest to each other Revise the program so that it finds the two points in a three-dimensional space nearest to each other. Use a two-dimensional list to represent the points. Test the program using the following points: points [[-1, 0, 3], [-1, -1, -1], [4, 1, 1] [2, 0.5, 9], [3.5, 2, 1], [3, 1.5, 3], [-1.5, 4. 2] [5.5, 4, -0.5]]

Solution

Assuming code is required to be in python ( guessing from problem statement ) :

import math as mt;

def nearestPoints( points ):
   size = len(points);
   minimumDistance = -1.0;
   result = [];
   for i in range(size):
       for k in range(size-i-1):
           j = k + i + 1;
           #distance between point i and j
           dist = ( points[i][0] - points[j][0] )*( points[i][0] - points[j][0] );
           dist = dist + ( points[i][1] - points[j][1] )*( points[i][1] - points[j][1] );
           dist = dist + ( points[i][2] - points[j][2] )*( points[i][2] - points[j][2] );
           dist = mt.sqrt( 1.0*dist );
           if( minimumDistance == -1 or minimumDistance > dist):
               minimumDistance = dist;
               result = [ points[i], points[j] ];
   return result;

points = [ [-1,0,3], [-1,-1,-1], [4,1,1], [2,0.5,9], [3.5,2,-1], [3, 1.5, 3], [-1.5,4,2], [5.5,4,-0.5]];
print nearestPoints( points );

 (Points nearest to each other) The program in Listing 11.3 finds the two points in a two-dimensional space nearest to each other Revise the program so that it

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site