tags:

views:

121

answers:

3

Is there an algorithm ( preferably in C# implementation) that allows me to compare how similar two lines are? In my case I have one reference line, and I have a lot of secondary lines, I need to choose, out of so many secondary lines, which is the closest to the reference line.

Edit: It is a 2D line, with start and stop points. When you compare the similarities, you to take into account of the full blown line. The direction of the line ( i.e., whether it's from left to right or vice versa) is not important. And yes, it has to do with how close it is from one another

I know this is kind of subjective ( the similarity, not the question), but still, I am sure there are people who have done work on this.

A: 

If you are talking about lines in the graphical sense, then I would look at a combination of things like line length and angle.

Depending on your situation, you may be able to make optimizations such as using the square of the length (saves a square root) and dy/dx for angle (saves a trig function, but watch for the divide-by-zero case).

geofftnz
Slope works in 2 dimensions. In more dimensions, he'd want to do a dot product of unit vectors extracted from the lines. I'm withholding an answer until I get a question that has an answer. ;-)
Nosredna
True... he could be talking about edit distance after all.
geofftnz
+3  A: 

Obvious metrics include slope, length, and distance between midpoints. You could calculate those and then find weightings that you like.

If you want to kind of wrap them all up into one thing, try the sum of the distances between the endpoints.

You're going to have to try a few things and see which cases irritate you and then figure out why.

Nosredna
A: 

lines (and in general hyperplanes) sit on an object call Grassmanian; e.g. lines in the plane sit in Gr(1,3), which is isomorphic to the 2-dimensional projective space, and yours is the simplest non trivial one: Gr(2,4). It is a compact metric space, which comes with a standard metric (arising from the plucker embedding - see the link above). However, this metric is a little expensive to compute, so you may want to consider an approximation (just as you'd consider using dot product instead of angle in 2 dimensions - it works find for small angles)

A more detailed explantion (based in the metric defined in the linked wikipedia article):

For each line l take two points (x1,y1,z1) and (x2,y2,z2) on it. Let A be the 4 by 2 matrix whose columns are (1,x1,y1,z1)^t and (1,x2,y2,z2)^t. Define P to be the 4 by 4 matrix A(A^tA)^(-1)A^t. Then P is dependent only on l and not of the choice of the two points.

The metric you want is the absolute value of the top eigen value of the difference between the matrices corresponding to the two lines.

David Lehavi
Thanks for the link, but it's all greek to me. Any pseudo code that I can use?
Ngu Soon Hui