1
votes

I have an 800 by 600 image. I want to treat it like a matrix and get the adjacent elements

ex.

(0,0) (1,0) (2,0) (3,0)

(0,1) (1,1) (2,1) (3,1)

(0,2) (1,2) (2,2) (3,2)

(0,3) (1,3) (2,3) (3,3)

example solutions: (0,0) is adjacent to: (1,0) (0,1) (1,1)

(1,1) is adjacent to: (0,0) (1,0) (2,0) (2,1) (2,2) (1,2) (0,2) (0,1)

so I have written a struct array that i will store each one of these points into

typdef struct point
{
    int x;
    int y;
}point[800*600];

so my first idea was to implement a dfs but that did not really work out so I wanted to get an outside opinion to keep myself on the right track. thanks

1
It seems like you're overthinking this problem - why not just use a 2D array?Carl Norum
this is in a C question anymore - you need to fix the tags.Carl Norum
What do you mean by "store each one of these points"? What are you storing? color values?Rumple Stiltskin
imagine you have arr an object of type point. What would there be in arr[0].x? And in arr[0].y? And in arr[800].x? ...pmg

1 Answers

0
votes

The final answer depends on how you visualize the arrangement of your 2D display into the 1D array ('row-first column-last' or 'column-first row-last').

Assuming 'row-first' (pixels along the row increment by 1, pixels along the column increment by ROW_LENGTH):

First use some definitions to set values for ROW_LENGTH and COL_LENGTH

#define ROW_LENGTH 800
#define COL_LENGTH 600

Then you can easily adjust your size if needs change without affecting how the rest of the code works.

typdef struct point
{
    int x;
    int y;
}point[ROW_LENGTH*COL_LENGTH];

Later, adjacent points can be obtained with something like:

adjacentWest = point[(y*ROW_LENGTH) + (x-1)];
adjacentNorth = point[((y+1)*ROW_LENGTH) + x];

You'll need to adjust North and South to be +1 or -1 depending on if your origin is in the top-left or bottom-left of your display.