{"id":718,"date":"2012-01-25T06:09:05","date_gmt":"2012-01-25T06:09:05","guid":{"rendered":"http:\/\/41j.com\/blog\/?p=718"},"modified":"2012-01-25T14:49:41","modified_gmt":"2012-01-25T14:49:41","slug":"what-spot-in-manhattan-is-farthest-from-any-subway-stop","status":"publish","type":"post","link":"https:\/\/41j.com\/blog\/2012\/01\/what-spot-in-manhattan-is-farthest-from-any-subway-stop\/","title":{"rendered":"What spot in Manhattan is farthest from any subway stop?"},"content":{"rendered":"<p>I thought I&#8217;d have a crack and writing some code to answer <a href=\"http:\/\/www.quora.com\/What-spot-in-Manhattan-is-farthest-from-any-subway-stop\">this<\/a> Quora question.<\/p>\n<p>Here is the resulting map, pixel value (grey scale) indicates pixel distance from nearest subway station:<\/p>\n<p><a href=\"http:\/\/41j.com\/blog\/wp-content\/uploads\/2012\/01\/distmap.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/41j.com\/blog\/wp-content\/uploads\/2012\/01\/distmap-431x1024.png\" alt=\"\" title=\"distmap\" width=\"431\" height=\"1024\" class=\"aligncenter size-large wp-image-719\" \/><\/a><\/p>\n<p>I&#8217;m afraid the overlay is a bit of a mess, I&#8217;ll try and fix this tomorrow. But according to this map the answer is the far corner of Battery Park.<\/p>\n<p>Here&#8217;s the code I used:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\r\n#include &lt;png++\/png.hpp&gt;\r\n#include &lt;iostream&gt;\r\n#include &lt;math.h&gt;\r\n\r\nusing namespace std;\r\n\r\nbool is_black(png::image&lt; png::rgba_pixel &gt; &amp;image,size_t x,size_t y) {\r\n\r\n  if(((image&#x5B;y]&#x5B;x].red == 0) &amp;&amp; (image&#x5B;y]&#x5B;x].green == 0) &amp;&amp; (image&#x5B;y]&#x5B;x].blue == 0))) return true;\r\n\r\n  return false;\r\n}\r\n\r\nint get_distance(int x,int y,int cx,int cy) {\r\n  int xd = (cx-x);\r\n  if(xd&lt;0) xd = 0-xd;\r\n\r\n  int yd = cy-y;\r\n  if(yd&lt;0) yd = 0-yd;\r\n\r\n  return sqrt((xd*xd)+(yd*yd));\r\n}\r\n\r\nint get_nextdist(png::image&lt; png::rgba_pixel &gt; &amp;image,size_t xin,size_t yin) {\r\n\r\n  int x = xin;\r\n  int y = yin;\r\n\r\n  int mindist = 10000;\r\n  for(int cx=(x-500);cx&lt;(x+500);cx++) {\r\n    for(int cy(y-500);cy&lt;(y+500);cy++) {\r\n\r\n      if((cx &gt; 0) &amp;&amp; (cy &gt; 0) &amp;&amp; (cx &lt; image.get_width()) &amp;&amp; (cy &lt; image.get_height())) {\r\n\r\n        if(is_black(image,cx,cy)) {\r\n          int dist = get_distance(x,y,cx,cy);\r\n          if(dist &lt; mindist) mindist = dist;\r\n        }\r\n\r\n      }\r\n    }\r\n  }\r\n\r\n  return mindist;\r\n}\r\n\r\n\r\nint main() {\r\n\r\n  png::image&lt; png::rgba_pixel &gt; image(&quot;input.png&quot;);\r\n\r\n  \/\/ Make image black and white.\r\n  for (size_t y = 0; y &lt; image.get_height(); ++y) {\r\n    for (size_t x = 0; x &lt; image.get_width(); ++x) {\r\n      if(((image&#x5B;y]&#x5B;x].red &lt;  80 ) &amp;&amp; (image&#x5B;y]&#x5B;x].green &lt;  80 ) &amp;&amp; (image&#x5B;y]&#x5B;x].blue &lt;  80 ) &amp;&amp; (image&#x5B;y]&#x5B;x].alpha != 0)) ||\r\n         ((image&#x5B;y]&#x5B;x].red == 255) &amp;&amp; (image&#x5B;y]&#x5B;x].green == 255) &amp;&amp; (image&#x5B;y]&#x5B;x].blue == 255) &amp;&amp; (image&#x5B;y]&#x5B;x].alpha != 0))) {\r\n        image&#x5B;y]&#x5B;x] = png::rgba_pixel(0, 0, 0);\r\n      } else {\r\n        image&#x5B;y]&#x5B;x] = png::rgba_pixel(255, 255, 255);\r\n      }\r\n    }\r\n  }\r\n\r\n  png::image&lt; png::rgba_pixel &gt; oimage = image;\r\n  \/\/ Filter out isolated pixels...\r\n  for (size_t y = 1; y &lt; (image.get_height()-1); ++y) {\r\n    for (size_t x = 1; x &lt; (image.get_width()-1); ++x) {\r\n      if(is_black(oimage,x,y)) {\r\n        int adj = 0;\r\n        if(is_black(oimage,x+1,y  )) {adj++; }\r\n        if(is_black(oimage,x-1,y  )) {adj++; }\r\n        if(is_black(oimage,x  ,y+1)) {adj++; }\r\n        if(is_black(oimage,x  ,y-1)) {adj++; }\r\n        if(adj &gt; 3) {\r\n          \/\/ do nothing...\r\n          image&#x5B;y]&#x5B;x] = png::rgba_pixel(0, 0, 0,255);\r\n        } else {\r\n          image&#x5B;y]&#x5B;x] = png::rgba_pixel(255, 255, 255,255);\r\n        }\r\n      }\r\n    }\r\n  }\r\n\r\n\r\n  oimage = image;\r\n  \/\/ Set each pixel value to min distance to nearest pixel.\r\n\r\n  for (size_t y = 0; y &lt; image.get_height(); ++y) {\r\n    for (size_t x = 0; x &lt; image.get_width(); ++x) {\r\n      if(!is_black(oimage,x,y)) {\r\n        cout &lt;&lt; &quot;processing: &quot; &lt;&lt; x &lt;&lt; &quot;,&quot; &lt;&lt; y &lt;&lt; endl;\r\n\r\n        int distance = get_nextdist(oimage,x,y);\r\n\r\n        image&#x5B;y]&#x5B;x] = png::rgba_pixel(distance, distance, distance,255);\r\n      }\r\n    }\r\n  }\r\n\r\n\r\n image.write(&quot;output.png&quot;);\r\n}\r\n<\/pre>\n<h2>Assorted files used&#8230;<\/h2>\n<p>Input to the above code:<br \/>\n<a href=\"http:\/\/41j.com\/blog\/wp-content\/uploads\/2012\/01\/input.real_.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/41j.com\/blog\/wp-content\/uploads\/2012\/01\/input.real_-431x1024.png\" alt=\"\" title=\"input.real\" width=\"431\" height=\"1024\" class=\"aligncenter size-large wp-image-720\" srcset=\"https:\/\/41j.com\/blog\/wp-content\/uploads\/2012\/01\/input.real_-431x1024.png 431w, https:\/\/41j.com\/blog\/wp-content\/uploads\/2012\/01\/input.real_-126x300.png 126w\" sizes=\"auto, (max-width: 431px) 100vw, 431px\" \/><\/a><\/p>\n<p>Original image:<br \/>\n<a href=\"http:\/\/41j.com\/blog\/wp-content\/uploads\/2012\/01\/subwaymap.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/41j.com\/blog\/wp-content\/uploads\/2012\/01\/subwaymap-431x1024.png\" alt=\"\" title=\"subwaymap\" width=\"431\" height=\"1024\" class=\"aligncenter size-large wp-image-721\" srcset=\"https:\/\/41j.com\/blog\/wp-content\/uploads\/2012\/01\/subwaymap-431x1024.png 431w, https:\/\/41j.com\/blog\/wp-content\/uploads\/2012\/01\/subwaymap-126x300.png 126w\" sizes=\"auto, (max-width: 431px) 100vw, 431px\" \/><\/a><\/p>\n<p>Maps from:<br \/>\nhttp:\/\/www.mta.info\/nyct\/maps\/submap.htm (cropped from PDF).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I thought I&#8217;d have a crack and writing some code to answer this Quora question. Here is the resulting map, pixel value (grey scale) indicates pixel distance from nearest subway station: I&#8217;m afraid the overlay is a bit of a mess, I&#8217;ll try and fix this tomorrow. But according to this map the answer is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[],"class_list":["post-718","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1RRoU-bA","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/718","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/comments?post=718"}],"version-history":[{"count":3,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/718\/revisions"}],"predecessor-version":[{"id":724,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/718\/revisions\/724"}],"wp:attachment":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/media?parent=718"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/categories?post=718"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/tags?post=718"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}