Y>Имеется множество прямоугольников, заданных координатами углов:
Y>Нужно получить таблицу, в которой прямоугольники разбиты по линиям пересечений.
Если что, вот моё решение, но не уверен, что пройдёт все тесты:
declare @rect table(id int, x1 int, y1 int, x2 int, y2 int, l int)
declare @result table(x1 int, y1 int, x2 int, y2 int)
insert into @rect(id, x1, y1, x2, y2, l)
values (1, 0, 0, 10, 10, 1),
(2, 1, 1, 5, 5, 2)
;with
x as -- all Xs
(
select x1 as x
from @rect
union
select x2
from @rect
),
xs as -- Xs pairs
(
select
row_number() over (order by x) n,
lag(x, 1, null) over(order by x) as x1, x as x2, (x+lag(x, 1, null) over(order by x))/2.0 m
from x
),
xy as -- all Ys for each pair of Xs
(
select x.n, r.y1 as y
from xs x
join @rect r on r.x1<=m and m<=r.x2
union
select x.n, r.y2
from xs x
join @rect r on r.x1<=m and m<=r.x2
),
res as -- pair of Ys for each pair of Xs as a result rectangles
(
select x.x1, lag(y, 1, null) over(partition by xy.n order by y) y1,
x.x2, y as y2
from xy xy
join xs x on x.n= xy.n
)
insert into @result(x1, y1, x2, y2)
select x1, y1, x2, y2
from res r
where y1 is not null
-- delete new rectangles that placed out of source rects
delete r from @result r
where not exists(select 1 from @rect rs where rs.x1 < (r.x2+r.x1)/2.0 and (r.x2+r.x1)/2.0< rs.x2 and rs.y1 < (r.y2+r.y1)/2.0 and (r.y2+r.y1)/2.0 < rs.y2)
select * from @result