java easy to understand solution
\nclass Solution {\n    public int[][] spiralMatrixIII(int rows, int cols, int rStart, int cStart) {\n        int[][] matrix=new int[rows*cols][2];\n        int
```\nclass Solution {\n    public int[][] spiralMatrixIII(int rows, int cols, int rStart, int cStart) {\n        int[][] matrix=new int[rows*cols][2];\n        int i=0;\n        matrix[i++]=new int[]{rStart,cStart};  //adding the initial position\n        int d=0;          // this will be used for direction there can be 4 direction only 0 1 2 3 \n\t\tint len=0;          //this is to take the number of steps;\n        int[] directions=new int[]{0,1,0,-1,0};   //\n        while(i<rows*cols){\n            if(d==0||d==2){   // 0 for east 1 for south 2 for west and 3 for south \n                len++;              //because only when we are facing east  or facing west then we are moving 1 step more\n            }\n            for(int k=0;k<len;k++){    //this for loop is to give the position after each step\n                rStart+=directions[d];     //when facing east the x coordinate will not be changing only y coordinate will be changing that\'s why cStart is incremented\n                cStart+=directions[d+1];\n                if(rStart<rows&&rStart>=0&&cStart<cols&&cStart>=0){\n                    matrix[i++]=new int[]{rStart,cStart};         //this condition is to ensure we are only adding the coordinates of position which are in the matrix \n                }\n            }\n            d=++d%4;       //this is to ensure d values never exceed 3 because we only want 4 direction and d value will be changing many times \n            \n        }\n        return matrix;\n    }\n}\n```
['Java']