1

I have the following dataset:

plgs = {{{1522., 388.5}, {1543.18, 414.971}, {1522., 406.5}}, {{1508.5, 
421.5}, {1508.5, 400.528}, {1515.69, 404.923}, {1511.83, 
416.5}}, {{565.455, 408.545}, {576.778, 397.222}, {594., 
426.5}, {614.375, 467.25}}, {{559.605, 386.053}, {548.065, 
416.826}, {213.929, 667.429}, {538.434, 363.636}}, {{1503.8, 
392.6}, {1502.82, 395.128}, {1369.24, 389.057}, {1491.38, 
347.071}}, {{1560.31, 482.308}, {1603.53, 445.263}, {1621.91, 
433.98}, {1715.11, 637.109}}, {{1097.69, 735.5}, {1091.99, 
283.009}, {1145.27, 324.965}, {1284.22, 735.5}}, {{246.529, 
735.5}, {648.257, 525.978}, {814.646, 461.982}, {821.031, 
735.5}}, {{821.031, 735.5}, {814.646, 461.982}, {889.082, 
353.262}, {928.152, 300.524}, {971.65, 735.5}}, {{1502.82, 
395.128}, {1503.35, 396.805}, {1434.43, 560.485}, {1281.53, 
410.475}, {1369.24, 389.057}}, {{1503.35, 396.805}, {1508.5, 
400.528}, {1508.5, 421.5}, {1437.37, 563.76}, {1434.43, 
560.485}}, {{1543.18, 414.971}, {1546., 416.5}, {1511.83, 
416.5}, {1515.69, 404.923}, {1522., 406.5}}, {{577.11, 
395.394}, {591., 393.5}, {594., 393.5}, {594., 426.5}, {576.778, 
397.222}}, {{576.682, 394.591}, {559.605, 386.053}, {538.434, 
363.636}, {544.174, 351.773}, {587.882, 338.591}}, {{577.11, 
395.394}, {576.682, 394.591}, {587.882, 338.591}, {591., 
336.776}, {591., 393.5}}, {{1484.16, 735.5}, {1446.59, 
574.589}, {1560.31, 482.308}, {1715.11, 637.109}, {1762.07, 
735.5}}, {{758.94, 231.5}, {591., 336.776}, {587.882, 
338.591}, {544.174, 351.773}, {451.872, 231.5}}, {{1646., 
231.5}, {1520., 384.5}, {1503.8, 392.6}, {1491.38, 
347.071}, {1643.73, 231.5}}, {{1762.07, 735.5}, {1715.11, 
637.109}, {1621.91, 433.98}, {1745.26, 231.5}, {2001.25, 
231.5}, {2001.25, 735.5}}, {{594., 426.5}, {594., 393.5}, {889.082,
353.262}, {814.646, 461.982}, {648.257, 525.978}, {614.375, 
467.25}}, {{971.65, 735.5}, {928.152, 300.524}, {974.766, 
231.5}, {1058.03, 231.5}, {1091.99, 283.009}, {1097.69, 
735.5}}, {{576.682, 394.591}, {577.11, 395.394}, {576.778, 
397.222}, {565.455, 408.545}, {548.065, 416.826}, {559.605, 
386.053}}, {{451.872, 231.5}, {544.174, 351.773}, {538.434, 
363.636}, {213.929, 667.429}, {208.75, 671.951}, {208.75, 
231.5}}, {{1284.22, 735.5}, {1145.27, 324.965}, {1281.53, 
410.475}, {1434.43, 560.485}, {1437.37, 563.76}, {1446.59, 
574.589}, {1484.16, 735.5}}, {{974.766, 231.5}, {928.152, 
300.524}, {889.082, 353.262}, {594., 393.5}, {591., 393.5}, {591., 
336.776}, {758.94, 231.5}}, {{1603.53, 445.263}, {1560.31, 
482.308}, {1446.59, 574.589}, {1437.37, 563.76}, {1508.5, 
421.5}, {1511.83, 416.5}, {1546., 416.5}}, {{1643.73, 
231.5}, {1491.38, 347.071}, {1369.24, 389.057}, {1281.53, 
410.475}, {1145.27, 324.965}, {1091.99, 283.009}, {1058.03, 
231.5}}, {{208.75, 735.5}, {208.75, 671.951}, {213.929, 
667.429}, {548.065, 416.826}, {565.455, 408.545}, {614.375, 
467.25}, {648.257, 525.978}, {246.529, 735.5}}, {{1520., 
384.5}, {1522., 388.5}, {1522., 406.5}, {1515.69, 
404.923}, {1508.5, 400.528}, {1503.35, 396.805}, {1502.82, 
395.128}, {1503.8, 392.6}}, {{1745.26, 231.5}, {1621.91, 
433.98}, {1603.53, 445.263}, {1546., 416.5}, {1543.18, 
414.971}, {1522., 388.5}, {1520., 384.5}, {1646., 231.5}}}

Now, I want to make a for loop that can find the adjacent neighbor for last three polygon values. Here is the following code I tried for finding neighbor of the last three polygon. But, it gives error. How can I run the for loop?

For[i = 1, i < sv + 1, i++, 
  iLength = Length[plgs[[-i]]]
  neigh[[i]] = 
    Select[
      plgs[[;; -i - 1]], 
      Length[Union[Join[#, plgs[[-i]]]]] =!= Length[#] + iLength &]]
  aa = neigh[[1]]
J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
Odrisso
  • 191
  • 6

1 Answers1

1

You have several post leading up to this one and it appears that you have been working up to this for a couple of days. Please read What are the most common pitfalls awaiting new users?. This will really help you out as Wolfram Language is different most traditional programming languages.

You don't need a loop to code this the Wolfram Language way.

Taking adjacent to mean that a polygon shares a vertex with the central polygon then an example of how to code this and display this is.

Graphics[{White, EdgeForm[Black], Polygon@plgs,
  LightGray, Polygon@ 
   Flatten[Through[(Select /@ Thread@ContainsAny[plgs[[-3 ;;]]])[plgs[[;; -4]]]], 1],
  Blue, Polygon@plgs[[-3 ;;]]}]

enter image description here

Have a read of how each of the functions work in the documentation. Map (\@) and Span (;;) are particularly useful. These and a few others pretty much negate the need for loops in most cases.

Hope this helps.

Edmund
  • 42,267
  • 3
  • 51
  • 143