1
$\begingroup$

Given an adjacency matrix on n vertices and a polygon on n vertices, how to add edges from adjacency matrix on the Polygon?

eg: if polygon is {(0,0),(0,1),(1,0),(1,1)} and the adjacency matrix has 1 in the position corresponding to vertices 1 and 4 then how to draw that line on the polygon.

It would be great if there is a function to execute this.

$\endgroup$
2
$\begingroup$
SeedRandom[5]
pts = RandomReal[1, {6, 2}];
pts = pts[[FindShortestTour[pts][[2]]]];
am = RandomChoice[{.7, .3} -> {0, 1}, {6, 6}];

AdjacencyGraph using the polygon vertices as vertex coordinates:

Labeled[AdjacencyGraph[am, VertexCoordinates -> pts, DirectedEdges -> False, 
                       Vertexlabels->"Name", Prolog -> {Yellow, Polygon[pts]}], 
        MatrixForm[am], Left]

Mathematica graphics

OP's example:

pts = {{0, 0}, {0, 1}, {1, 0}, {1, 1}};
am = {{0, 0, 0, 1}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}};
Labeled[AdjacencyGraph[am, VertexCoordinates -> pts, DirectedEdges -> False, 
                  VertexLabels->"Name", Prolog -> {Opacity[.5, Yellow], Polygon[pts]}], 
        MatrixForm[am], Left]

Mathematica graphics

$\endgroup$
  • $\begingroup$ Elegant... but it doesn't show the polygon. $\endgroup$ – David G. Stork May 26 '16 at 23:05
1
$\begingroup$

Here's an example where the adjacency matrix shows a link from pt[[1]] to pt[[4]] and from pt[[2]] to pt[[3]]:

pts = {{0, 0}, {0, 1}, {1, 1}, {1, 0}};
adMat = {{0, 0, 0, 1},
   {0, 0, 0, 0},
   {0, 0, 0, 0},
   {0, 1, 0, 0}};
myfig = Graphics[

  {{Opacity[0.2], Yellow, Polygon[pts]},
   {Red, PointSize[0.02], Point[pts]}, 
   Line@({pts[[#1]], pts[[#2]]} & @@@ Position[adMat, 1])
   }]

enter image description here

This assumes one needs only one entry to link two points (e.g., just 1 -> 2 without 2 -> 1).

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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