0

I want to determine position of Point for a giving line in c#. I read this link and this link. I test them but it seems that my code is wrong. For some points close (not very close) to line it return wrong values. Here is my code:

    public static PointToLineSituation WhereIsPoint(Line l, Point p)
    {
        var x = p.X;
        var y = p.Y;
        var x1 = l.X1;
        var x2 = l.X2;
        var y1 = l.Y1;
        var y2 = l.Y2;

        var d = (x - x1) * (y1 - y2) - (y - y1) * (x2 - x1);

        if (d > 0)
            return PointToLineSituation.Up;

        if (d < 0)
            return PointToLineSituation.Down;

        return PointToLineSituation.OnLine;
    }

I want to use in graphical coordination. Is it my problem? Any help will be appreciated.

Here is an example of coordination system, an up point, and a down point as depicted below:

enter image description here

Babak.Abad
  • 2,839
  • 10
  • 40
  • 74
  • You want to see if the line is sloped up or down? –  May 09 '18 at 21:16
  • @Amy Please read my question again. I want to determine position of a point according to a line. – Babak.Abad May 09 '18 at 21:21
  • I'm asking questions to clarify the question *because it isn't clear what you're trying to do*. "position of a point according to a line" doesn't mean much. –  May 09 '18 at 21:23
  • First, what defines "Up" or "Down"? If the line is horizontal this is pretty easy, just compare the Y coordinates to see if the p.Y > l.Y. What about a vertical line? What is "Up" or "Down"? Is this a line segment with endpoints (x1, y1) and (x2, y2) or a line that passes through those two points? And again, what defines "Up" and "Down" in these two cases? – user7396598 May 09 '18 at 21:31
  • Question is updated with a picture – Babak.Abad May 09 '18 at 21:40
  • 2
    If you're trying to implement what's in those links, then `(y1 - y2)` in your formula should be `(y2 - y1)`. – lurker May 09 '18 at 22:14
  • Your code deosn't really match the code from link1, does it? Shouldn't it be `Math.Sign((x2-x1)*(y-y1)-(y2-y1)*(x-x1))` ? – TaW May 09 '18 at 22:14

2 Answers2

1

Your formula is almost (you swapped y1, y2) correct. What matters is not the result of this formula, but the sign of the result.

The accuracy on the result depends on the type the data, prefer double.

This

double d = (x - x1) * (y2 - y1) - (y - y1) * (x2 - x1);

should be enough... but numerical issues may yield a wrong result.

More about floating-point at What Every Computer Scientist Should Know About Floating-Point Arithmetic

Ripi2
  • 7,031
  • 1
  • 17
  • 33
1

Cross-product approach does not tell us - whether point is up or down, it just determines orientation of triplet P1-P2-P. To get needed information, you must also be sure in line direction. The simplest way - negate result sign when X2 < X1 (note that vertical line case X1=X2 is special or illegal for you).

Also note that line point differences should be consistent

var d = (x - x1) * (y2 - y1) - (y - y1) * (x2 - x1);
if (x2 < x1)
        d = -d;
MBo
  • 77,366
  • 5
  • 53
  • 86