5

I know how to check if a point is on a 2d line or not, but I'd like to do this in 3D. Any ideas?

        // slope from point 1 to point 3
        var p13:Number = (Math.atan2 (end.x - start.x, end.y - start.y)) * toDegrees;

        // slope from point 1 to point 2 -- matches?
        var p12:Number = (Math.atan2 (point.x - start.x, point.y - start.y)) * toDegrees;

        return Math.round(p12) == Math.round(p13);
Robin Rodricks
  • 110,798
  • 141
  • 398
  • 607
  • Algorithmically speaking, this should be on [Math SE](http://math.stackexchange.com/), although the question may have already been answered. – zzzzBov Feb 18 '11 at 16:26
  • think it was asked here http://stackoverflow.com/questions/563198/how-do-you-detect-where-two-line-segments-intersect – Joseph Le Brech Feb 18 '11 at 16:24

3 Answers3

6

Normalize the vectors. Check if the normals match.

Find the greatest value, divide all of the other values by that value so you get a vector normal.

Any point on a line should have the same vector normal.

Lee Louviere
  • 5,162
  • 30
  • 54
3

A point can never be 'on' a line in real coords. what you need to do is calculate the distance to the closest point to the line and decide if this is close enough for you.

Martin Beckett
  • 94,801
  • 28
  • 188
  • 263
  • 1
    -1 - very long and complex solution. completely unnecessary. see Xaade's answer. – Robin Rodricks Feb 18 '11 at 16:29
  • 2
    here is a nicer formula: http://mathworld.wolfram.com/Point-LineDistance3-Dimensional.html This solution is good because you don't need to worry about special cases, and your "tolerance" can be a distance rather than an angle. – Tom Sirgedas Feb 18 '11 at 17:12
  • @Jenko - the problem with comparing normals is that the distance to the line is proportional to the distance along the line. – Martin Beckett Feb 19 '11 at 00:09
  • 1
    Sorry I wasn't at all clear! If you are using the normalized slope of the line, and the slope (point-line start), then if the point is close to the origin then 1% error in the slope is still close to the line, but if you are a 1000km along the line then a 1% diff in slope could be a long way off. – Martin Beckett Feb 19 '11 at 23:09
  • @Robinicks Just wanted to add that I was looking for "Xaade's" answer but couldn't find it. After a bit of digging I found out what I expected, it is the answer from "Lee Louviere", they changed their name. I thought this information might be helpful for future readers. – Ela782 May 28 '18 at 09:29
0

The equation of a line is

v(t) = v0 + t*dir

Where v0 is some point on a line and dir is it's direction. Simply check if your point match this linear equation with enough accuracy

Andrew
  • 24,218
  • 13
  • 61
  • 90