6
$\begingroup$

In Mathematica I'm trying to render a polygon as a set of vertices and directed edges.

What I have so far:

Graphics[Polygon[{{1, 10}, {2, 4}, {10, 5}, {20, 10}}]]

I see Mathematica has the Graph and PathGraph commands - both of which have a DirectedEdges option - but it seems like I have no control over the position of the vertices with these commands.

If I could customize the fill and edge/stroke of the Polygon command, that would be acceptable - but I'm not seeing how to do it. It looks like this command is specifically meant to draw filled polygons.

I'd also be OK with a custom Mathematica routine to draw what I want using a loop and multiple Line commands within a Graphics command - but I can't see how to draw the lines as arrows (I'm sure I could make the routine do this with three Lines per edge, but I really just think I'm missing something here.)

$\endgroup$
0

3 Answers 3

4
$\begingroup$

Graph has the option VertexCoordinates which allows you to specify the coordinates of the vertices, so you could do something like

crds = {{1, 10}, {2, 4}, {10, 5}, {20, 10}};
vertices = Range[Length[crds]];
edges = Thread[vertices \[DirectedEdge] RotateLeft[vertices]];

Graph[vertices, edges, VertexCoordinates -> crds,
 EdgeShapeFunction -> GraphElementData[{"Arrow", "ArrowSize" -> .1}]]

Mathematica graphics

You can also use Graphics primitives, for example

edges1 = Thread[{crds, RotateLeft[crds]}];

Graphics[{Red, Arrow /@ edges1}]

Mathematica graphics

$\endgroup$
1
  • $\begingroup$ Loving the new Mathematica SE site! That's awesome... thank you very much. Just what I was looking for. $\endgroup$
    – Steve
    May 28, 2012 at 22:05
4
$\begingroup$
n = 5;
Graphics@Arrow@Table[{Sin[2 Pi i/n], Cos[2 Pi i/n]}, {i, 1, n + 1}]

enter image description here

Edit

GraphicsGrid[Partition[
  Table[Graphics@
    Arrow@Partition[Table[{Sin[2 Pi i/j], Cos[2 Pi i/j]}, {i, 1, j + 1}], 2, 1], 
   {j,3, 12}], 3], Frame -> All]

enter image description here

$\endgroup$
1
  • $\begingroup$ Thanks! Two good answers... but I had to pick one. I really appreciate the help. $\endgroup$
    – Steve
    May 28, 2012 at 22:11
4
$\begingroup$

You had this already

pol = Graphics[Polygon[{{1, 10}, {2, 4}, {10, 5}, {20, 10}}]];

You can turn it into something similar to what you want with

pol /. Polygon[i_] :> Thread@Arrow@Partition[i, 2, 1, 1]

Mathematica graphics

$\endgroup$
1
  • $\begingroup$ +1 Very nice! Thank you! $\endgroup$
    – Steve
    May 29, 2012 at 15:21

Your Answer

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

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