3
$\begingroup$

I would like to construct a list of polygons by using two different lists of points:

list1={{0, 0.}, {0, 1.18961}, {0, 2.37923}, {0, 3.56884}, {0, 4.75846}, {0, 5.94807}, {0, 7.13769}}

list2 = {{2.70289, 0.512313}, {2.47741, 1.70193}, {2.25193, 2.89154}, {2.02645, 4.08116}, {1.80096, 5.27077}, {1.57548,6.46039}, {1.35, 7.65}}

The sequence/function that I tried to use to build the vertices of the polygons (5 points) is:

fpoly[a_Integer, x_List, y_List] = {x[[a]], y[[a]], y[[a + 1]], x[[a + 1]], x[[a]]}

That option does not work. I think if a type of evaluation control is used the fpoly might be an option, but I do not know how to implement it.

To build all the polygons from fpoly my plan was to use something like:

Table[fpoly[i,list1,list2],{i,1,6}]

Any suggestions, please?

$\endgroup$
2
  • 1
    $\begingroup$ Use SetDelayed, i.e., fpoly[a_Integer, x_List, y_List] := {x[[a]], y[[a]], y[[a + 1]], x[[a + 1]], x[[a]]} $\endgroup$
    – Bob Hanlon
    Feb 22, 2021 at 0:23
  • $\begingroup$ I cannot believe that I missed that. It is so basic! When I thought that I was starting to understand Mathematica and then I missed something some basic. $\endgroup$
    – Drod
    Feb 22, 2021 at 21:38

2 Answers 2

2
$\begingroup$
polygoncoords = (Join @@@ 
   Partition[Transpose[{list1, list2}], 2, 1])[[All, {1, 2, 4, 3, 1}]];

polygons = Polygon /@ polygons;

Graph the third polygon (polygons[[3]]);

Graphics[{Blue, Line@list1, Red, Line@list2, 
  EdgeForm[rc = RandomColor[]], rc, Opacity[.5], polygons[[3]]}]

enter image description here

Graphs polygons 1, 3 and 6 (polygons[[{1, 3, 6}]]):

Graphics[{Blue, Line @ list1, Red, Line @ list2,
 {EdgeForm[rc = RandomColor[]], rc, Opacity[.5], #} & /@ polygons[[{1, 3, 6}]]}]

enter image description here

Graph all 6 polygons:

Graphics[{Blue, Line @ list1, Red, Line @ list2,
  {EdgeForm[rc = RandomColor[]], rc, Opacity[.5], #} & /@ polygons}]

enter image description here

$\endgroup$
1
  • $\begingroup$ This is my favourite answer as it is very functional programming oriented $\endgroup$
    – Drod
    Feb 22, 2021 at 21:36
5
$\begingroup$

We can select two points from the two list alternatively and use ConvexHullMesh to construct the convex polygon.

BTW,Here we have Reverse the order of the second listlist2to keep the orientation of polygon,so we can also replace ConvexHullMesh by Polygonto make the same convex polygons.

list1 = {{0, 0.}, {0, 1.18961}, {0, 2.37923}, {0, 3.56884}, {0, 
    4.75846}, {0, 5.94807}, {0, 7.13769}};

list2 = {{2.70289, 0.512313}, {2.47741, 1.70193}, {2.25193, 
    2.89154}, {2.02645, 4.08116}, {1.80096, 5.27077}, {1.57548, 
    6.46039}, {1.35, 7.65}};
polygons = 
  ConvexHullMesh /@ 
   Join @@@ Tuples[{Subsets[list1, {2}], Subsets[Reverse@list2, {2}]}];
i = RandomInteger[{1, Binomial[7, 2]^2}]
Graphics[{{EdgeForm[Green], FaceForm[Directive[Opacity[.5], Yellow]], 
   polygons[[i]]}, {Red, Point[list1], Arrowheads[.1], 
   Arrow[list1]}, {Blue, Point[list2], Arrowheads[.1], Arrow[list2]}}]

enter image description here

$\endgroup$
1
  • $\begingroup$ I found this solution interesting because it shows a way of using ConvexHullMesh. I really like the utilization of Graphics to show the results $\endgroup$
    – Drod
    Feb 22, 2021 at 21:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.