Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>let us implement the formula again, according to the contents of your original <a href="http://en.wikipedia.org/wiki/Perpendicular_distance" rel="nofollow">link</a>.</p> <ul> <li>we have a <strong>vector</strong> for the line: <em>V(a; b)</em></li> <li>we have a <strong>point</strong> on the line (the centre of the sprite): <em>P(x1, y1)</em></li> <li>we have <strong>another point</strong> somewhere else: <em>B(xB, yB)</em></li> </ul> <p>for the testing here are two rows of random values:</p> <ol> <li><code>a = -0.7; b = -0.3; x1 = 7; y1 = 7; xB = 5; yB = 5;</code></li> <li><code>a = -0.7; b = -0.3; x1 = 7; y1 = 7; xB = 5.5; yB = 4;</code></li> </ol> <p><strong>the numerator</strong> is the following then: <em>(it seems you are calculating the numerator an unknown way, I don't understand why you did it because this is the proper way to calculate the numerator for the linked formula, perhaps this is why you got totally wrong distances.)</em></p> <pre><code>float _numerator = abs((b * xB) - (a * yB) - (b * x1) + (a * y1)); // for the 1. test values: (-0.3 * 5) - (-0.7 * 5) - (-0.3 * 7) + (-0.7 * 7) = -1.5 + 3.5 + 2.1 - 4.9 = -0.8 =&gt; abs(-0.8) = 0.8 // for the 2. test values: (-0.3 * 5.5) - (-0.7 * 4) - (-0.3 * 7) + (-0.7 * 7) = -1.65 + 2.8 + 2.1 - 4.9 = -1.65 =&gt; abs(-1.65) = 1.65 </code></pre> <p><strong>the denominator</strong> is the following then:</p> <pre><code>float _denomimator = sqrt((a * a) + (b * b)); // for the 1. test values: (-0.7 * -0.7) + (-0.3 * -0.3) = 0.49 + 0.09 = 0.58 =&gt; sort(0.58) = 0.76 // for the 2. test values: (-0.7 * -0.7) + (-0.3 * -0.3) = 0.49 + 0.09 = 0.58 =&gt; sort(0.58) = 0.76 </code></pre> <p><strong>the distance</strong> is obvious now:</p> <pre><code>float _distance = _numerator / _denominator; // for the 1. test values: 0.8 / 0.76 = 1.05 // for the 2. test values: 1.65 / 0.76 = 2.17 </code></pre> <p>and these results (<code>1.05</code> and <code>2.17</code>) are the correct distances exactly for our random values, if you can draw the lines and the points on the paper you can measure the distance and you would get the same values, using standard ruler.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload