find the closest pair of numbers (x and y)

R_Gol's icon

I have the following patch which storing points of the X axis from the mousestate into coll object when the user is pressing the key A.
Then when pressing the bang object ("press here to compare") the system is finding the closest number from the coll object that we store in comparison to the input data from the mouse. The above working very well.
I was wander I can I do the same but to compare the two axis x and y - So first to sotre two ponts into coll then to compare the two pints ( x y)
I can store two points into coll but I don't know how can I do the next stage which is compare the two

Max Patch
Copy patch and select New From Clipboard in Max.

11OLSEN's icon

As soon as you have more than 1 value, it becomes a vector. You can compare vectors by calculating the euclidean distance.

Max Patch
Copy patch and select New From Clipboard in Max.

R_Gol's icon

This is not exactly what I was looking for
Lets say I store those points
0, (100, 200)
1, ( 400, 100)
2, (0, 50)
3, (50, 100)

and I have a data that is constantly outputting pair of numbers
at any given point the system should output the closest pair of numbers from the numbers I store.
Is that possible?

11OLSEN's icon

But it is what you were looking for. You calculate the distance for every stored coord and take the entry with the smallest one.

Max Patch
Copy patch and select New From Clipboard in Max.

R_Gol's icon

Thanks. Works great.
How can I do the same with only 1 number or with 3 numbers?

edit: I manage to do only with one number:

Max Patch
Copy patch and select New From Clipboard in Max.


How can I do with 3 numbers?

Roman Thilenius's icon


the breakpoint for choosing stuff is always the mean of that pair.

then truncate the input to this number.

you could store the mean in the coll already instead of the pair.

11OLSEN's icon

I changed the [pack] for a [join]. Now you can use 3 or more.

Max Patch
Copy patch and select New From Clipboard in Max.

If you think about it for 1 value you only need to compare the differences.