algorithmpointpseudocodecoordinate

Algorithm to find all diagonal point of a given point


How to write an algorithm to find all diagonal point of a given point?

Such as:

00 01 02 03 04 05
10 11 12 13 14 15
20 21 22 23 24 25
30 31 32 33 34 35
40 41 42 43 44 45
50 51 52 53 54 55

When selected a point 34, how do I find the neighboring diagonal points? (01,12,23,45,25,43,52)

00(01)02 03 04 05
10 11(12)13 14 15
20 21 22(23)24(25)
30 31 32 33  X 35
40 41 42(43)44(45)
50 51(52)53 54 55

Since I'm not stating the programming language I use, I would more prefer pseudocode or instructions.

Ps: I don't want direct code giveaway as I'm trying to learn to code by following pseudocode or instructions.


Solution

  • Let's say your point is on (a, b). A given (c, d) point is on the same diagonal if exists an i integer, so that

    a + i = c and b + i = d or a + i = c and b - i = d

    since the distance is i, you can do the following:

    for i <- 0, n - 1
        if i <> a then
            if (b - i) >= 0 and (b - i) < n then (i, b - i) is on the diagonal
            if (b + i) >= 0 and (b + i) < n then (i, b + i) is on the diagonal
        end if
    end for