# Input

An integer n and a pair of distinct integer valued (x, y) coordinates on the boundary of the grid. The coordinates are indexed from 1. For example, n=10 and (1,2), (10, 7).

The points will always be on different sides of the grid and neither of them will be $$\(1, 1)\$$.

# Output

The number of integer points in an n by n grid that are on the same side of the line as the coordinate (1,1). Points that are exactly on the line should be counted as well.

# Examples

For n=10 and (1,2), (10, 7),

The output should be 41.

For n=8 and (8, 3), (3, 8),

The output should be 49.

For n=8 and (7, 1), (8, 2),

The output should be 63.

For n=8 and (1, 2), (2, 1),

The output should be 3.

• Indicing from 1 seems quite weird
– l4m2
Dec 25, 2023 at 11:24
• @l4m2 Tell that to R coders :)
– Simd
Dec 25, 2023 at 12:08
• Having the axes cross at (1, n) seems even weirder.
– Neil
Dec 25, 2023 at 14:43
• @LuisMendo Example added. Thank you.
– Simd
Dec 25, 2023 at 18:29
• Is it acceptable if a point on the line is declared to be above or below because of floating-point numerical issues, producing a wrong answer? Dec 25, 2023 at 18:55

# APL+WIN, 32 40 bytes

Plus 8 bytes to comply with the question as it is now written. Works for all four examples given.

Prompts for x coordinates followed by y coordinates and then grid size

+/,0≤(↑n)×n←×n∘.-⌊(⊃1,¨n←⍳⎕)+.×⎕⌹⎕∘.*0 1


Try it online! Thanks to Dyalog Classic

# JavaScript (Node.js), 99 bytes

(n,a,b,c,d,g=(x,y)=>(~y+b)*(c-a)-(~x+a)*(d-b))=>[...Array(s=i=n*n)].map(_=>s-=g(--i%n,i/n)*g()<0)|s


Try it online!

1-index

• There was a bug in the question. Apologies. It is fixed now.
– Simd
Dec 25, 2023 at 18:26

# Charcoal, 35 bytes

≔…·¹ＮθＮηＮζ≔×⁻θζ⁻ＮηεＩΣ⭆×⁻θη⁻Ｎζ⭆ε¬‹ιλ


Try it online! Link is to verbose version of code. Takes five inputs starting with n. Explanation:

≔…·¹Ｎθ


Create a range from 1 to n.

ＮηＮζ


Input the first point.

≔×⁻θζ⁻Ｎηε


Input the x-coordinate of the second point and subtract the x-coordinate of the first point from it, then for each integer from 1 to n multiply that by the y-coordinate subtracted from that integer.

ＩΣ⭆×⁻θη⁻Ｎζ⭆ε¬‹ιλ


Input the y-coordinate of the second point and subtract the y-coordinate of the first point from it, then for each integer from 1 to n multiply that by the x-coordinate subtracted from that integer. Pair the elements of this array with those of the previous array and count the number of parings where the element of this array does not exceed the element of the previous array.

Note: The output depends on the input points being entered in the correct order. The inputs for the other test cases are as follows:

• 8 3 8 8 3
• 8 8 2 7 1
• 8 1 2 2 1

# Python 3.8, 109 bytes

lambda n,x,y,X,Y:sum(((a:=(Y-y)/(d:=x*Y-X*y))*-~(K%n)+(b:=(x-X)/d)*-~(K//n)-1)*(a+b-1)>=0for K in range(n*n))


Try it online!

(Even) Econ majors will know that a line can be described by the equation $$\ax+by=1\$$. So this solution just computes $$\ax+by\$$ for each point on the grid and compares it to $$\1\$$.

# Jelly, 15 bytes

_þḢU×_/}Z>þ/¬SS


Try it online!

A dyadic link taking $$\n\$$ as the left argument and the points as the right argument as a list of two lists of integers.

This is loosely a Jelly translation of @Neil’s Charcoal answer so please upvote that one too!

## Explanation

_þ              | Outer using subtract (will implicitly turn n into 1..n)
U            | Reverse order of innermost lists
×_/}        | Multiply by the difference between the two points
Z       | Transpose
>þ/    | Outer using greater than
¬   | Not
SS | Sum and then sum again


n#(a,b,c,d)|(%)<-(\x y->(c-a)*(y-b)-(d-b)*(x-a))=sum[1|i<-[1..n],j<-[1..n],i%j*1%1>=0]