$x_min, 'x_max' => $x_max, 'y_min' => $y_min, 'y_max' => $y_max]; function increment(&$x, &$y, &$x_velocity, &$y_velocity) { $current_x = $x; $x += $x_velocity; $y += $y_velocity; if ($x_velocity > 0) $x_velocity -= 1; elseif ($x_velocity < 0) $x_velocity += 1; else $x_velocity = 0; $y_velocity -= 1; } function check($x, $y, $target) { return $x >= $target['x_min'] && $x <= $target['x_max'] && $y >= $target['y_min'] && $y <= $target['y_max']; } $x = 0; $y = 0; $x_velocity = 6; $y_velocity = 9; $possibilities = []; for ($i = 0; $i <= abs($target['x_max']); $i++) { for ($j = -abs($target['y_min']); $j <= abs($target['y_min']); $j++) { if ($i === 0 && $j === 0) continue; $x = 0; $y = 0; $x_velocity = $i; $y_velocity = $j; while(!check($x, $y, $target)) { echo sprintf('x: %s, y: %s%s', $x, $y, "\n"); increment($x, $y, $x_velocity, $y_velocity); if ($y <= $target['y_min']) { break; } } if (check($x, $y, $target)) { $possibilities[] = sprintf('%s,%s', $i, $j); } } } echo count($possibilities);