ftp.nice.ch/pub/next/science/mathematics/2DLab.3.1.s.tar.gz#/2DLab/dist.m

This is dist.m in view mode; [Download] [Up]

#include "math.h"
#include "stdio.h"
#include "tspheaders.h"
#include "TwoDView.h"
#include "TwoDTSP.h"
#include "prototypes.h"

/************************************************************************/
/* This function calculates the distance between every pair of cities in
   the problem. 

   The distance between city i and city j where i < j is stored only once
   as k = i*NumberOfCities - i*(i+1)/2 + (j-i+1).  Hence, the length of the 
   distance array is NumberOfCities*(NumberOfCities - 1)/2. 
*/
/************************************************************************/
/* Parameters:      float *Data: Pointer to area that contains coordinates 
	                         of cities.

                float *Distance: Pointer to area that will contain the
                                 distances between each pair of cities.

             int NumberOfCities: Number of cities in this tour.
*/

/************************************************************************/
/************************************************************************/
void CalculateDistance(float *Data,float *Distance,int NumberOfCities) 
{
int i,j,k;

/* calculate distance between every pair of points */
for(i=0;i<NumberOfCities;i++) {
  for(j=(i+1);j<NumberOfCities;j++) {
    k = kfrom(i,j,NumberOfCities);
    Distance[k] = (double)(square(x(i) - x(j)) 
                + square(y(i) - y(j)));
    Distance[k] = sqrt((double)Distance[k]);
  }
}
}

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.