Given lattice points A and B, list K lattice points C such that both segments AC and BC are primitive and triangle ABC contains no other lattice point.
Hard8Number theoryGeometryMathBrute forceNo attempts yetTime limit1sMemory limit512 MBTwo friends stand at A and B, two distinct lattice points in the plane. To fit both of them in one photo you pick a lattice point C that satisfies all of the following.
A point whose x coordinate and y coordinate are both integers is a lattice point.
Nobody takes a single snap, so for the given A and B you need K such points C. Infinitely many points satisfy the conditions, so the output section fixes which K of them you print.
The first line contains the number of test cases T. (1≤T≤1000)
Each of the next T lines contains five integers Ax, Ay, Bx, By, K. The two points A=(Ax,Ay) and B=(Bx,By) are distinct. (−109≤Ax,Ay,Bx,By≤109, 0≤K, the sum of all K is at most 20000)
For each test case print K lines, each holding the coordinates of one point C in the format "Cx Cy". A test case with K=0 prints nothing.
Choose the K points this way. Among the lattice points C that satisfy every condition above, keep only those with
s(C)=(Bx−Ax)(Cy−Ay)−(By−Ay)(Cx−Ax)>0
and
f(C)=(Cx−Ax)(Bx−Ax)+(Cy−Ay)(By−Ay)≥0.
Print K of the kept points in increasing order of f(C), starting from the smallest.
Infinitely many points remain, so K of them always exist, and the kept points have pairwise different values of f(C), so the output is unique. Every coordinate chosen by this rule has absolute value at most 1014.