Find if point lays on line segment

前端 未结 12 1301
抹茶落季
抹茶落季 2020-11-30 04:01

I have line segment defined by two points A(x1,y1,z1) and B(x2,y2,z2) and point p(x,y,z). How can I check if the point lays on the line segment?

12条回答
  •  借酒劲吻你
    2020-11-30 04:44

    Based on Konstantin's answer above, here is some C code to find if a point is actually on a FINITE line segment. This takes into account horizontal/vertical line segments. This also takes in to account that floating point numbers are never really "exact" when comparing them with one another. The default epsilon of 0.001f will suffice in most cases. This is for 2D lines... adding "Z" would be trivial. PointF class is from GDI+, which is basically just: struct PointF{float X,Y};

    Hope this helps!

    #define DEFFLEQEPSILON 0.001
    #define FLOAT_EQE(x,v,e)((((v)-(e))<(x))&&((x)<((v)+(e))))
    
    static bool Within(float fl, float flLow, float flHi, float flEp=DEFFLEQEPSILON){
        if((fl>flLow) && (fl

提交回复
热议问题