{"id":296582,"date":"2019-12-30T09:00:11","date_gmt":"2019-12-30T09:00:11","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=296582"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=296582","title":{"rendered":"\u0420\u0438\u0441\u0443\u0435\u043c \u043c\u043e\u0440\u043e\u0437\u043d\u044b\u0435 \u0443\u0437\u043e\u0440\u044b \u043d\u0430 SQL"},"content":{"rendered":"\n<div class=\"post__text post__text-html js-mediator-article\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/482460\/\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/i6\/w5\/tx\/i6w5txjtftpyzzp2-euijaew6rw.png\"><\/p>\n<p>  \u041d\u0435\u043c\u043d\u043e\u0433\u043e SQL-\u043c\u0430\u0433\u0438\u0438 \u043f\u043e\u0434 \u043a\u0430\u0442\u043e\u043c: \u043c\u0430\u0442\u0435\u043c\u0430\u0442\u0438\u043a\u0430, \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044f, \u043f\u0441\u0435\u0432\u0434\u043e\u0433\u0440\u0430\u0444\u0438\u043a\u0430.<\/p>\n<p>  \u0412\u0441\u043f\u043e\u043c\u0438\u043d\u0430\u0435\u043c \u043f\u043e\u0434 \u041d\u043e\u0432\u044b\u0439 \u0433\u043e\u0434 \u0444\u043e\u0440\u043c\u0443\u043b\u0443 \u0443\u0433\u043b\u0430 \u043c\u0435\u0436\u0434\u0443 \u0432\u0435\u043a\u0442\u043e\u0440\u0430\u043c\u0438:<br \/>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/qh\/l8\/jf\/qhl8jfhpeb8sgxqqnkjgyhwv8h4.png\"><br \/>  <a name=\"habracut\"><\/a>  <\/p>\n<pre><code class=\"sql\">WITH RECURSIVE T AS (   SELECT     0 x   , 0 y   , '{\"{0,0}\"}'::text[] c -- \u0440\u0430\u0441\u0442\u0438\u043c \u0443\u0437\u043e\u0440 \u043e\u0442 \u0446\u0435\u043d\u0442\u0440\u0430   , 0 i UNION ALL   (     WITH Z AS (       SELECT         dn.x       , dn.y       , T.c       , T.i       FROM         T         -- \u0432\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u0443\u044e \u0442\u043e\u0447\u043a\u0443 \u043d\u0430 \u043f\u043b\u043e\u0441\u043a\u043e\u0441\u0442\u044c       , LATERAL(           SELECT             ((random() * 2 - 1) * 100)::integer x           , ((random() * 2 - 1) * 100)::integer y         ) p         -- \u0438\u0437 \u0432\u0441\u0435\u0445 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0445 \u0442\u043e\u0447\u0435\u043a \u0432\u044b\u0431\u0438\u0440\u0430\u0435\u043c \u0443 \u043d\u0435\u0439 \u0431\u043b\u0438\u0436\u0430\u0439\u0448\u0443\u044e       , LATERAL(           SELECT             *           FROM             (               SELECT                 (unnest::text[])[1]::integer x               , (unnest::text[])[2]::integer y               FROM                 unnest(T.c::text[])             ) T           ORDER BY             sqrt((x - p.x) ^ 2 + (y - p.y) ^ 2)           LIMIT 1         ) n         -- \u0438\u0437 8 \u0435\u0435 \u0441\u043e\u0441\u0435\u0434\u0435\u0439 \u0437\u0430\u043f\u043e\u043b\u043d\u044f\u0435\u043c \u0431\u043b\u0438\u0436\u0430\u0439\u0448\u0443\u044e \u043f\u043e \u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044e \u043a \u0432\u0431\u0440\u043e\u0448\u0435\u043d\u043d\u043e\u0439       , LATERAL (         SELECT           n.x + dx x         , n.y + dy y         FROM           generate_series(-1, 1) dx         , generate_series(-1, 1) dy         WHERE           (dx, dy) &lt;&gt; (0, 0)         ORDER BY           CASE             WHEN (p.x, p.y) = (n.x, n.y) THEN 0             ELSE abs(acos(((p.x - n.x) * dx + (p.y - n.y) * dy) \/ sqrt((p.x - n.x) ^ 2 + (p.y - n.y) ^ 2) \/ sqrt(dx ^ 2 + dy ^ 2)))           END         LIMIT 1       ) dn     )     SELECT       Z.x     , Z.y     , Z.c || ARRAY[Z.x, Z.y]::text     , Z.i + 1     FROM       Z     WHERE       Z.i &lt; (1 &lt;&lt; 10)   ) ) -- \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0439 \u0442\u043e\u0447\u043a\u0438 \u0440\u0438\u0441\u0443\u043d\u043a\u0430 \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u043c \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0434\u043e \u0443\u0437\u043e\u0440\u0430 , map AS (   SELECT     gx x   , gy y   , (       SELECT         sqrt((gx - T.x) ^ 2 + (gy - T.y) ^ 2) v       FROM         T       ORDER BY         v       LIMIT 1     ) v   FROM     generate_series(-40, 40) gx   , generate_series(-30, 30) gy ) -- \u0444\u043e\u0440\u043c\u0438\u0440\u0443\u0435\u043c \u0430\u043b\u0444\u0430\u0432\u0438\u0442 \u043e\u0442\u0440\u0438\u0441\u043e\u0432\u043a\u0438 , gr AS (   SELECT     regexp_split_to_array('#*+-. ', '') s ) -- \u0440\u0438\u0441\u0443\u0435\u043c \u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0443 SELECT   string_agg(     coalesce(s[(v * (array_length(s, 1) - 1))::integer + 1], ' ')   , ' '   ORDER BY x) frozen FROM   (     SELECT       x     , y     , v::double precision \/ max(v) OVER() v -- \u043d\u043e\u0440\u043c\u0438\u0440\u0443\u0435\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0439 \u043f\u043e \u043c\u0430\u043a\u0441\u0438\u043c\u0443\u043c\u0443     FROM       map   ) T , gr GROUP BY   y ORDER BY   y; <\/code><\/pre>\n<\/div>\n<p>               <script class=\"js-mediator-script\">!function(e){function t(t,n){if(!(n in e)){for(var r,a=e.document,i=a.scripts,o=i.length;o--;)if(-1!==i[o].src.indexOf(t)){r=i[o];break}if(!r){r=a.createElement(\"script\"),r.type=\"text\/javascript\",r.async=!0,r.defer=!0,r.src=t,r.charset=\"UTF-8\";var d=function(){var e=a.getElementsByTagName(\"script\")[0];e.parentNode.insertBefore(r,e)};\"[object Opera]\"==e.opera?a.addEventListener?a.addEventListener(\"DOMContentLoaded\",d,!1):e.attachEvent(\"onload\",d):d() } } }t(\"\/\/mediator.mail.ru\/script\/2820404\/\",\"_mediator\")}(window);<\/script>      <br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/482460\/\"> https:\/\/habr.com\/ru\/post\/482460\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html js-mediator-article\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/482460\/\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/i6\/w5\/tx\/i6w5txjtftpyzzp2-euijaew6rw.png\"><\/p>\n<p>  \u041d\u0435\u043c\u043d\u043e\u0433\u043e SQL-\u043c\u0430\u0433\u0438\u0438 \u043f\u043e\u0434 \u043a\u0430\u0442\u043e\u043c: \u043c\u0430\u0442\u0435\u043c\u0430\u0442\u0438\u043a\u0430, \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044f, \u043f\u0441\u0435\u0432\u0434\u043e\u0433\u0440\u0430\u0444\u0438\u043a\u0430.<\/p>\n<p>  \u0412\u0441\u043f\u043e\u043c\u0438\u043d\u0430\u0435\u043c \u043f\u043e\u0434 \u041d\u043e\u0432\u044b\u0439 \u0433\u043e\u0434 \u0444\u043e\u0440\u043c\u0443\u043b\u0443 \u0443\u0433\u043b\u0430 \u043c\u0435\u0436\u0434\u0443 \u0432\u0435\u043a\u0442\u043e\u0440\u0430\u043c\u0438:<br \/>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/qh\/l8\/jf\/qhl8jfhpeb8sgxqqnkjgyhwv8h4.png\">  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-296582","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/296582","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=296582"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/296582\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=296582"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=296582"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=296582"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}