4

A line segment is defined as the two points $(x_1, y_1)$ and $(x_2, y_2)$. A point is then defined as $(x_3, y_3)$. My goal is to compute the mean (average) distance between the point and the line segment. (Not the shortest or longest distance).

My thoughts so far on this subject are as such. We can create a vector from $(x_1, y_1)$ to $(x_2, y_2)$ and then integrate for the average distance.

$$\int\left[\sqrt{(x_3 - ((x_2 - x_1) \times t + x_1))^2 + (y_3 - ((y_2 - y_1) \times t + y_1))^2}, {t, 0, 1}\right]$$

A numerical solution to this problem would be:

var numericalResult = 0;
for (var t = 0; t < 1; t += 0.000001) {
    numericalResult += Math.sqrt(Math.pow(x3 - ((x2 - x1) * t + x1), 2) + Math.pow(y3 - ((y2 - y1) * t + y1), 2));
}
numericalResult /= 1 / 0.000001;

I had someone else work on the integral with Mathematica. A verbose solution would be:

(sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)) * (((x1 - x2) * (x1 - x3) + (y1 - y2) * (y1 - y3)) * sqrt((x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3)) + ((x2 - x1) * (x2 - x3) + (y2 - y1) * (y2 - y3)) * sqrt((x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3))) - (log(-((x1 - x2) * (x1 - x3) + (y1 - y2) * (y1 - y3)) + sqrt(((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)) * ((x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3)))) - log(((x2 - x1) * (x2 - x3) + (y2 - y1) * (y2 - y3)) + sqrt(((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)) * ((x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3))))) * (-(x1 * y2 - y1 * x2) + (x1 * y3 - y1 * x3) - (x2 * y3 - y2 * x3)) * (-(x1 * y2 - y1 * x2) + (x1 * y3 - y1 * x3) - (x2 * y3 - y2 * x3))) / (2 * sqrt(((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)) * ((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)) * ((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2))))

This returns the correct value. You can run it through a simplifier to get an easier to read form with exponentials. A compressed optimized version in Javascript looks like:

var diff12X = x1 - x2;
var diff12Y = y1 - y2;
var diff13X = x1 - x3;
var diff13Y = y1 - y3;
var diff21X = x2 - x1;
var diff21Y = y2 - y1;
var diff23X = x2 - x3;
var diff23Y = y2 - y3;

var d122 = diff12X * diff12X + diff12Y * diff12Y;
var d123 = diff12X * diff13X + diff12Y * diff13Y;
var d133 = diff13X * diff13X + diff13Y * diff13Y;
var d213 = diff21X * diff23X + diff21Y * diff23Y;
var d233 = diff23X * diff23X + diff23Y * diff23Y;

var c12 = x1 * y2 - y1 * x2;
var c13 = x1 * y3 - y1 * x3;
var c23 = x2 * y3 - y2 * x3;
var temp = (-c12 + c13 - c23);
temp *= temp;

var newValue = (Math.sqrt(d122) * (d123 * Math.sqrt(d133) + d213 * Math.sqrt(d233)) - (Math.log(-d123 + Math.sqrt(d122 * d133)) - Math.log(d213 + Math.sqrt(d122 * d233))) * temp) / (2 * Math.sqrt(d122 * d122 * d122));

This calculation ends up being 6 square roots and 2 logs. Wondering if I missed anything. Searching for this problem online seems to yield no results due to the similarity of the problem to the shortest and longest distance problems. If it's simpler to analyze you can shift the problem so $(x_3, y_3)$ is $(0, 0)$ so:

$$\int\left[\sqrt{((x_2 - x_1) \times* t + x_1)^2 + ((y_2 - y_1) \times t + y_1)^2}, {t, 0, 1}\right]$$

I feel like the answers I'm getting are far more complex than they needs to be. (I believe the problem can be simplified to use two $\arcsin h$, but it doesn't really simplify the problem much for a computer). If someone could double check or suggest other methods or equations I'd really appreciate it.

hola
  • 1,339
Sirisian
  • 143

1 Answers1

1

To gain more insight into your answer, first consider a special case of the problem in which the line segment is positioned on the $+x$ axis, with $x_1=0$, and $x_2= l = $ segment length and $y_1=y_2=0$. Then $(x,0)$ is a typical point on the segment, the distance to which becomes

\begin{align} f(x) = \sqrt{ (x-x_3)^2 + y_3^2}, ~ x\in [0,l] \end{align}

The mean of $f(x)$ over the relevant interval is then $\int_{0\leq x\leq l} \frac{f(x)dx }{l}$.

Next, note that no matter how your initial line segment is positioned, it can always be rotated and translated to fall on $[0,l]$. The point $(x_3,y_3)$ should be similarly transformed. Even in the transformed coordinate system, the indefinite integral $\int f(x) dx$ works out to a very complicated expression, just as you pointed out:

Integrate[Sqrt[(x - a)^2 + b^2], x] == ((-a + x)*(a^2 + b^2 - 2*a*x + x^2) - b^2*Sqrt[a^2 + b^2 - 2*a*x + x^2]* Log[2*(a + Sqrt[b^2 + (a - x)^2] - x)])/ (2*Sqrt[a^2 + b^2 - 2*a*x + x^2])

where I set $(a,b):=(x_3,y_3)$.

See Wolfram Alpha for this.

Ganesh
  • 1,721