I attached a picture: the shaded areas (gray color) are stored in the database, as are the coordinates of the upper left corner and the lower right, i.e. FOUR numbers (x1, y1, x2, y2). As a query to the database MySql get the coordinates of the free area, if the conditions are:
- starting from the top left find a free area of 1x1
- starting from the top left find a free area of 2x2
That is, for item 1, the answer should be 0,0,1,1 (blue square) for p. 2 = 6,0,8,2 (orange square)
All that was enough knowledge)) is the MIN function to find the free coordinate along the X axis, but in combination with the Y axis this is wrong. And for searching for 2x2 areas, searching through MIN is not applicable at all.
Alternatively, I thought of creating another table listing all possible points (i.e., for a given square, this is 10x10 = 100 rows) and ticking off — such and such points are occupied, but such-and-such is not and search among the unoccupied, but if the size is the fields will need to be increased (from 10x10 to for example 100x100), then delays will most likely begin ???
Base:
DROP TABLE IF EXISTS `m_items`; CREATE TABLE `m_items` ( `id` int(11) NOT NULL, `x1` smallint(4) NOT NULL, `y1` smallint(4) NOT NULL, `x2` smallint(4) NOT NULL, `y2` smallint(4) NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `m_items` (`id`, `x1`, `y1`, `x2`, `y2`) VALUES (1, 3, 0, 6, 4), (2, 8, 0, 9, 2), (3, 0, 1, 2, 2), (4, 0, 3, 2, 5), (5, 7, 3, 9, 7), (6, 1, 6, 6, 7), (7, 1, 8, 2, 10);
create table
andinsert
to it - Mike