Try for each pair (i, j) of starting indices. Now, to compare them, first we have to transform both of them to start from zero. So all coordinates of the first part are seen as (x[k] - x[i], y[k] - y[i]) and similarly for second. Now the scaling factor for a particular pair can be calculated by finding the number which we should multiply the coordinates in the second part to get the corresponding coordinate in the first part. This is (x[j + 1] - x[j]) / (x[i + 1] - x[i]). Now we just keep looping till we can scale the second part to match the first part and calculate the distance in the first part. Take maximum of those. Seems like using double for the scaling factors causes precision problems. Hope i am clear.