[Question]
  [
Divide the first quadrant (including the positive x-axis, the positive y-axis, and the origin) into 1x1 grids, with each grid labelled by the coordinates of its bottom-left corner, as demonstrated below:
![](https://i.stack.imgur.com/61W6S.png)
Note that each grid contains its boundaries and its vertices. Using mathematical symbols, the grid labelled (m,n) would represent the square `{(x,y) | m ≤ x ≤ m+1, n ≤ y ≤ n+1}`.
---
Given a straight line in the form of `ax+by+c=0` with integers `a`, `b`, and `c`, and a grid represented by `(m,n)`, output whether the line passes through the grid, i.e. whether any point in the given grid is on the line.
---
```
a  b  c m n output
1  1  0 0 0 true
1  1  0 1 1 false
1  1  0 0 2 false
1  1 -3 0 1 true
1  1 -3 0 0 false
2 -1  0 1 1 true
2 -1  0 1 0 false
2 -1  0 0 2 true
2 -1  0 0 1 true
2 -1  0 1 2 true
2  0 -1 0 0 true
2  0 -1 0 1 true
2  0 -1 0 2 true
2  0 -1 1 0 false
2  0 -1 1 1 false
0  2 -1 0 0 true
0  2 -1 1 0 true
0  2 -1 2 0 true
0  2 -1 0 1 false
0  2 -1 1 1 false
1  0 -1 0 0 true
1  0 -1 0 1 true
1  0 -1 0 2 true
1  0 -1 1 0 true
1  0 -1 1 1 true
```
Please suggest more testcases in the comments.
---
This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"). Shortest answer in bytes wins. [Standard loopholes](https://codegolf.meta.stackexchange.com/questions/1061/loopholes-that-are-forbidden-by-default) apply.
  
[Answer]
# Python 3, 84 66 bytes
First golf, first fail (maybe).
Thanks to Rod for shaving off 18 bytes by using a function instead of direct input.
```
def f(a,b,c,m,n):f=-(a*m+c)/b;g=f-a/b;print(min(f,g)<=n<=max(f,g))
```
[Try it online!](https://tio.run/##Hcc9DoAgDEDhqzi2WqLETeEwgBYZqMYw6OnxJ2/48o67bLuMtS4rNwyOPAXKJDixVeDa3AXs/RwtK/d6nEkK5CTAFNFYMTa76x@sDJo0DV9YHw)
**Explanation:**
Basically we calculate the value of the line function for m and m+1, if n is inbetween the values, then the list must pass through it at some point.
Would be much better if the language had some simpler way to input multiple integers.
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 10 bytes
```
ż‘{Œpæ.ṠE¬
```
[Try it online!](https://tio.run/##y0rNyan8///onkcNM6qPTio4vEzv4c4FrofW/H@4Y9OhnYcnpB2dFPaoac2hrQ93Tis2frhz1uHl@joPd67iOjqp5OHuLYfbgZLu//8bKigAkQEYlhSVpnLBBAyBMC0xpxghYqBghCyiawxWhdAEFjCAKjFS0IUbA1aCEEBXAjIXRYkBph64CiBP1xDhWoSAIboAmhZke2EiMB8aKCgYoRgLEzBEFzBCFzDAMAQl4FBda4juWkN01xoiuRZNAKIFAA "Jelly – Try It Online")
### Background
Like other answers before mine, this relies on the fact that a straight line divides the plane into two semi-planes. The rectangle is either contained in one of these semi-planes (no intersection with the line) or intersects both semiplanes (and thus the line that separates them.
### How it works
```
ż‘{Œpæ.ṠE¬  Main link. Left argument: [m, n]. Right argument: [a, b, c]
 ‘{         Increment left; yield [m+1, n+1].
ż           Zipwith; yield [[m, m+1], [n, n+1]].
   Œp       Cartesian product; yield [[m, n], [m, n+1], [m+1, n], [m+1, n+1]].
     æ.     Take the dot products with [a, b, c], mapping each [x, y] to ax+by+c.
       Ṡ    Take the signs.
        E   Test the signs for equality.
         ¬  Logical NOT.
```
[Answer]
# [Python 2](https://docs.python.org/2/), 59 bytes
```
lambda a,b,c,m,n:min(0,a,b,a+b)<=-a*m-b*n-c<=max(0,a,b,a+b)
```
[Try it online!](https://tio.run/##ZZDNbsMgEITP8VNsLUVyUqiMe4viY3LtJVIvlSrs4sQSYAS4SZ7eBTf@I8IS7MfMsGt1t5dGZt1XlXeciuKHAkUFKpFAcidqmaTI1/S12OxzTLcCF1uJy30u6G1211ndMsjh5Laootz44uj3yOr7LlpdLzVnQNxpxd2VptfvWqrWJhtHjCPjq8i6SlCVsF/KEX8zitdO5nU6r5JR50FdgX7JrU9dKV1LC/GnbuQZmta6cKgaDWsTwxpMxG4lUxYOH8eD1o12nofjxIyFkhpmoGyE4syyuCMA7kv75WeLBkDc6geMJkk2J/i9V02mHqQPSQZ4jOklEwglPnchSZ89o8JVmEzdToCEILDM3x3IMGEKkC1iB0BCkIUgfQpZ/LhltyTsloTdklm3Afi3/AE "Python 2 – Try It Online")
We can tell which side of the line a point is by the sign of `a*x+b*y+c`. The line passes through the square (counting touching) unless all four vertices `(m,n),(m,n+1),(m+1,n),(m+1,n+1)` are strictly on the same side of the line. We can plug these values in an extract out the constant `a*m+b*n+c` that appears in all four:
```
a*m+b*n+c
a*m+b*n+c+a
a*m+b*n+c+b
a*m+b*n+c+a+b
```
So, the line passes through the square unless these four values are all positive or all negative. So, it suffices for their minimum to be `<=0` and maximum to be `>=0`.
```
min(a*m+b*n+c,a*m+b*n+c+a,a*m+b*n+c+b,a*m+b*n+c+a+b)<=0<=max(a*m+b*n+c,a*m+b*n+c+a,a*m+b*n+c+b,a*m+b*n+c+a+b)
```
Subtracting the common `a*m+b*n+c` from each part gives the code.
A slightly longer approach is to check whether the set of signs (+,0,-) has length at least 2.
**[Python 2](https://docs.python.org/2/), 62 bytes**
```
lambda a,b,c,m,n:len({cmp(a*m+b*n+c,-d)for d in(0,a,b,a+b)})>1
```
[Try it online!](https://tio.run/##ZZBda4MwGIWv9Ve8Ewpa4zDuruDu2tvdFHYzGFEjFWIMSVxXxn67S1z9SokQ38dzTk4UN33peDbU@cfASFtUBAgqUIlaxA@M8vCnbEVI9m1c7HlcoqSK6k5CBQ0PU2SlJC6i3@gVD1r2FHI4m82vCVN2ONnd1/J28L3rpWEUsHnzmPkkyfWz4aLXYWSIMmQ@GGkztUSE9IswxJ6VYI2RWZ3M63DWWdDUIJ9ybVM9IRuuIXiXnabQ9dqEg227UwHsQPn0u6RCw/HtdJSyk8Zzd5yp0lASRRWUXSsY1TQYMIB50nHZu/kTwGaNF/QXSbYmycuoWkwjSO@SDJI5ZpQswJXY3I0kffTMCjMleGm7AOwCx7I@dyLTDVOAbBM7AeyCzAXpQ8jmx23bYrctdtviVVsH/Fv@AA "Python 2 – Try It Online")
[Answer]
# Mathematica, ~~60~~ 55 bytes
```
Solve[m#+n#2==-#3&&#4<=m<=#4+1&&#5<=n<=#5+1,{m,n}]!={}&
```
*-5 bytes thanx to @MartinEnder*
input form
> 
> [a,b,c,m,n]
> 
> 
> 
[Answer]
## Batch, 66 bytes
```
@cmd/cset/a"q=%1*%4+%2*%5+%3,((-(q+%1)*(q+%2)&-q*(q+%1+%2))>>31)+1
```
Explanation: We consider the values taken by the equation at the four corners of the cell. If the line does not intersect the cell, then all four values have the same sign, but if it does intersect the cell, then at least one value will be zero or the opposite sign. The comparison is simplified by multiplying pairs of opposite corners, then if both values are positive then the line does not intersect the cell. Some bit-twiddling then converts the multiplications into an overall result.
[Answer]
# [Python](https://docs.python.org/), 54 bytes
```
lambda a,b,c,m,n:abs(2*(a*m+b*n+c)+a+b)<=abs(a)+abs(b)
```
[Try it online!](https://tio.run/##ZZDNboMwEITP8BRbpEj8Vti9ReWYXHuJ1EulylDTIBmwbNM0T0/XNEBwZKRlP2bGu8irOfcdHeviYxSsLb8YsLRMq7RNuz0rdUjjkMVtUsZdUkUJS8rotbCcYYOljEajBg4FnLD4NRPaNkdbfaOue9@7nBvBgeCbJ/CTYpfPppODCSMkGslyYWqwa5kM@Q8TqXjWUjQoszpV1OGis6CpQT0VxqZ6UjWdgeBd9d039IPBcKh7BTsdwA60z38rLg0c3o4HpXqFnpvjxLWBimmuoepbKbjhwUgA8MmnY3fzZ0DwTAv6q4Tek@xlUq2mCeQ3CYVsiZkkK3AlNncjyR89iwK7jKzTroC4wLHc3zuTecMcgG5iZ0BcQF2QP4Rsftx2WuJOS9xpyd20Dvi3/AE "Python 2 – Try It Online")
(Thanks to xnor for the testing script.)
### How it works
The line goes through *m* + 1/2 + *x*, *n* + 1/2 + *y* if and only if
*a*⋅(*m* + 1/2 + *x*) + *b*⋅(*n* + 1/2 + *y*) + *c* = 0  
⇔ 2⋅(*a*⋅*m* + *b*⋅*n* + *c*) + *a* + *b* = −2⋅*a*⋅*x* − 2⋅*b*⋅*y*.
This is possible for some |*x*|, |*y*| ≤ 1/2 if and only if |2⋅(*a*⋅*m* + *b*⋅*n* + *c*) + *a* + *b*| ≤ |*a*| + |*b*|.
[Answer]
## Mathematica, 50 bytes
```
-4<Tr@Sign[Tuples@{{#,#+1},{#2,#2+1}}.{##4}+#3]<4&
```
[Try it online!](https://sandbox.open.wolframcloud.com/)
Takes `m, n, c, a, b` as input in that order.
Explanation: `Tuples@{{#,#+1},{#2,#2+1}}` makes a list of the coordinates of the four corners of the square, then taking the dot product with `.{##4}` (which means `{#4, #5}`) and adding `+#3` computes `ax + by + c` for `x,y` at each corner. If the line passes through the point, this is zero; if the line is further from the origin, it's negative; and if the line is nearer to the origin, it's positive — therefore we check the `Sign`s of these four values. The line passes outside the square if and only if all four values are 1 or all four are -1, so we check that their sum is strictly between -4 and 4.
(This answer is vaguely inspired by my answer to [this question](https://codegolf.stackexchange.com/a/116337/66104).)
[Answer]
## Python 2, 147 110 bytes
```
def f(a,b,c,m,n):
 if b:d=sorted((-a*x-c)/float(b)for x in(m,m+1));return d[0]-1<=n<=d[1]
 return m<=-c/a<=m+1
```
And a *huge* thanks to Leaky Nun!
[TIO.](https://tio.run/##dZFBbsMgEEX3PsUsoR0Uxt2lZtsTdGd5gY1RLdk4IkRKT@86kZMm9kRILJ7efPhw@E0/Y8inybUevLBYY4MDBrnPoPNQ7505jjG1Tghl386qkTvfjzaJWvoxwhm6IAYc3knKz9imUwzgSl0pKkwojCupymDhQ2FUs7OFme3pELuQwPa9KDMvCAn1ZUkwBr7jqcU7nfcr/bL98QFrzLdYfVwGNiFXrJ/1HNVj@k2/Y16/nbrS9auUta0Xn8fEYzaEueHCV881z3OHLph4nPNYvwhn/ohtSnxT4pvSc9MN/g/JKjn9AQ)
[Answer]
# [Java (OpenJDK 8)](http://openjdk.java.net/), 71 bytes
```
(a,b,c,x,y)->(0<a?0:a)+(0<b?0:b)<=(x=-a*x-b*y-c)&x<=(0>a?0:a)+(0>b?0:b)
```
[Try it online!](https://tio.run/##hVTLboJAFN37FTckNmAHMth0o6Kbppum7cJl08UAg8EiQ5jBQBq/3V55SY1AMpCTOTPnPuDcPTsyUyQ83vs/5/CQiFTBHvesTIWRNVtOksyNQg@8iEkJ7yyMfycAYax4GjCPw6sQlw0AV4iIsxhcluovAi9xYARq5LbIa1HeosJYosQJH6mYwmAoCoEQzllnxCUeyUlhmGudrtiGLpjxiMhF5BorR88dk81y050Vpmc85LhD1@2xdXXsfEnwEqMupg5zFKEPByxJ36o0jHdf3yzdSaOqR3GpdJsALlq@cQUskrxM9pbHNe/lzadGQ6VZP0/vCKCo2c3gVuAfPyxQpdgvQMcD3L9PuxKDvD3CD@r3F3g9cO8bVbr9GV55e4Sfj/B0LIH@v2iwh/ZID@2RHto3PRzku/odS5ZeKU/7jbn9xtt@Y22/cXYNCtJMBZ4n3FPcr821LaTiB0tkykrQeSrQtencogH0vp/lAqZyGmukHQmkESU4K6zL3LlOC8dpyI32@aYttA@hAEFd1un8Bw "Java (OpenJDK 8) – Try It Online")
Port of xnor's Python solution.
### Original solution, using Java's shape/line intersection built-in (108 bytes)
```
(a,b,c,x,y)->b==0?x<=-c/a&-c/a<=x+1:new java.awt.Rectangle(x,y,1,1).intersectsLine(x,c=(c+a*x)/-b,x+1,c-a/b)
```
[Try it online!](https://tio.run/##hVRLb6MwEL73V4yQsoKtITirXtKyve1lH5Xa42oPY@NEpNSOsNMSrfrb0zGQNE0DSH7J38w3D/i8wmeMzVrpVf64W29EWUiQJVoLv7HQ8P8CoNBOVQuUCn4Y09wACGNKhRoEVmFuyE0BMuhO4nCSzHtD3W7b6JqcX2lah44Ceb4FzQx2ITLBJKvZNoq/iyxLb@ubLJZT/OKXm6y@5HOtXmBF@Sb44pJ7JR3qZalC8mGc8ShpErV0b38V2t/LLJSX@LWOprFgxMBkjFMR7XwFPpWu3i6bZ1Pk8ERVhw@uKvTy7z/AammjrmSnrAs5Axpps9JYYGlVU9UpTmPWi8ff9hyu2vTj6RkCIo2PMzgl@IAPE7Qp9hOk4wHO@6fHFIM4H8EH@fsLfDc4941a3v4M33E@gs9G8HQsgf6/aLCHfKSHfKSH/KSHg/gx/5F2G7U01l7a2CpctNuJ7NnhuVD1muSp8r2mHrbWqafEbFyyJsm5RRhMZjl8mld2DhM70QHzkSgKRSB2Yt4zMv@QJP45@mAQQZYdbOAWgrufwRyCP8YBnbqaXndv "Java (OpenJDK 8) – Try It Online")
## Credits
* -8 bytes thanks to [ceilingcat](https://codegolf.stackexchange.com/users/52904/ceilingcat)
]