How to know if a segment is completely included between two lines? I have three segments (not necessarily parralel):


*

*blue $((ax1, ay1), (ax2, ay2))$

*green$((bx1, by1), (bx2, by2))$

*red $((cx1, cy1), (cx2, cy2))$


and a $margin$ value which is the width of the sky blue band in the sketch bellow (with infinite length and always centered to the blue segment).

Is there a way to know if a segment is completely in the sky blue band knowing the coordinates of each segment and the value of the margin ?
 A: *

*From $[(cx_1, cy_1), (cx_2, cy_2)]$ find $(M_x, M_y)$ of M, the midpoint of c.


*From $[(ax_1, ay_1), (ax_2, ay_2)]$, find L, the equation of a. Let say it is $L: Ax + By + C = o$.


*From the above, find d, the distance of M of from L. d is given by $|\dfrac {A(M_x) + B(M_y) + C}{\sqrt {A^2 + B^2}}|$.


*Compare d with half of the margin value.


*If a and c are not parallel (i.e. slope of a is not the same as c), extra checking is necessary by testing $d_1$ and $d_2$; where $d_1$ is found by the same formula with $(M_x)$ and $(M_y)$ replaced by $cx_1$ and $cx_2$ respectively. $d_2$ is similarly defined.
A: Yes. The sky blue margin is convex, so the segment is in the interior if and only if its endpoints are.
A: Of course. Since the band is convex, to make sure the segment is completely in the band, you only need two endpoints in the band. (It is applicable for all convex domains)
A: If both endpoints are within the band, the segment is completely within the band.
The band is $2 h$ wide, and centered on a line passing through points $(x_1, y_1)$ and $(x_2, y_2)$.
(Note that I am using $(x_1,y_1)$ instead of $(ax1, ay1)$, and $(x_2, y_2)$ instead of $(ax2, ay2)$, and $h = {margin}/2$, because I find it clearer this way.)
Therefore, point $(x,y)$ is within the band if and only if its distance from the line passing through points $(x_1, y_1)$ and $(x_2, y_2)$ is at most $h$. 
Point-line distance is fortunately easy to calculate:
$$d = \frac{\left\lvert (x_2-x_1)(y_1-y) - (y_2-y_1)(x_1-x)\right\rvert}{\sqrt{(x_2-x_1)^2 + (y_2-y_1)^2}}$$
Thus, if both endpoints of a segment fulfill $2 d \le margin$ (or, $d \le margin/2$, or $d \le h$), the segment is completely within the band.
