One way of doing that is by doing the following steps:
- calculate the vertices of the triangle
- for each 2 vertices, calculate the line that goes through them
- 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)