manipulate element within a distance of x

I am looking for a method in which i can manipulate elements in a matrix a certain distance, from a certain point.

an example could be.

given

1
2
3
4
int matrix[4][10]  = {{1,2,3,1,2,3,1,2,3,1},
                            {1,2,3,1,2,3,1,2,3,1},
                            {1,7,3,1,4,3,3,2,3,1},
                            {1,3,3,1,5,3,4,2,3,5}}

and I say that my starting point is 3,3, and all elements which is distanced 2 element shall be rewritten to 2 such that the matrix will end up looking like this

1
2
3
4
matrix = {{2,2,2,2,2,3,1,2,3,1},
                {[2,2,3,1,2,3,1,2,3,1},
                {[2,7,x,1,2,3,3,2,3,1},
                {[2,3,3,1,2,3,4,2,3,5}}


I hope it makes sense..
I could do it using 8 if for each direction, but that only works for a distance of one..
So if I understand correctly, you want to locate a matrix element x and change all matrix elements a distance d away from that data point to some value y.

I think on the surface it's pretty easy to do:

1
2
3
4
5
6
7
8
int matrix[w][h]; // = ...

int targetX, targetY;   //x, y value of matrix element that change is based off
int distance;              //distance from target that will be changed
int changeValue;       //value to change the matrix elements to

matrix[targetX + distance][targetY] = changeValue;
// ... 


The issue is that if, for example, targetX > distance, you will have to handle that case differently.

The idea is it has to move like a wavefront planer..
I've created a method which does this, but for some reason it isn't doing the correct thing for the adjacent pixels. Only the pixel on the line gets inserted correctly


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
void manipulate(int distance, int replacement,int start_x , int start_y, int **array,int height, int lenght)
{

    if (lenght > (start_y + distance)) {
        if (array[start_x][start_y + distance]  == 0) {
            array[start_x][start_y + distance] = replacement;

        }
    }
    if (height > start_x+distance) {
        if (array[start_x+distance][start_y] == 0) {
            array[start_x+distance][start_y] = replacement;
            
        }
    }
    
    if (start_y-distance > 0) {
        if (array[start_x][start_y-distance] == 0) {
            array[start_x][start_y-distance] = replacement;
        }
    }
   
    if (start_x-distance > 0) {
        if (array[start_x-distance][start_y] == 0) {
            array[start_x-distance][start_y] = replacement;
        }
    }
   
    
    for (int x = 0; x<= distance; x++) {
        if (start_x-distance > 0 && start_y-distance > 0) {
            if (array[start_x-x][start_y + distance] == 0) {
                array[start_x-x][start_y + distance] = replacement;
            }
            if (array[start_x-x][start_y - distance] == 0) {
                array[start_x-x][start_y - distance] = replacement;
                
            }

        }
        
        if (start_y-distance > 0 && start_x-distance > 0) {
        
            if (array[start_x-distance][start_y-x] == 0) {
                array[start_x-distance][start_y-x] = replacement;
            }
            if (array[start_x+distance][start_y-x] == 0) {
                array[start_x+distance][start_y-x] = replacement;
                
            }

        }
        if (lenght > (start_y + distance) && start_x >= distance) {
            if (array[start_x-distance][start_y+x] == 0) {
                array[start_x-distance][start_y+x] = replacement;
                
            }
            
            if (array[start_x+distance][start_y+x]  == 0) {
                array[start_x+distance][start_y+x] = replacement;
            }

        }
        if (height > start_x+distance && start_y >= distance) {
            if (array[start_x+x][start_y - distance] == 0) {
                array[start_x+x][start_y - distance] = replacement+x;
                
            }
            if (array[start_x+x][start_y + distance] ==0) {
                array[start_x+x][start_y + distance] = replacement;
            }
        }
        
       
    }

    
}
Topic archived. No new replies allowed.