How to calculate the distance between two x and y points?

108 Views Asked by At

The euclidean formula $\sqrt{{(x_2 - x_1)^2 + (y_2 - y_1)^2}}$ works just fine when $ x{_1} = x{_2} \vee y{_1} = y{_2}$ but when calculating diagonals where $ x{_1} \neq x{_2} \wedge y{_1} \neq y{_2} $ things get a bit odd for my needs because it returns even values.

What I really need is to calculate the difference between two points but didn't find anything about it, for example:

From value $ 8 $ where $x=0 \wedge y=0 $ to center point $ 0 $ where $ x=4 \wedge y=4 $ I would like the result to be $ 4 $.

Matrix

I could divide the result to get what I need but then the other values will be messed up, is there any formula that calculates the difference between two points?

Edit: I will add more details to my question.

Given this matrix of 8x8 squares, suppose that from the given origin (4,4) I have a Cartesian plane where the rows are represented by Y axis and columns by X axis:

Cartesian plane


Extending arrows from the origin until the edges I have the positions where I need to perform the calculations

straight lines from origin


As said before, using the euclidean formula I can find the amount of square from the origin to the desired position but only for X and Y axes.

For instance, the amount of square from origin(4,4) to position(4,1) = 3 squares

First example


Another example, from origin(4,4) to (2,6) = 2 squares

Second example


The desired behavior for diagonals is that from given origin (4,4) to position (0,0) it results in 4 not 8.

Desired behavior

So, my main question is how could I calculate the distance between two squares in any origin to any desired position(within the presented range), but in such way that also works for diagonals, not only for X and Y axes.