Problem Detail: There’s a line from (x1,y1) to (x2,y2) in a grid of squares of unit length. Write a program to compute the number of squares which are intersected by the line internally, i.e squares which are only touched by the line should not be counted. Notes x1,y1,x2,y2 are all integers. 0 <= x1,y1,x2,y2 <= 10000 Write the program so that it accepts 4 command line parameters – x1,y1,x2,y2 The output of the program should be a single line consisting of only the integer output Example: Input (x1 y1 x2 y2): 1 1 6 6 Output (No. of squares): 5
please help me to understand how these squares will generate? do i need to predefined all the squares in a 2d matrix form?
please help me to understand how these squares will generate? do i need to predefined all the squares in a 2d matrix form?
Asked By : Abhishek Karmakar
Answered By : Ran G.
As the question says, the program should be as follows:
INPUT:
4 integer numbers: $(x_1,y_1)$, $(x_2,y_2)$
OUTPUT:
a single number $n$. $n$ should be the number of squares that are touched by a line that starts at $(x_1,y_1)$ and ends at $(x_2,y_2)$. Assuming the squares are of length 1, and the first square has corners at (0,0), (1,0), (0,1), (1,1). That’s it. Nobody gives you the squares as a matrix or any other form. They are not in the memory, and nobody tells you how to construct them. Furthermore, your program DOES NOT NEED to construct the squares. It can, but it needs not. The output $n$ can be found in a purely mathematical way.
Best Answer from StackOverflow
Question Source : http://cs.stackexchange.com/questions/44517 Ask a Question Download Related Notes/Documents