I’m trying to get the array of fixed size squares from a bigger square. For example:
I have coordinates of a square:
$x1 = 1;
$x2 = 100;
$y1 = 1;
$y2 = 100;
$smallerSquareSide = 10;
What I expect as result:
$res = [[1,10,1,10],[11,20,1,10],[21,30,1,10],…]
But I have no idea how to do that correctly.
P.S. there may be empty spaces around the borders because the numbers might not match
CodePudding user response:
this should work (format of output: res=[square1, square2, square3, ...], squareN=[x1, x2, y1, y2])
$x1 = 1;
$x2 = 100;
$y1 = 1;
$y2 = 100;
$smallerSquareSide = 10;
$res = [];
for($i=$y1; $i ($smallerSquareSide-1)<=$y2; $i =$smallerSquareSide){ // loop y
for($j=$x1; $j ($smallerSquareSide-1)<=$x2; $j =$smallerSquareSide){ // loop x
// add next square coords -> [$j, $j ($smallerSquareSide-1), $i, $i ($smallerSquareSide-1)]
array_push($res, [$j, $j ($smallerSquareSide-1), $i, $i ($smallerSquareSide-1)]);
}
}