Bit of a challenging Python problem for me.
I have from one to six points in constant motion but only in a Cartesian(2D) plane.
I need to create a function that would calculate all the possible distances between these points and return a collection of some sort with theses distances and corresponding points.
So for a maximum of six points there would be 15 unique distances that need calculating (see diagram)

I assume I can use this for calculating the actual distance

What I need help with is how to best create the input and output.
ie I know I need to pass from 1 to 6 possible points (x,y coordinates and associated point reference) . So does this suggest 1-6 dictionary objects would first need to be created?

And as well the returned collection needs both the calculated distances and associated point pairs for each distance.

Lastly since this is constantly running, optimization is important.
Any feedback appreciated.

Here’s another take as well, something I modified a bit to relate to your 6 point scenario. It’s entirely a python approach, so depending on what you’re ultimately trying to do, @DavidBraun’s approach might be a lot faster! However if it’s important to you to have access to the data in a convenient python way then a class object / structure may be helpful. This is not complete, but may give some more ideas.

import math
class PointSet:
def __init__(self, initPoints):
'''
we do the more expensive work just once, upon initialization of the class object.
'''
### create points dictionary where keys are int indicies, and x/y pair accesible via string names.
self.points = []
for each in initPoints:
self.points.append( [each[0],each[1]] )
self.lines = []
for Ai,A in enumerate(self.points):
for Bi,B in enumerate(self.points):
if Bi != Ai: # a line cannot be between two of the same points.
Ai_smaller = min(Ai,Bi)
Ai_bigger = max(Ai,Bi)
line = [Ai_smaller,Ai_bigger]
if line not in self.lines: # check that line is not already in list.
self.lines.append( line )
self.pointLineLookup = []
for pointIndex in range(len(self.points)):
# get line indicies who's starting and ending point matches pointIndex
aList = [ lineIndex for lineIndex,line in enumerate(self.lines) if line[0] == pointIndex ]
bList = [ lineIndex for lineIndex,line in enumerate(self.lines) if line[1] == pointIndex ]
# converting to sets, allows us to combine, and remove possible duplicates.
# then convert back to list, and sort it.
matchingLineIndicies = sorted( list( set( aList ).union( set( bList ) ) ) )
# append this points lookup list to the master.
self.pointLineLookup.append( matchingLineIndicies )
self.lineLengths = []
for line in self.lines:
x1 = self.points[line[0]][0]
y1 = self.points[line[0]][1]
x2 = self.points[line[1]][0]
y2 = self.points[line[1]][1]
d = self.dist(x1,y1,x2,y2)
self.lineLengths.append( d )
def dist(self,x1,y1,x2,y2):
'''
Lower case first letter makes this a private function accessible only within the class.
'''
return math.sqrt((x2 - x1)**2 + (y2 - y1)**2)
def Update_Coordinates(self,coordList):
assert len(self.points)==len(coordList),'Number of coordinates provided do not match what was initialized.'
for i,pt in enumerate(self.points):
x = coordList[i][0]
y = coordList[i][1]
self.points[i] = [x,y]
def Calc_Distances(self):
for i,line in enumerate(self.lines):
x1 = self.points[line[0]][0]
y1 = self.points[line[0]][1]
x2 = self.points[line[1]][0]
y2 = self.points[line[1]][1]
d = self.dist(x1,y1,x2,y2)
# print(x1)
self.lineLengths[i] = d
return
def Get_Distances(self, pointIndex):
pointLines = self.pointLineLookup[pointIndex]
lineDistances = [ self.lineLengths[each] for each in pointLines ]
return lineDistances
def Dump_Results( self, datTable ):
datTable.clear()
datList = [ [ '' ] + [ "P:"+str(each) for each in range(len(self.points)) ] ]
for i in range(len(self.points)):
distList = self.Get_Distances(i)
datList.append( [ 'P:%i'%(i) ] + distList )
datTable.appendRows(datList)

I would check Paketa’s Plexus tutorial method for much more points on youtube, I think there’s also another one out there, maybe Exxtass on GLSL, cheers, P