ramadanproject/app/Services/SelectionMapper.php
Ahmed Darrazi 45a147253c
Some checks failed
tests / ci (push) Failing after 6m13s
linter / quality (pull_request) Failing after 58s
linter / quality (push) Failing after 1m19s
tests / ci (pull_request) Failing after 5m28s
feat(public-grid): add QA, quickstart, decision docs; scheduler docs; ignore files; tasks updates; run pint
2026-01-03 04:56:12 +01:00

33 lines
1.1 KiB
PHP

<?php
namespace App\Services;
class SelectionMapper
{
/**
* Map a pixel-based selection rectangle to cell-unit coordinates.
*
* @param float $x Top-left x in pixels (canvas coords)
* @param float $y Top-left y in pixels (canvas coords)
* @param float $w Width in pixels
* @param float $h Height in pixels
* @param float $offsetX Canvas offset X (pan)
* @param float $offsetY Canvas offset Y (pan)
* @param int $cellSize Cell size in pixels
* @return array{ x:int, y:int, w:int, h:int }
*/
public function mapPixelsToCells(float $x, float $y, float $w, float $h, float $offsetX, float $offsetY, int $cellSize): array
{
$relX = $x - $offsetX;
$relY = $y - $offsetY;
$cellX = (int) max(0, floor($relX / $cellSize));
$cellY = (int) max(0, floor($relY / $cellSize));
$wCells = (int) max(1, ceil($w / $cellSize));
$hCells = (int) max(1, ceil($h / $cellSize));
return ['x' => $cellX, 'y' => $cellY, 'w' => $wCells, 'h' => $hCells];
}
}