How to determine if a function has a point in a rectangle? I have a linear function and I need to determine if it has a point in a rectangular area. How do I find if it's true or not?

 A: Given a rectangle orthogonal to the axes defined by two opposite corners $(x_1,y_1)$ and $(x_2,y_2)$ being the lower left and upper right corners respectively and a line given by $y=mx+b$ then this line will intersect the rectangle if and only if it intersects one of the vertical portions of its boundary since it cannot be vertical. This means it suffices to check if $y_1 \leq mx_1 +b \leq y_2$ or $y_1 \leq mx_2 +b \leq y_2$ one of which will be true if they intersect.
Edit: I'm mistaken that it must cross the horizontal lines but it will still be forced to intersect the boundary so we can test the horizontal boundaries as well by checking if $x_1 \leq m^{-1}y_1 -b \leq x_2$ or $x_1 \leq m^{-1}y_2 -b \leq x_2$ is true if any of the four conditions are met. 
A: Let the rectangle
D------C
|      |
|      |
A------B

be defined by the four points: 
\begin{align*}
A &= (x_0,y_0)\\
B &= (x_1,y_0)\\
C &= (x_1,y_1)\\
D &= (x_0,y_1)
\end{align*}
If the linear function defined by $f(x)=ax+b$ intersects the rectangle, there are three cases: 
1) $f$ intersects $AD$, then 
$$y_0≤f(x_0)≤y_1$$
is true.
2) $f$ intersects $BC$, then
$$y_0≤f(x_1)≤y_1$$
is true. 
3) It is steep enough to go through the horizontal lines. Let the intersection point of the linear mapping with the horizontal line through $A$ and $B$ be: $(\tilde{x},y_0)$.  
Then we can calculate that point by: 
$$ y_0 = f(\tilde{x}) = a\tilde{x} + b \qquad ⇒ \qquad \tilde{x}=\frac{y_0-b}{a}.$$
(That solution always exists, if $f$ is not constant.)
Now we need to check if $$x_0≤\tilde{x}≤x_1.$$
If that is true, the linear mapping intersects the bottom line. 

Thx @Professor Vector for the remark, that case 3 is also possible. 
A: Let's say your line is $y = a x + b$ and you are given
a simple $n$-polygon with vertices $v_1, v_2, \ldots, v_n$
in that order.
A way to test whether your line hit the $n$-gon goes like this.


*

*Compute the value of $f(v) = y - (ax+b)$ at each $v_k$.

*If any $f(v_k) = 0$, then your line hit the vertex $v_k$.

*If any $f(v_k)$ and $f(v_{k+1})$ has different sign ($v_{n+1}$ should be interpreted as $v_1$), your line hit 
some point on the line segment $v_kv_{k+1}$ and hence hit the polygon.

*If none of this happens, then your line miss the polygon.


For the problem at hand where the polygon is a rectangle $[x_1, x_2 ] \times [y_1, y_2]$, what you need to check is whether there are any sign changes of $f(v)$ along the loop:
$$
\stackrel{\color{blue}{v_1}}{(x_1,y_1)} \to 
\stackrel{\color{red}{v_2}}{(x_2,y_1)} \to 
\stackrel{\color{blue}{v_3}}{(x_2,y_2)} \to 
\stackrel{\color{red}{v_4}}{(x_1,y_2)} \to
\stackrel{\color{blue}{v_1}}{(x_1,y_1)}$$
Based on the sign of $a$, we can optimize this test a little bit.
There are three possible cases:


*

*$\color{red}{a > 0}$ - we have $f(v_4) > f(v_1) \;?\; f(v_3) > f(v_2)$.
If there are any sign changes, $f(v_4)$ and $f(v_2)$ will have different sign. 
This means we only need to test whether $f(v_2)$ and $f(v_4)$ have the same sign.

*$\color{blue}{a < 0}$ - we have $f(v_3) > f(v_2) \;?\; f(v_4) > f(v_1)$.
Similar to above case, we only need to test whether $f(v_1)$ and $f(v_3)$ have the same sign.

*$a = 0$ - we have $f(v_3) = f(v_4) > f(v_1) = f(v_2)$.
Once again, we only need to check whether $f(v_1)$ and $f(v_2)$ have the same sign.

