-1

I know how to get distance between points, however I want to get distance between 2 objects where each object has several points. (See illustration below.)

Illustration of My Problem

I want to calculate distance between object A and object B based on their points with Euclidean Distance.

Can I use euclidean distance for my problem?

Example Equation in Java: Math.SQRT(Math.sqr(y2-y1) + Math.sqr(x2-x1));

Erica
  • 2,399
  • 5
  • 26
  • 34
Rex
  • 13
  • 1
  • 4
  • 1
    You need to first to determine _the definition of distance_ between two-dimensional objects -- closest points? furthest points? centroid? -- before there can be an answer to this question. Unless you simply want a table of distances between all points? (e.g. A1 to B1, B2, B3, A2 to B1, B2, B3, etc.) – Erica Feb 02 '16 at 12:55
  • This also may be more on-topic on Math.SE or GIS.SE (for example) than on StackOverflow. – Erica Feb 02 '16 at 12:57
  • Thanks for reply Erica, I want to know how similar between object A and B, next i will calculate to for another object, example : object A and C , object B and D. After that i want to choose the appropriate class for object A. The minimum distance for my problem is the best condition. Thanks – Rex Feb 02 '16 at 13:00
  • @Erica : Hello Erica, when i search more, i find (maybe) similar problem, this is the link : http://stackoverflow.com/questions/5582374/euclidean-distance ; thanks – Rex Feb 02 '16 at 14:29

2 Answers2

1
  • The maybe best way might be (as @Erica already suggested) to take the distance as the sum of the distances of the closest points, but beware, this is NOT SYMMETRIC, hence not a real distance in the mathematician way. To gain symmetric you might add it with the same sum of the other object, this will yield a mathematician distance method.

  • Another way would be to index the points and take the distance of the same points (when you know, there are always the same amount of points). This has the drawback, that the same points with different index is another object (you might indicate it with the distance to root and anti-clockwise for same distance to negate that effect). This also yields a mathematician distance method.

Code example for first one (one side):

double distance = 0;
for(Point x : A.getPoints()){
    double distOfX = 0;
    for(Point y : B.getPoints()){
        double tempDist = Math.pow(x.getX()-y.getX(),2)+Math.pow(x.getY()-y.getY(),2);
        distOfX = tempDist>distOfX?tempDist:distOfX;
    }
    distance += Math.sqrt(distOfX);
}

And for the second case (after indicating):

double distance = 0;
if(A.getPoints().length != B.getPoints().length)
    distance = -1;
else{
    for(int i=0; i<A.getPoints().length; i++){
        distance += Math.sqrt( Math.pow(A.getPoints()[i].getX()-B.getPoints()[i].getX(),2)+Math.pow(A.getPoints()[i].getY()-B.getPoints()[i].getY(),2));
    }
}
ctst
  • 1,610
  • 1
  • 13
  • 28
  • Can you give calculation example ? – Rex Feb 02 '16 at 17:29
  • Thanks for response, I want to ask about Math.pow(A.getPoints()[i].getX()-B.getPoints()[i].getX(),2) | Why i point in A minus i point in B ? How about first point in A minus second point in B ? It will give same result or different? sorry for my english – Rex Feb 02 '16 at 18:00
  • This will surely give a different result and won't even be a distance function (distance of twice the same element is always 0). – ctst Feb 02 '16 at 18:02
  • Ok Thanks @ctst , i will learn more from your suggestion. However for your additional information, i currently learn feature extraction use Hough Transform, and got result like that illustration where x-axis is Theta and Y-axis is rho. and use euclidean distance for check similarity between image A and B from points so if you find something wrong from my idea, kindly give me advice. Anyway thanks for your great response. – Rex Feb 02 '16 at 18:07
0

try this method:

    // GET DISTANCE METHOD
//Calculate the distance between two points base on their coordinates
public float getDistance(float X_1, float Y_1, float X_2, float Y_2) {
    //define the output distance
    float Distance;
    //calculate the distance
    Distance = (float) Math.sqrt(Math.pow((X_1 - X_2),2) + Math.pow((Y_1 - Y_2),2));
    //output the distance
    return Distance;
}
M.You
  • 1
  • 1