1

I am working on implementing a clustering algorithm in C++. Specifically, this algorithm: http://www.cs.uiuc.edu/~hanj/pdf/sigmod07_jglee.pdf

At one point in the algorithm (sec 3.2 p4-5), I am to calculate perpendicular and angular distance (d┴ and dθ) between two line segments: p1 to p2, p1 to p3.

It has been a while since I had a math class, I am kinda shaky on what these actually are conceptually and how to calculate them. Can anyone help?

basil
  • 690
  • 2
  • 11
  • 30
  • possible duplicate of [Calculating the shortest distance between two lines (line segments) in 3D](http://stackoverflow.com/questions/627563/calculating-the-shortest-distance-between-two-lines-line-segments-in-3d) – R.. GitHub STOP HELPING ICE Mar 07 '11 at 17:21

2 Answers2

1

Look at figure 5 on page 3. It draws out what d┴ and dθ are.

EDIT: The "Lehmer mean" is defined using Lp-space conventions. So in 3 dimensions, you would use p = 3. Let's say that the (Euclidean) distance between the two start points is d1, and between the ends is d2. Then d┴(L1, L2) = (d1^3 + d2^3) / (d1^2 + d2^2).

To find the angle between two vectors, you can use their dot product. The norm (denoted ||x||) is computed like this.

Xodarap
  • 11,581
  • 11
  • 56
  • 94
  • well it speaks of d-dimensions but it defines lehmer means in very vague terms and it gives no real discernable method for determinding the angle between the two line segments. i suppose it could be done with some trigonometry but i dont quite know how to extrapolate that out to 3d. – basil Mar 07 '11 at 18:46
  • @basil: I've tried to clear it up, let me know if you still have questions. – Xodarap Mar 07 '11 at 19:31
  • Thank you. I think I kinda conceptually grasp it. I am looking on how to calculate euclidean distances right now. If you have any references for how to do that, it'd be great. Its been about 10 years since I took linear algebra and this stuff is coming back painfully slow. – basil Mar 07 '11 at 19:54
  • @basil: See [wikipedia](http://en.wikipedia.org/wiki/Euclidean_distance). "Euclidean distance" is just what everyone else calls "distance." – Xodarap Mar 07 '11 at 19:58
1

To get the perpendicular distance of a point Q to a line defined by two points P_1 and P_2 calculate this:

d = DOT(Q, CROSS(P_1, P_2) )/MAG(P_2 - P_1)

where DOT is the dot product, CROSS is the vector cross product, and MAG is the magnitude (sqrt(X*X+Y*Y+..))

Using Fig 5. You calculate d_1 the distance from sj to line (si->ei) and d_2 the distance from ej to the same line.

I would establish a coordinate system based on three points, two (P_1, P_2) for a line and the third Q for either the start or the end of the other line segment. The three axis of the coordinate system can be defined as such:

e = UNIT(P_2 - P_1)      // axis along the line from P_1 to P_2
k = UNIT( CROSS(e, Q) )  // axis normal to plane defined by  P_1, P_2, Q
n = UNIT( CROSS(k, e) )  // axis normal to line towards Q

where UNIT() is function to return a unit vector (with magnitude=1).

Then you can establish all your projected lengths with simple dot products. So considering the line si-ei and the point sj in Fig 5, the lengths are:

(l || 1) = DOT(e, sj-si);
(l |_ 1) = DOT(n, sj-si);
ps = si + e * (l || 1)      //projected point

And with the end of the second segment ej, new coordinate axes (e,k,n) need to be computed

(l || 2) = DOT(e, ei-ej);
(l |_ 1) = DOT(n, ej-ei);
pe = ei - e * (l || 1)      //projected point

Eventually the angle distance is

(d th)  = ATAN( ((l |_ 2)-(L |_ 1))/MAG(pe-ps) )

PS. You might want to post this at Math.SO where you can get better answers.

John Alexiou
  • 28,472
  • 11
  • 77
  • 133
  • This isn't really what I asked. I'm not looking for a perpendicular distance between a point and a line segment, but between two line segments. But I will take a look over at Math.SO, thanks. – basil Mar 07 '11 at 19:10
  • Two line segments are not necessarily in a plane to derive the perpendicular distance. I gave you an algorithm above to compute all those values in Fig 5 of the paper, but I guess you are not really following the logic above. IMHO you need a good grasp of 3D geometry to tackle such a project. – John Alexiou Mar 07 '11 at 19:20
  • I guess it is math.SE not math.SO - http://math.stackexchange.com. Sorry too much overflow! – John Alexiou Mar 07 '11 at 19:38