Search code examples
arrayscmultidimensional-arraystructuremath.h

analyze array in a specific direction


I have a dynamically allocated array that contains data for a piece of land:

     0| 1| 2| 3| 4| 5|    ->coordinates
 0|  1  0  8  4  1  1
 1|  7  4  2  6  7  8
 2|  7  4  2  4  3  4
 3|  7  1  6  2  0  8
 4|  0  0  3  3  6  6

The numbers inside represent height. I have to analyze specific data inside to figure out where i can have suitable jumps(Suitable jumps have 3 squares of reinforcement with an increasing slope of up to 45 degrees,followed by 2 squares steep descending slope -80 degrees).

But the catch is that I only need to analyze the heights that are from the direction from east to west:[1]: https://i.sstatic.net/axiNh.png

How can I implement that direction specifically? this is a simplified version of my code so far:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int length=4, width=5;

struct LandData
{
   int height;
};

struct LandData* WritingData()
{

    struct LandData *arr = (struct LandData*)malloc(length* width* sizeof(struct LandData));

    for (int i = 0; i < length ; i++){ //len
        for (int j = 0; j < width; j++){//wid
            ((arr + i*width + j)->height) = rand() % 10;
        }
    }

    int l = 1, h;
    float jumps;

    for (int i = 0; i < length ; i++) {
        for (int j = 0; j < width; j++){
            h = ((arr + i*width + j)->height);
            jumps = tan(h/l); //  cause the angle for the jumps should be 45 deg
            if(jumps <= 1)
            printf("suitable jumps: %d %d\n", i ,j);            
        }
    }

    return(arr);
}

int main()
{
    struct LandData *arr = WritingData();
}

Solution

  • as far as I understand, the direction is important for you. if it's, then the following changing is enough for that.

        for (int i = 0; i < length ; i++) {
        for (int j = width; j > 0; j--){
            h = ((arr + i*width + j)->height);
            jumps = tan(h/l); //  cause the angle for the jumps should be 45 deg
            if(jumps <= 1)
            printf("suitable jumps: %d %d\n", i ,j);            
        }
    }