6

Possible Duplicate:
How can I tell if a point is nearby a certain line?

//Returns the point on the line traced from start to end which
//comes nearest to 500,000, 500,000. The points are scaled between
//1,000,000 and 0 from their original fp types.
Point closestToCentre(Point start, Point end);

Anyone know of quicker way than single stepping through the pixels?

Could some one more alert than me demonstrate their maths & geometry prowess please?

_______EDIT___________

Thanks Kris, this was confusing me :

[x; -a/bx-c/b]=[0; -c/b]-1/b[-b; a]x.

Now I see it is just splitting (mainly the y component) the vector into two which combine to yield the same result. Got the old partial fractions brain cell excited for a minute then :)

_______EDIT_________

Jason Moore, thanks for the inspiration, here is what I am doing, graphically,

64x64 square with 2 sample lines each passing edge to edge and missing the centre by some distance

I hope that is clearer.

____EDIT________

So I could reasonably expect to take a line at right angles to my sampled line and run it from the centre but how to tell when they touch?

enter image description here

I think Kris's page of equations is the way to go. If you're all telling me it is a two step process. It is just two simultaneous equations now, so I may not need Kris's derivations.

____EDIT_________

Whether good or bad thing, I don't know, but the beauty of stackoverflow as a search engine has revealed to me several routes of investigation. Chiefly I like the first solution here: Shortest distance between a point and a line segment.

But to prove this to my self I needed the link from matti's solution at the bottom (but one):

http://www.topcoder.com/tc?d1=tutorials&d2=geometry1&module=Static

The derivation is so simple and elegant even I could follow it!

Given http://mathworld.wolfram.com/Point-LineDistance2-Dimensional.html

Community
  • 1
  • 1
John
  • 6,433
  • 7
  • 47
  • 82
  • I'm thinking the ubiquitous quick sort is going to be tapping itself out here soon. No! They are already sorted!! So close, yet.. – John Aug 22 '11 at 00:19
  • This is a trigonometry question. Try asking it on http://math.stackexchange.com/. – Enigmativity Aug 22 '11 at 00:26
  • Not duplicate. Well it is to other people who asked the same thing. But finding the point on the line closest to a given point is a different question than finding whether or not a point is close to a line. You can use the answer to this to find the answer to that, but notably the chosen solution to that question does not answer this one. – Tatarize Jun 21 '15 at 07:38

2 Answers2

7

This is a matter of linear projection of a point onto a line, which can be done with some fine vector gymnastics, as elaborated at MathWorld.

The article details how to find the shortest distance from a point to a line, and one of the intermediate steps is finding the perpendicular line from the point x,y to the original line. Intersecting these two lines will give you the point, on the line, closest to x,y.

Edit in response to comment: What equation (2) in the link is doing is transforming the vector into a form reminiscent of y = mx + c, which allows you to quickly and easily read off the gradient, from which the perpendicular gradient can be easily calculated.

justkris
  • 800
  • 4
  • 15
  • That looks a good link. My iterative sol ran to a fpPoint class and several local vars. Now I'm puzzling over: [x; -a/bx-c/b]=[0; -c/b]-1/b[-b; a]x. Care to edit your post, I don't think I've covered simplyfying vectors so am being slow here. Thanks. – John Aug 22 '11 at 00:58
  • aah, yes y = mx + c, I've heard that before. But I think you'll find that is (eq.1)'s goal. Was gonna say I was 1 step ahead of you, but now I'm stumped on eq.3 :) – John Aug 22 '11 at 01:08
  • In your link does the umlat ^ above the vector signify that it is a unit vector? I think UK maths books use a flat hat.. – John Aug 22 '11 at 09:39
  • Comment 6 years after the answer: This answer could be improved to avoid link rot by bringing the main details of the answer right into SO, so this answer doesn't die of the link does... – StackExchange What The Heck Aug 23 '17 at 10:50
  • 1
    This Wikipedia section uses the same formula, and also lists the formulas to find `x` and `y` for the closest point on the line: https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line#Line_defined_by_an_equation – Sphinxxx Oct 22 '17 at 00:44
  • 1
    @yochannah and Sphinxxx good call, both - I'll get on it. – justkris Oct 26 '17 at 15:21
1

I think the quickest way will be a two step process:

  1. Assume your line is infinite in length, and find the intersection of your line and its perpendicular bisector through (500,000, 500,000).
  2. Make sure that point is actually on your line, else find the closest endpoint.

Kris's post covers step 1 pretty well, all you have to do is add the check for step 2 because you have a line segment and you're golden.

Let point 1 = (x1, y1) and endpoint 2 = (x2, y2). Then the line containing these two points is

y = (y2 - y1)/(x2 - x1) * (x - x1) + y1

and the perp. bisector through (5e5, 5e5) is

y = (x1 - x2)/(y1 - y2) * (x - 5e5) + 5e5

Your point (x,y) is the solution (x,y) to the above two equations (or one of the two endpoints). This might be more straightforward than the mathworld link. Note that this solution fails, however, when your line is either almost vertical or almost horizontal whereas I don't think the mathworld solution style does, though I haven't looked very closely.

Sean
  • 3,002
  • 1
  • 26
  • 32
  • Sorry, the wordiness of this post turned me to my VC IDE and an iterative sol. Your point "2. Make sure that point is actually on your line, else find the closest endpoint." Having not read Kris's link yet it sounds like you were expecting me to find my point on my line, which is most improbable. – John Aug 22 '11 at 01:13
  • Sorry if I was unclear. Step 2 is simply selecting the closest point from three candidate points: the point found in step 1, or both of the end points. There isn't any complicated math involved there, really. If the following condition turns out to be true: `x < min(p1.x, p2.x) || y < min(p1.y, p2.y) || x > max(p1.x, p2.x) || y > max(p1.y, p2.y)`, you have to choose between your two endpoints for the correct closest point. – Sean Aug 22 '11 at 01:57