33 lines
1.1 KiB
PHP
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];
|
|
}
|
|
}
|