How can I check if a point is below a line or not ?

前端 未结 3 1648
醉梦人生
醉梦人生 2020-12-14 11:20

How can I check if a point is below a line or not ?

I\'ve the following data:

Line [ {x1,y1}, {x2,y2} ]
Points {xA,yA}, {xB,yB} ...

3条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-14 11:54

    Lets say you've given 2 points A, B and you want to know in which halfplane a third point C lies. The terms "below" and "above" as criteria are very vague, so you need a reference point, for example the origin. Just be sure this reference point is not collinear with A and B.

    What you have now is a triangle (A, B, C). Using the determinant you can calculate the signed area (see here, or here). The only interesting thing here is to remember the sign.

    Next step: for a given point D calculate the signed area of the triangle (A, B, D). If the result has the same sign as the area of your reference triangle -> C and D are on the same side of (A, B). If the sign differs -> C and D lie on opposite sides of the line. If the area of (A, B, D) is 0 then A, B and D are collinear. Note: use the Python builtin cmp to compare the triangle areas.

提交回复
热议问题