pythonsympywolframalphatriangle

3-Line --> triangle --> 3-inequality I want to convert


I would appreciate any good advice. I want to determine the inequality sign from the triangular part of the seven places.

https://www.wolframalpha.com/input/?i=x+%E2%88%92+y+%3D+0%2C+x+%2B+y+%E2%88%92+2+%3D+0%2C+3*x+%E2%88%92+y+%E2%88%92+6+%3D+0&lang=ja

https://www.wolframalpha.com/input/?i=x+%E2%88%92+y+%3E%3D+0+%2C+x+%2B+y+%E2%88%92+2+%3E%3D+0%2C++3*x+%E2%88%92+y+%E2%88%92+6+%3C%3D+0&lang=ja

Input: x − y = 0, x + y − 2 = 0, 3*x − y − 6 = 0

I want to convert bellow

Output: x − y >= 0 , x + y − 2 >= 0, 3*x − y − 6 <= 0


Solution

  • One way of doing that is by doing the following steps:

    1. calculate the vertices of the triangle
    2. for each 2 vertices, calculate the line that goes through them
    3. for each vertex, check whether the line of the 2 other vertices is above or below the vertex

    STEP 1

    You can find the vertices of the triangle by finding the intersection points of every pair of sides:

    let's find the intersection point of two lines: ax+by+c=0 and dx+ey+f=0.

    the point should satisfy both equations, so:

    ax+by=-c
    dx+ey=-f
    

    we can remove the y from the first equation by subtracting the second equation multiplied by (b/e). and we get:

    (a-(d*b/e))x=-c+(f*b/e) => x=(-c+f*b/e)/(a-(d*b/e))`
    dx+ey=-f
    

    and then we can remove the x from the second equation by subtracting the first equation multiplied by d. and we get:

    x=(-c+f*b/e)/(a-(d*b/e))
    ey=-f-d(-c+f*b/e)/(a-(d*b/e)) => y=(-f-d(-c+f*b/e)/(a-(d*b/e)))/e
    

    so the intersection point is: ((-c+f*b/e)/(a-(d*b/e)),(-f-d(-c+f*b/e)/(a-(d*b/e)))/e)

    ugly, I know, but it works :)

    STEP 2

    now that we have the 3 vertices of the triangle, let's call them (x1,y1),(x2,y2),(x3,y3). the equation of the line that goes through 2 points (a,b) and (c,d) is: ((d-b)/(c-a))x-y-(((d-b)/(c-a))a+b)=0

    so the lines are:

    ((y2-y1)/(x2-x1))x-y-(((y2-y1)/(x2-x1))x1+y1)=0
    ((y3-y1)/(x3-x1))x-y-(((y3-y1)/(x3-x1))x1+y1)=0
    ((y3-y2)/(x3-x2))x-y-(((y3-y2)/(x3-x2))x2+y2)=0
    

    STEP 3

    If a,b,c are numbers so that b<0 then a point (x',y') is above the line ax+by+c=0 if and only if ax' + by' + c <= 0. so just check the equations we found before: each vertex of the triangle is in the triangle, so the lines inequalities must hold for it, if a vertex is above a side, then all other points in the triangle are above.

    Hope you find this answer useful :)

    Note: beware of extreme cases, some of the steps don't work for all cases (like when the line is x=0)