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$

migrated from math.stackexchange.com May 28 '12 at 21:45

This question came from our site for people studying math at any level and professionals in related fields.

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$
  • $\begingroup$ Loving the new Mathematica SE site! That's awesome... thank you very much. Just what I was looking for. $\endgroup$ – Steve May 28 '12 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$
  • $\begingroup$ Thanks! Two good answers... but I had to pick one. I really appreciate the help. $\endgroup$ – Steve May 28 '12 at 22:11
3
$\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$
  • $\begingroup$ +1 Very nice! Thank you! $\endgroup$ – Steve May 29 '12 at 15:21

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.